【问题标题】:Issues accessing Inputs from renderUI in R Shiny在 R Shiny 中从 renderUI 访问输入的问题
【发布时间】:2017-07-11 17:00:22
【问题描述】:

我是 Shiny 的新手,但我正在开发一个需要在 UI 和 shinyServer 之间来回切换的应用程序。为此,我已经熟悉了动态 renderUI 函数。但是,我在正确处理通过 renderUI 函数发送到 UI 的输入时遇到了一些问题。

我已经编写了一个关于我的问题的快速玩具示例。

library(shiny)

ui <- fluidPage(
numericInput("cat.count", "How many cats do you have?", min = 0, value = 0),
checkboxInput('pet.check',"Do you other pets?", value = FALSE),
uiOutput("dog.input"),
uiOutput("dog.num")
)

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

output$dog.input <- renderUI({
if(input$pet.check){
    return(checkboxInput("dog.check", "Do you have dogs?", value = FALSE))
}
})
output$dog.num <- renderUI({
if (exists("input$dog.check") & input&dog.check){
    return(numericInput("dog.count", "How many dogs do you have?", min = 1, 
value = 0))
}

})

})

shinyApp(ui = ui,server = server)

一旦这个玩具应用程序正常运行,“最终产品”将是一个简单的条形图,通过 geom_bar 和 ggplot2 图形显示用户拥有的宠物总数。

除了 req() 和 exists() 函数之外,我还研究了很多 reactive() 函数,但到目前为止,我一直在绕圈子。

提前致谢!

【问题讨论】:

    标签: r user-interface ggplot2 shiny


    【解决方案1】:

    这样的作品?

    rm(list = ls())
    library(shiny)
    
    ui <- fluidPage(
      numericInput("cat.count", "How many cats do you have?", min = 0, value = 0),
      checkboxInput('pet.check',"Do you other pets?", value = FALSE),
      uiOutput("dog.input"),
      uiOutput("dog.num")
    )
    
    server <- shinyServer(function(input,output){
    
      output$dog.input <- renderUI({
        if(is.null(input$pet.check)){return()}
        if(input$pet.check){
          return(checkboxInput("dog.check", "Do you have dogs?", value = FALSE))
        }
      })
      output$dog.num <- renderUI({
        if(is.null(input$dog.check)){return()}
        numericInput("dog.count", "How many dogs do you have?", min = 1, value = 0)
    
      })
    
    })
    runApp(list(ui = ui, server = server))
    

    【讨论】:

    • 不是我的想法,但你的代码帮助很大。谢谢,猪排和@Gregor
    【解决方案2】:

    如果你输入类似的语句

    A(x) & B(x)
    

    进入R,即使A(x) 错误,AB 也会得到评估。这就是您的错误的来源。因此,最好使用嵌套的 if 语句,如 Pork Chop 的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2018-08-11
      • 2021-01-15
      相关资源
      最近更新 更多