2017-06-16 2 views
0

クエリの結果に基づいて動的な入力を含むShinyページを作成しようとしています。私は入力の選択肢が以前のselectInput()の選択に依存することを望みます。クエリ結果はreactiveValue()で開催されたdata.tablesのシリーズとして返されると、以下のように構成されています動的なselectInputは、後のselectInputが以前の選択に依存します

V1 V2 V3 V4   V5 key_index 
along with this development comes 45473 

最初selectInput()の選択オプションが貼り付けられた列V1のリストだけである:最初のデータのV5。表。私はそれを次の、そしてそれに続くselectInputs()が関連するテーブルのV1:V5で構成され、そのテーブルのV1が前のV5と同じになるようにしようとしています。サンプルデータを使用して、2番目のselectInput()の選択肢は、「来る」で始まるngramsのdata.table2だけで構成されます。これは、私が2番目の入力を設定するために使用できる値を返すような方法で最初の入力を作成しようとしてきた方法ですが、シリーズの最初の入力を過ぎても取得できません。これを行うより良い方法はありますか?

trythis <- reactive({ 
    tmpthis <- unlist(lapply(1:nrow(ngra1), function(n) ngra1[n,5])) 
    return(tmpthis) 
}) 
trythat <- reactive({ 
    tmpthat <- unlist(lapply(1:nrow(ngra1), function(m) paste0(ngra1[m,1:5],collapse=" "))) 
    return(tmpthat) 
}) 
output$oneapage <- renderUI({ 
    sidebarLayout(
    sidebarPanel(
     fluidRow(
     print(trythat()), 
     print(trythis()), 
     thing <- for(j in 1:length(trythat())){paste("'",trythat()[[j]],"' = '",trythis()[[j]],"'")}, 
     selectInput("tester","test",choices = c(thing[1:length(thing)])) 
    ) 
), 
+0

変数入力の内容にアクセスするinput[[]]を使用していた(チェックマークをクリックして)あなた自身の答えを受け入れることを検討してください。これは、この投稿を閉じたものとしてマークします。 – CPak

答えて

0

私は以下のデータフレームを構成しましたが、あなたのものと似ています。これは、文字の3列data.frameです:

df <- data.frame(V1 = letters[1:10], 
        V2 = letters[2:11], 
        V3 = letters[3:12]) 

    V1 V2 V3 
1 a b c 
2 b c d 
3 c d e 
... 

私はchoice1オプションのリスト作成:

C1.options <- with(df, paste(V1,V2,V3)) 
[1] "a b c" "b c d" "c d e" ... 

あなたは、ユーザーがC1.optionsの間でエントリを選択したい、その後、サブセットをdfdf$V1 == choice1$V3(これをC2.optionsと呼ぶ)、、次にと入力して、ユーザにC2.optionsの項目を選択させます。ここで

はそれを行うには、コードブロックです:ここで

ui <- fluidPage( 
      selectInput("choice1", "First Choice", C1.options, selected=NULL), 
      htmlOutput("plot1") # render dynamic selectInput in server 
    ) 

server <- function(input,output){ 
       data <- reactive({ Str <- unlist(strsplit(input$choice1," ")) 
           Str <- Str[length(Str)] # Grab last character in option 1 
           Filt <- df %>% filter(V1 == Str) # Filter by condition 
           C2.options <- with(Filt, paste(V1,V2,V3)) 
           return(C2.options) }) 
       output$plot1 <- renderUI({ selectInput("choice2", "Second Choice", data(), selected=NULL) }) 
      } 

shinyApp(ui=ui,server=server) 
0

は、私は最終的に上の決着コードがあります。私は最終的にすべて一緒にそれを引っ張って許可ビットは

output$ngram_res_panel <- renderUI({ 
    tagList(
    fluidRow(
     column(selectInput("box1",NULL,choices = c("choose",ngram$results[[1]]$th),selected = input$box1,selectize=FALSE),width=5 
    ) 
    ),  
     lapply(2:length(ngram$results), function(i) { 
     tagList(
      fluidRow(
      column(selectInput(inputId = paste0("box", i), label = NULL, 
        choices = c("choose",ngram$results[[i]]$th[which(ngram$results[[i]]$V1 == ngram$results[[i-1]]$last[ 
         which(input[[paste0("box", i-1)]] == ngram$results[[i-1]]$th) 
         ]) 
        ]),selected = input[[paste0("box", i)]],selectize=FALSE 
       ),width = 5) 
     ) 
     ) 
    }) 
    ) 
}) 
関連する問題