R中的动态selectInput闪亮

时间:2015-12-04 03:51:34

标签: r shiny

我有3个selectInput框和4个选项池,可以通过这3个框选择。我希望selectInputs显示的选项在选择其他selectInput时动态更改。但是我想要" NONE"所有三个方框都可以在所有时间点使用。我正在使用的代码是

    library(shiny)
    library(shinydashboard)


    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic selectInput"),
      dashboardSidebar(
        sidebarMenu(
          menuItemOutput("menuitem")
        )
      ),
      dashboardBody(
        uiOutput('heirarchy1'),
        uiOutput('heirarchy2'),
        uiOutput('heirarchy3')
      )
    )

    server <- function(input, output) {
      output$menuitem <- renderMenu({
        menuItem("Menu item", icon = icon("calendar"))
      })

      heirarchy_vector<-c("NONE","A","B","C")
      output$heirarchy1<-renderUI({
        selectInput("heir1","Heirarchy1",c("NONE",setdiff(heirarchy_vector,c(input$heir2,input$heir3))),selected="NONE")
      })


      output$heirarchy2<-renderUI({
        selectInput("heir2","Heirarchy2",c("NONE",setdiff(heirarchy_vector,c(input$heir1,input$heir3))),selected="NONE")
      })

      output$heirarchy3<-renderUI({
        selectInput("heir3","Heirarchy3",c("NONE",setdiff(heirarchy_vector,c(input$heir1,input$heir2))),selected="NONE")
      })

    }

    shinyApp(ui, server)

Any help on this will be greatly appreciated

修改

为此尝试使用updateSelectInput。但是代码似乎没有运行

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
  dashboardHeader(title = "Dynamic selectInput"),
  dashboardSidebar(
    sidebarMenu(
      menuItemOutput("menuitem")
    )
  ),
  dashboardBody(
    selectInput("heir1","Heirarchy1",c("NONE","A","B","C"),selected="NONE"),
    selectInput("heir2","Heirarchy2",c("NONE","A","B","C"),selected="NONE"),
    selectInput("heir3","Heirarchy3",c("NONE","A","B","C"),selected="NONE")
  )
)

server <- function(input, output) {
  output$menuitem <- renderMenu({
    menuItem("Menu item", icon = icon("calendar"))
  })

  heirarchy<-c("A","B","C")

  observe({
    hei1<-input$heir1
    hei2<-input$heir2
    hei3<-input$heir3

    choice1<-c("NONE",setdiff(heirarchy,c(hei2,hei3)))
    choice2<-c("NONE",setdiff(heirarchy,c(hei1,hei3)))
    choice3<-c("NONE",setdiff(heirarchy,c(hei1,hei2)))

    updateSelectInput(session,"heir1",choices=choice1)
    updateSelectInput(session,"heir2",choices=choice2)
    updateSelectInput(session,"heir3",choices=choice3)

  })

 }

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:7)

你关闭了!有两件事,你需要在启动服务器实例时分配会话变量,当你更新选择输入时需要设置选择哪个选项,除了一切看起来都没问题。试试这个:

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
  dashboardHeader(title = "Dynamic selectInput"),
  dashboardSidebar(
    sidebarMenu(
      menuItemOutput("menuitem")
    )
  ),
  dashboardBody(
    selectInput("heir1","Heirarchy1",c("NONE","A","B","C"),selected="NONE"),
    selectInput("heir2","Heirarchy2",c("NONE","A","B","C"),selected="NONE"),
    selectInput("heir3","Heirarchy3",c("NONE","A","B","C"),selected="NONE")
  )
)

server <- function(input, output, session) {
  output$menuitem <- renderMenu({
    menuItem("Menu item", icon = icon("calendar"))
  })

  heirarchy<-c("A","B","C")

  observe({
    hei1<-input$heir1
    hei2<-input$heir2
    hei3<-input$heir3

    choice1<-c("NONE",setdiff(heirarchy,c(hei2,hei3)))
    choice2<-c("NONE",setdiff(heirarchy,c(hei1,hei3)))
    choice3<-c("NONE",setdiff(heirarchy,c(hei1,hei2)))

    updateSelectInput(session,"heir1",choices=choice1,selected=hei1)
    updateSelectInput(session,"heir2",choices=choice2,selected=hei2)
    updateSelectInput(session,"heir3",choices=choice3,selected=hei3)

  })

}

shinyApp(ui, server)