Hide the brilliant exit
How do you hide the conclusion shiny
? Specifically, I have some numbers / tables generated with shiny
and I have a button that, when clicked, should hide the numbers / tables, and when clicked again, they should show them.
This is what I have so far (below) and it works somewhat, but where the output is supposed to be hidden renderPlot
, there is a large white space in the document that I am trying to get away.
It should be possible to just copy and paste this code into Rstudio and hit run run document (it's rmarkdown with a brilliant 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
source to share
You can use a package shinyjs
to hide items using a function hide()
(or use a function toggle()
to toggle between hiding and showing). Disclaimer: I wrote this package.
I've never used it in rmarkdown before, so I'll just show you how to use it in a regular shiny app and use the feature shinyApp()
to include a full shiny app in rmarkdown. You can read here on how to include shiny apps in your 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)
)
```
To be able to use hide, I had to:
- install and download
shinyjs
- add a call
useShinyjs()
to the UI - call
hide
ortoggle
element you want to hide / show
I hope this helps
source to share