【问题标题】:Hiding or showing shiny elements without pressing submit隐藏或显示闪亮的元素而不按提交
【发布时间】:2017-04-04 16:14:20
【问题描述】:

我有一个闪亮的应用程序,我想根据用户输入隐藏或显示一些元素。我试图通过在闪亮中使用conditionalPanel 来做到这一点。但是,它只有在按下提交按钮后才能工作。我想隐藏或显示textInput 元素而不按提交按钮。下面是我尝试过的一个例子。

UI.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("submitButton example"),
  fluidRow(
    column(3, wellPanel(
      sliderInput("n", "N:", min = 10, max = 1000, value = 200,
                  step = 10),
      checkboxInput("checkbox", label = "Message", value = FALSE),
      conditionalPanel(
        condition = "input.checkbox == true",
        textInput("text", "Text:", "text here")),

      submitButton("Submit")
    )),
    column(6,
           plotOutput("plot1", width = 400, height = 300),
           verbatimTextOutput("text")
    )
  )
))

服务器.R

shinyServer(function(input, output) {
  output$plot1 <- renderPlot({
    hist(rnorm(input$n))
  })

  output$text <- renderText({
    paste("Input text is:", input$text)
  })
})

我想在用户选中 checkbox 后立即显示 textInput 并在取消选中时隐藏它,而不依赖于 submit 按钮。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以试试

    用户界面:

    library(shiny)
    shinyUI(fluidPage(
      titlePanel("submitButton example"),
      fluidRow(
        column(3, wellPanel(
          sliderInput("n", "N:", min = 10, max = 1000, value = 200,
                      step = 10),
          checkboxInput("checkbox_1", label = "Message", value = FALSE),
         uiOutput('test')
    
          ,actionButton("Submit",label ="Submit" )
        )),
        column(6,
               plotOutput("plot1", width = 400, height = 300),
               verbatimTextOutput("text")
        )
      )
    ))
    

    服务器:

    shinyServer(function(input, output,session) {
      output$test=renderUI({ 
        if(input$checkbox_1==T){
          list(textInput("text", "Text:", "text here"),
               numericInput("num","num",0), numericInput("num1","num1",0))}
      })
    
      observeEvent(input$Submit,{
        output$plot1 <- renderPlot({
          hist(rnorm(isolate(input$n)))
        })
        output$text <- renderText({
          paste("Input text is:", isolate(input$text))
        })
    
      })
    
    })
    

    【讨论】:

    • 感谢您的回答。 textInput 中的消息应仅在按下提交按钮后反映。在您的回答中,它不依赖于submit ,而是立即出现。
    • 不完全明白你想要什么。您希望仅在提交后更改 hist 和 verbatimTextOutput("text") 吗?
    • 是的,没错,应该在按下提交后出现
    • 编辑我的答案,你想要什么? (您在第一次单击提交删除隔离后使文本和绘图反应。
    • 我只能尝试)您不能在一个应用程序中使用提交按钮和反应式(我的想法),但更灵活的是actionButton + observeEvent + isolate .. 它为您提供反应性部分应用程序并隔离你想要的(在你的例子中它的输入在output$plot1output$text中使用)
    猜你喜欢
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2017-03-06
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多