【问题标题】:How to use shiny actionButton to show & hide text output?如何使用闪亮的 actionButton 显示和隐藏文本输出?
【发布时间】:2017-06-16 18:22:34
【问题描述】:

我正在尝试使用actionButton 编写一个简单的闪亮应用程序。当actionButton被按下时,一些文本输出应该直接打印在下面。下面的代码让我找到了解决方案:

shinyApp(
  ui = shinyUI( fluidPage(
    actionButton("button", "don't press the button"),
    verbatimTextOutput("text")
  )
  ),
  server = function(input, output, session){
    observeEvent(input$button, {
      output$text <- renderText({"ahh you pressed it"})
    })
  }
)

有两点我想更改,但不确定如何更改:

1) 上面的代码在按下按钮之前显示了一个空的灰色框 - 我希望在按下按钮之前没有任何内容。看起来conditionalPanel 可能是正确的方法,但不确定如何实现。

2) 上面的代码是否可以修改,以便在第二次按下按钮后,再次隐藏文本输出?

【问题讨论】:

  • 1) 我无法确认 - 这里没有灰色框。 2)可以使用shinyjs包,在fluidPage中包含useShinyjs(),设置文本为hidden(verbatimTextOutput("text"))并在ovbserver中添加`toggle("text")`,使得文本在每次点击时切换。

标签: r shiny


【解决方案1】:

你可以用shinyjshiddentoggle试试这个

library(shiny)
library(shinyjs)

shinyApp(
  ui = shinyUI(fluidPage(useShinyjs(), 
    actionButton("button", "don't press the button"),
    hidden(
      div(id='text_div',
        verbatimTextOutput("text")
        )
    )
  )
  ),
  server = function(input, output, session){
    observeEvent(input$button, {
      toggle('text_div')
      output$text <- renderText({"ahh you pressed it"})
    })

  }
)

【讨论】:

    猜你喜欢
    • 2015-08-17
    • 2017-04-01
    • 1970-01-01
    • 2020-12-13
    • 2022-01-16
    • 1970-01-01
    • 2020-01-18
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多