闪亮的更新输入选择器问题

时间:2016-11-29 13:06:17

标签: r shiny

我试图在Shiny Dashboard中使用updateSelectInut无济于事。我尝试了多种方法,但无法更新selectInput。数据具有VG和AG列,每列具有多个因子。我想要做的是当我选择一个VG时,然后只更新该VG文件的AG值:

VG  AG
A    1
A    2
B    1
B    3

如果选择了VG,则AG选项将为1,3。

代码如下:

UI<-...

column(3,selectInput("VG.ETD", label = h4("VG"),choices = levels(data$VG),selected ="Y1")),
column(3,selectInput("AC.ETD", label = h4("AC"),choices = levels(data$AC), selected ="X1")),

Server<-function(input, output,session){...

df_b <- reactive({filter(data, VG== input$VG.ETD)})
observe({updateSelectInput(session, 'AC.ETD', choices=levels(droplevels(df_b()$AC)))})

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

#rm(list = ls())
library(shiny)

VG <- c("A","A","B","B")
AG <- c(1,2,1,3)
data <- data.frame(VG, AG)

ui <- fluidPage(
  column(3,selectInput("VG", label = h4("VG.ETD"),choices = levels(data$VG),selected ="Y1")),
  column(3,selectInput("AG", label = h4("AG.ETD"),choices = unique(data$AG),selected ="X1"))
)

server<-function(input,output,session){

  observeEvent(input$VG,{
    updateSelectInput(session, 'AG', choices = unique(data$AG[data$VG %in% input$VG]))
    })
}

shinyApp(ui=ui,server=server)