【问题标题】:R Shiny: How to make the initial value for numericInput dynamic, based on user inputR Shiny:如何根据用户输入使 numericInput 的初始值动态化
【发布时间】:2021-10-03 15:18:24
【问题描述】:

我正在尝试根据用户的输入使 shiny::numericInput()value 参数动态化。

下面的两个代码块都将运行,但它们无法设置动态初始值。

块 1:

idOptions <- c("1","2","3")

ui <- shiny::fluidPage(
  shiny::selectInput(inputId = "idSelection", "Identification: ", idOptions),
  shiny::numericInput("num", "Number associated with id:", value=shiny::verbatimTextOutput("numberOut")),
)

server <- function(input, output) {
  df <- data.frame(id = c("1","2","3"), number = c(100,227,7))
  output$numberOut <- shiny::renderText({ input$idSelection })
}

shiny::shinyApp(ui, server)

块 2:

idOptions <- c("1","2","3")

ui <- shiny::fluidPage(
  shiny::selectInput(inputId = "idSelection", "Identification: ", idOptions),
  shiny::numericInput("num", "Number associated with id:", value=shiny::verbatimTextOutput("numberOut")),
)

server <- function(input, output) {
  df <- data.frame(id = c("1","2","3"), number = c(100,227,7))
  dfReactive <- shiny::reactive({
    dataOut <- df %>%
      dplyr::filter(., id %in% input$idSelection)
    return(dataOut)
  })
  shiny::observe({
    output$numberOut <- shiny::renderText({ 
      return(dfReactive()$number)
    })
  })
}

shiny::shinyApp(ui, server)

我希望shiny::numericInput 的初始值改变如下:

当用户为标识选择“1”时,初始值为100:

当用户选择“2”标识时,初始值为227:

这背后的想法是根据输入的识别向用户建议一个适当的初始值。

我猜问题可能出在shiny::verbatimTextOutput("numberOut"),但我不知道如何渲染一个简单的数值以传递给shiny::numericInputvalue 参数。

有什么想法吗?

非常感谢。

【问题讨论】:

    标签: r dynamic shiny responsive


    【解决方案1】:

    一种方法是使用renderUI 并在服务器端进行计算:

    idOptions <- c("1","2","3")
    
    shiny::shinyApp(
    
      ui = shiny::fluidPage(
        shiny::selectInput(inputId = "idSelection", "Identification: ", idOptions),
        shiny::uiOutput("num")
      ),
      
      server = function(input, output) {
        
        df <- data.frame(id = c("1","2","3"), number = c(100,227,7))
        
        output$num <- shiny::renderUI({
          shiny::numericInput("num",
                              "Number associated with id:",
                              value = df$number[as.numeric(input$idSelection)])
        })
        
    })
    

    替代方法是将输入留在 UI 端并在 observeEvent 中使用 updateNumericInput

    idOptions <- c("1","2","3")
    
    shiny::shinyApp(
      
      ui = shiny::fluidPage(
        
        shiny::selectInput(inputId = "idSelection",
                           "Identification: ",
                           idOptions),
        
        shiny::numericInput("num",
                            "Number associated with id:",
                            value = NULL)
      ),
      
      server = function(input, output, session) {
        
        df <- data.frame(id = c("1","2","3"),
                         number = c(100,227,7))
        
        observeEvent(input$idSelection, {
          updateNumericInput(session,
                             inputId = "num",
                             value = df$number[as.numeric(input$idSelection)])
        })
      })    
    

    【讨论】:

    • @awag1:我还更新了第二种方法。看看什么最适合你。作为避免错误和错误的一般规则,最好避免在 UI 函数中使用输出作为参数。
    • 我已经确认您的两个建议都对我有用;我将很快决定在我的实际应用中使用哪个。再次感谢您的快速工作!
    猜你喜欢
    • 2016-11-09
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2019-05-05
    相关资源
    最近更新 更多