闪亮R:当selectInput值改变时,重置其他输入值

时间:2016-08-10 21:45:09

标签: r input shiny

我有一个简单的应用,用户从selectInput(player_name)下拉菜单中选择一个播放器的名称,然后您可以从selectInputs(season)和radioButtons中选择其他过滤器( contested)显示一个表格。如果不使用提交或操作按钮,是否仍然会发现player_name值已更改,然后会将seasoncontested输入重置为默认值?通常情况下,我会使用conditionalPanel进行输入更改,但在这种情况下,player_name有数千名玩家。

1 个答案:

答案 0 :(得分:1)

这个的基本解决方案是使用额外的uiOutput" filters_UI"放置第二个selectInput和radioButtons并向其添加input$player_name,因此每当玩家名称更改时,将使用初始值再次创建完整的uiOutput。请参阅下面的示例。

library(shiny)

ui <- fluidPage(
  fluidRow(
    column(6, uiOutput('controls_UI')),
    column(6, DT::dataTableOutput("table_DT"))
  )
)

server <- shinyServer(function(input, output, session) {
  # to simulate players
  dat <- USJudgeRatings
  players <- row.names(dat)
  seasons <-  names(dat)

  output$controls_UI <- renderUI({
    fluidRow(
      column(6, 
        selectInput("player_name", "Players", players, selected = players[1])
      ),
      column(6, 
        uiOutput("filters_UI"))
    )
  })

  # render again if input$player_name changes
  output$filters_UI <- renderUI({
    input$player_name
    fluidRow(
      column(6, 
        selectInput("season", "Season", seasons, selected = seasons[1])
      ),
      column(6, 
        radioButtons("contested", label = "Contested",
          choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
          selected = 1)
      )
    )
  })

  output$table_DT <- DT::renderDataTable(
    datR(),
    options = list(
      scrollX = TRUE, 
      paging = TRUE,
      lengthMenu = c(10, 20, 50)
    ),
    selection = "single",
    rownames = TRUE
  )

  # creates a table based on the sliders and radio buttons
  datR <- reactive({
    input$player_name
    varName <- input$season
    contested <- ifelse(is.null(input$contested),1,as.integer(input$contested))
    dat0 <- data.frame( dat[sample(2:length(players),contested*3), varName] )   
    names(dat0) <- varName
    dat0
  })

})

shinyApp(ui, server)

我希望它可以帮到你。