5

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.

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.

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
user12912834
  • 422
  • 1
  • 4
  • 9
  • It has to be in rmarkdown, it can't be just a shiny app? If it's a shiny app I can show you a one word solution, but I don't know how to do it in a rmarkdown – DeanAttali Jun 03 '15 at 04:23
  • @daattali either way is fine (hopefully), most things are interchangeable, I looked at your package, but couldn't come up with a solution – user12912834 Jun 03 '15 at 04:24
  • Well the solution I have doesn't work in rmarkdowns that's why I asked – DeanAttali Jun 03 '15 at 04:24
  • @daattali thats a bummer, but I would still be interested to see a solution for regular shiny, or even just the name of an appropriate function – user12912834 Jun 03 '15 at 04:25
  • Ok, I realized you can just include a normal shiny app in an rmarkdown so np – DeanAttali Jun 03 '15 at 04:39

1 Answers1

16

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.

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)
)
```

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

  • 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

I hope this helps

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • This is really cool, it does exactly what I want. Thanks a lot! It would be nice to be able to use this without wrapping it in all the extra code (ie. `shinyApp(...)`. Is that definetly not going to work with `toggle`? – user12912834 Jun 03 '15 at 04:55
  • Any function in shinyjs requires the call to `useShinyjs` that initializes the required JavaScript. I never really had rmarkdown in mind when developing it, I only thought about shiny apps. I don't really understand/know much about how shiny and rmarkdown play together so I don't know how to change the function so that it'd be able to be used inside an rmarkdown, sorry :/ – DeanAttali Jun 03 '15 at 05:00
  • @DeanAttali Thanks for your great work. To make a Hide/Show button in Rmd documents you can surround the code block with the following:
    ```{r} #stuff in block ```
    – Matt L. Sep 13 '17 at 18:55
  • That and a few other solutions for Rmd are in this SO question: https://stackoverflow.com/questions/42630248/collapsing-hiding-figures-in-r-markdown/46125161#46125161 – Matt L. Sep 13 '17 at 18:57
  • @MattL. if your rmd file uses the shiny engine to include shiny components, then you can use shinyjs http://deanattali.com/shinyjs/advanced#usage-interactive – DeanAttali Sep 14 '17 at 16:19