为什么第一个下拉列表在更改时会重置第二个下拉列表

时间:2017-11-26 04:31:02

标签: r shiny

这是我编写的代码的一部分,我遇到的问题是在选择播放器1和播放器之后,如果我想再次更换播放器1,播放器2也会重置。我必须让Player 2的输入依赖于Player1,因为我不希望用户能够为这两个下拉菜单选择相同的播放器。这是我的代码:

server.R

shinyServer <- function(input, output) {

  # Creates the first drop down menu through which the user can select the first player.
  output$firstdropdown <- renderUI({
    selectizeInput("player1", label = "Choose Player 1:", choices = winrateRole(input$role)$player,
                   selected = NULL, multiple = FALSE, options = NULL)
  })

  # Creates the second drop down menu. User cannot select the same player he/she select as the first player.
  output$seconddropdown <- renderUI({
    all.choices <- winrateRole(input$role)$player
    without.player1 <- all.choices[which(all.choices != input$player1)]
    selectizeInput("player2", label = "Choose Player 2:", choices = without.player1,
                   selected = NULL, multiple = FALSE, options = NULL)
  })

ui.R

shinyUI <- fluidPage(

         navbarPage(strong("Test1"),
           tabPanel("Overview"),
           tabPanel("Tab1",
             sidebarLayout(
               sidebarPanel(
                 uiOutput("firstdropdown"),
                 br(),
                 uiOutput("seconddropdown")
               )
    ))
)

winrateRole

winrateRole <- function(role) {
  blue.role <- paste0("blue", role)
  blue <- league.data %>%
    group_by_(blue.role) %>%
    summarise(winrate.blue = round((sum(bResult) / n() * 100), digits = 2)) %>%
    arrange_(blue.role) %>%
    select(player = blue.role, winrate.blue)

  red.role <- paste0("red", role)
  red <- league.data %>%
    group_by_(red.role) %>%
    summarise(winrate.red = round((sum(rResult) / n() * 100), digits = 2)) %>%
    arrange_(red.role) %>%
    select(player = red.role, winrate.red)
  return (left_join(blue, red))
}

1 个答案:

答案 0 :(得分:0)

您的问题是,每次第一次选择的值发生更改时,您都会重新创建整个输入。

在这种情况下,您希望使用updateInput方法,该方法仅更改所需的值,但保持其余值不变。不幸的是,每次更改选择时,updateSelectizeInput似乎都会重置,因此您需要将输入值保存在反应变量中。

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

  current_selection <- reactiveVal(NULL)
  observeEvent(input$player2, {
    current_selection(input$player2)
  })
  # update player 2
  observeEvent({
    input$player1
  },{
    all.choices <- winrateRole
    without.player1 <- all.choices[which(all.choices != input$player1)]
    updateSelectInput(
      inputId = "player2",
      session = session,
      selected = current_selection(),
      choices = without.player1
    )
  },
  ignoreInit = FALSE
  )
  output$firstdropdown <- renderUI({ 
    selectizeInput(
      "player1", 
      label = 
        "Choose Player 1:", 
      choices = winrateRole, 
      selected = NULL, 
      multiple = FALSE,
      options = NULL) })
  # Creates the second drop down menu. User cannot select the same player he/she select as the first player. 
  output$seconddropdown <- renderUI({ 
    selectizeInput(
      "player2", 
      label = "Choose Player 2:", 
      choices = winrateRole, 
      selected = NULL, 
      multiple = FALSE,
      options = NULL)})
}

** ui.R **

  shinyUI <- fluidPage(


navbarPage(strong("Test1"),
           tabPanel("Overview"),
           tabPanel("Tab1",
                    sidebarLayout(
                      sidebarPanel(
                        uiOutput("firstdropdown"), 
                        br(), 
                        uiOutput("seconddropdown")
                    ))
)