R 动态选择输入';s,其中随后选择Input';取决于以前的选择

R 动态选择输入';s,其中随后选择Input';取决于以前的选择,r,shiny,R,Shiny,我试图创建一个闪亮的页面,其中包含基于查询结果的动态输入。我希望输入选项取决于前面的selectInput()选择。查询结果作为一系列数据返回。表保存在reactiveValue()中,其结构如下: V1 V2 V3 V4 V5 key_index along with this development comes 45473 第一个selectInput()choices选项只是第一个data.table的粘贴列V1:V5的列表。我试图使下一个和

我试图创建一个闪亮的页面,其中包含基于查询结果的动态输入。我希望输入选项取决于前面的
selectInput()
选择。查询结果作为一系列数据返回。表保存在
reactiveValue()
中,其结构如下:

V1    V2    V3    V4          V5    key_index
along with  this  development comes 45473
第一个
selectInput()
choices选项只是第一个data.table的粘贴列V1:V5的列表。我试图使下一个和后续的
selectInputs()
由其关联表的V1:V5组成,其中该表的V1与上一个表的V5相同。使用示例数据,第二个
selectInput()
的选项将仅由以“comes”开头的数据组成。表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)]))
    )
),

trythis我创建了以下与您的结构相似的数据帧。这是一个3列数据。字母框架:

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
...
您希望用户在
C1.options
中选择一个条目,然后通过
df$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)

ui这是我最终确定的代码。让我最终将其整合在一起的是使用
input[[]]
访问输入变量的内容

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)
          )
        )
     })
   )
 })

<代码>输出$nGracyReStPad,请考虑接受您自己的答案(通过点击复选标记)。这会将此帖子标记为已关闭。
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)
          )
        )
     })
   )
 })