隐藏闪亮的输出 [英] Hide shiny output

查看:72
本文介绍了隐藏闪亮的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何隐藏渲染的 shiny 输出?具体来说,我有一些由 shiny 生成的数字/表格,我有一个按钮,点击时应该隐藏数字/表格,再次点击时应该显示它们.

How do you hide rendered shiny output? Specifically, I have some figures/tables generated by shiny and I have a button, that when clicked should hide the figures/tables, and when clicked again should show them.

这是我到目前为止所拥有的(如下),它有点工作,但是它应该隐藏 renderPlot 输出的地方,文档中有一个很大的空白区域,我正在尝试走开.

This is what I have so far (below), and it works somewhat, but where it's supposed to hide the renderPlot output, there is a big blank space in the document that I am trying to make go away.

应该可以将此代码复制并粘贴到 Rstudio 中并点击运行文档(它是带有闪亮运行时的 rmarkdown).

It should be possible to just copy and paste this code into Rstudio and hit run document (it's rmarkdown with shiny runtime).

---
runtime: shiny
---

```{r, echo=F}
actionButton("hide", "Hide")
dat <- data.frame(a=1:10, b=rexp(10, 1/10), c=letters[sample(1:24, 10)])
renderTable({
    if (input$hide %% 2 == 1)
        dat
})

```
lodi dodi

```{r, echo=F}
renderPlot({
    if (input$hide %% 2 == 1)
        plot(b ~ a, data=dat)
})

```
this text is separated by blank space, but it shouldn't be

推荐答案

您可以使用 shinyjs 包通过 hide() 函数隐藏元素(或使用toggle() 函数在隐藏和显示之间交替).免责声明:我写了那个包.

You can use the shinyjs package to hide elements with the hide() function (or use the toggle() function to alternate between hiding and showing). Disclaimer: I wrote that package.

我以前从未在 rmarkdown 中使用过它,所以我将展示如何在普通的闪亮应用程序中使用它,并使用 shinyApp() 函数来包含一个完整的闪亮应用程序rmarkdown 中的应用程序.您可以在此处阅读有关如何在 rmarkdown 文档中包含闪亮应用的信息.

I've never used it in an rmarkdown before, so I'm just going to show how to use it in a normal shiny app and use the shinyApp() function to include a full shiny app inside an rmarkdown. You can read here about how to include shiny apps inside an rmarkdown doc.

---
runtime: shiny
---

```{r, echo=F}
suppressPackageStartupMessages(
  library(shinyjs)
)

dat <- data.frame(a=1:10, b=rexp(10, 1/10), c=letters[sample(1:24, 10)])

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    actionButton("hide", "Hide"),
    p("Text above plot"),
    plotOutput("plot"),
    p("Text below plot")
  ),
  server = function(input, output, session) {
    output$plot <- renderPlot({
      plot(b ~ a, data=dat)
    })

    observeEvent(input$hide, {
      hide("plot")
      # toggle("plot") if you want to alternate between hiding and showing
    })
  },
  options = list(height = 700)
)
```

为了能够使用 hide,我必须:

In order to be able to use hide, I had to:

  • 安装和加​​载 shinyjs
  • 在 UI 中添加对 useShinyjs() 的调用
  • 在要隐藏/显示的元素上调用 hidetoggle
  • install and load shinyjs
  • add a call to useShinyjs() in the UI
  • call hide or toggle on the element that you want to hide/show

希望能帮到你

这篇关于隐藏闪亮的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆