【问题标题】:shiny updateSelectizeInput return wrong value闪亮的 updateSelectizeInput 返回错误的值
【发布时间】:2019-08-22 01:53:20
【问题描述】:

我在下面的代码中有 selectizeInput 中的依赖项。
我正在尝试更新两个输入,但出现问题并且只更新第一个值
有任何想法吗?
谢谢帮助

library(shiny)
library(DT)
    ui <- navbarPage(
      title = "Interaction with Table Cells", id = "x0",

      tabPanel(
        "Table", DT::dataTableOutput("x1"),
        selectizeInput("s1", "speed", choices = cars %>% pull(speed) %>% unique()),
        uiOutput("s2")
      )
    )
    server <- function(session, input, output) {
      # add CSS style 'cursor: pointer' to the 0-th column (i.e. row names)
      output$x1 <- DT::renderDataTable({
        datatable(
          cars,
          selection = "none", class = "cell-border strip hover"
        ) %>% formatStyle(0, cursor = "pointer")
      })

      output$s2 <- renderUI({
        selectizeInput("s2", "dist", choices = cars %>% filter(speed == input$s1) %>%
          pull(dist) %>% unique())
      })

      observeEvent(input$x1_cell_clicked, {
        info <- input$x1_cell_clicked
        # do nothing if not clicked yet, or the clicked cell is not in the 1st column
        if (is.null(info$value) || info$col != 0) {
          return()
        }

        updateSelectizeInput(session, "s1", selected = cars[info$row, "speed"])
        updateSelectizeInput(session, "s2", selected = cars[info$row, "dist"])
      })
    }

    shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你对 Shiny 的反应有疑问。

    当您说只有第一个值被更新时,这是不正确的。它有点相反。 s2 按以下顺序更新两次:

    第一个

    之后
    updateSelectizeInput(session, "s2", selected = cars[info$row, "dist"])
    

    被调用。

    第二,因为input$s2input$s1更新后依赖于input$s1的值。

    第二次 s2 仅依赖于当前 s1 可用的所有选项。因此会显示第一个值,而不是强制调用 updateSelectizeInput(s2...) 时选择的值。

    为了克服这个问题,您可以创建一个反应变量values$s2_selected,它存储 s2 的指定值。我们去掉第二个updateSelectize,只使用s1的变化引起的s2的重新验证。在这样做的同时,我们使用存储的指定选择作为我们为 s2 选择的选项。

    示例代码:

    library(shiny)
    library(DT)
    library(dplyr)
    ui <- navbarPage(
      title = "Interaction with Table Cells", id = "x0",
    
      tabPanel(
        "Table", DT::dataTableOutput("x1"),
        selectizeInput("s1", "speed", choices = cars %>% pull(speed) %>% unique()),
        uiOutput("s2")
      )
    )
    server <- function(session, input, output) {
      values<-reactiveValues()
      values$s2_selected<-""
      # add CSS style 'cursor: pointer' to the 0-th column (i.e. row names)
      output$x1 <- DT::renderDataTable({
        datatable(
          cars,
          selection = "none", class = "cell-border strip hover"
        ) %>% formatStyle(0, cursor = "pointer")
      })
    
      output$s2 <- renderUI({
        choices<-cars %>% filter(speed == input$s1) %>%
          pull(dist) %>% unique()
        if(isolate(values$s2_selected)%in%choices){
          selected=isolate(values$s2_selected)
        }
        else{
          selected<-choices[1]
        }
        selectizeInput("s2", "dist", choices = choices,selected=selected)
      })
    
      observeEvent(input$x1_cell_clicked, {
        info <- input$x1_cell_clicked
        # do nothing if not clicked yet, or the clicked cell is not in the 1st column
        if (is.null(info$value) || info$col != 0) {
          return()
        }
        values$s2_selected<-cars[info$row, "dist"]
        updateSelectizeInput(session, "s1", selected = cars[info$row, "speed"])
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      相关资源
      最近更新 更多