【问题标题】:Shiny App - Saving an input entry as variable and use it in the codeShiny App - 将输入条目保存为变量并在代码中使用它
【发布时间】:2020-03-06 05:22:00
【问题描述】:

我正在开发 Shiny App,我需要将数字输入 ("num") 保存为变量并在 ifelse 语句中使用它。

例如,我尝试在服务器函数中执行myvalue <- input$num,但它不起作用。

我需要至少使用其他 4 或 5 个输入来执行此操作。

有人有想法吗?

【问题讨论】:

    标签: shiny shinydashboard shiny-server shinyapps


    【解决方案1】:

    选项 1:在任何你想使用 myvalue 的地方使用 input$num。这不是问题。

    选项 2:使用reactive()。注意括号myvalue(),像调用函数一样调用它。 (这一个函数)。

    library(shiny)
    
    ui <- fluidPage(
      numericInput("num", "Number", 0),
      textOutput("positive"),
      textOutput("odd")
    )
    
    server <- function(input, output, session) {
    
      # Option 1
      output$positive <- renderText({
        if (input$num >= 0) {
          "Positive number."
        } else {
          "Negative number."
        }
      })
    
      # Option 2
      myvalue <- reactive({
        input$num
      })
    
      output$odd <- renderText({
        if (myvalue() %% 1 != 0) {
          "Not an integer."
        } else if (myvalue() %% 2 == 0) {
          "Even number."
        } else {
          "Odd number."
        }
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢。我有 5 个输入,需要将它们作为参数插入到我创建的函数中。然后打印函数的结果。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多