Hide shiny output - r

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

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

Related

Set an Output Component to Empty in R/Shiny

I have uiOutput and plotOutput components in my main Shiny panel.
plotOutput("plot_data"),
uiOutput("summary_data")
I have the typical code in the server function to react and populate each component, for example:
output$plot_data <- renderPlot({
hist(data_vars())
})
output$summary_data <- renderPrint({
summary(data_vars())
})
I'd like to add functionality to each that sets the output component of the other to NULL or an empty string, etc. so that these two outputs share the same space. When one has data, the other is empty. I don't think it would work this way, but it could look like this:
output$plot_data <- renderPlot({
# Code to "flatten" uiOutput
# Then populate the component
hist(data_vars())
})
output$summary_data <- renderPrint({
# Code to "flatten" plotOutput
# Then populate the component
summary(data_vars())
})
I think this might be done using observeEvent, but I haven't found a way to completely remove content from one so that the other could take up the same space on the page. Please help. Thank you.
Rather than having a separate plotOutput and printOutput, you can have just one uiOutput and then you can add code in the server to show which output you would like in that slot. Here's a working example where I added a button to swap between views.
library(shiny)
ui <- fluidPage(
actionButton("swap","Swap"),
uiOutput("showPart")
)
server <- function(input, output, session) {
showState <- reactiveVal(TRUE)
observeEvent(input$swap, {showState(!showState())})
output$plot_data <- renderPlot({
hist(mtcars$mpg)
})
output$summary_data <- renderPrint({
summary(mtcars)
})
output$showPart <- renderUI({
if (showState()) {
plotOutput("plot_data")
} else {
verbatimTextOutput("summary_data")
}
})
}
shinyApp(ui, server)
Using this method only one of the two output will be rendered in the uiOutput slot.

RShiny static text and textOutput on the same line

I've put together a simple RShiny app related to finance and trading.
https://obrienciaran.shinyapps.io/cfd_new_value/
This is likely a very basic question, but right now in the side bar you can see text that says 'Current margin:' with a percent under it. Obtained via:
tags$b("Current margin:"),
h5(textOutput("current_margin"))
I just want these outputs side by side so it is displayed as
'Current margin: x%'
and not
'Current Margin:'
'x%'
Use the argument inline=TRUE and put the text "Current margin" and the output in the same div, here it is h5.
library(shiny)
ui <-
fluidPage(
h5(tags$b("Current margin:"),textOutput("current_margin", inline = TRUE))
)
server <- function(input, output, session) {
output$current_margin <- renderText({
"10%"
})
}
shinyApp(ui, server)

How to disable button based on a condition in a R Markdown document with shiny elements?

Suppose we have a group of 8 checkboxes (8 letters) and an action button which prints the label of all the selected checkboxes. What I want to do, enable and disable the state of the action button based on a condition. The condition is that if the number of selected checkboxes are between 2 and 5, then the button should be enabled, else disabled. For changing the state of the button I want to use the functions enable, disable or toggleState functions from the shinyjs package. And when the button is enabled, I will be able to trigger an event to print the numbers of selected items.
Here is what I tried until now:
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
if(result()>1 & result()<=5)
enable("action")
else
disable("action")
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
```
Took me awhile to find it, but you have to enable shinyjs to use R-markdown explicitly, it needs to setup its javascript differently in this case.
You do this by calling: useShinyjs(rmd=T) in the chunk where you are using it.
---
title: "Disable Button"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shinyjs)
library(shiny)
```
```{r, echo=FALSE}
useShinyjs(rmd=T)
checkboxGroupInput("param_group", label = h3("Letters"),
choices = LETTERS[1:8])
actionButton('action', "Print")
result<-reactive({
length(input$param_group)
})
observe({
useShinyjs()
if(result()>1 & result()<=5){
enable("action")
} else {
disable("action")
}
})
txt<-eventReactive(input$action,{
cat("Number of letters selected: ",length(input$param_group))
})
renderPrint({
txt()
})
Screen shot:

How to make shinyAce app container background transparent

I created a shiny presentation using rmarkdown in which one of the slides includes a shinyAce editor (code below). The entire app seems to be inside a container which has a white background. I want to make the background of this container transparent using css, but has thus far I've been unable to find the right object name to apply the change to.
I've tried a simple par(bg = NA) and tooled around in the default css files in the rmarkdown, shiny, and shinyAce packages...but cant find the appropriate place to make the change.
Finally, I've included code in mainPanel to change the color of the background. However, this just seems to be an overlay on the white container because when I set body {background-color: transparent} the background remains white.
As always, any help is appreciated
---
title : "White Backgrounds are Ugly"
subtitle : "Help me make it go away"
author : "Me"
date : "Now"
output:
ioslides_presentation:
widescreen : true
runtime: shiny
---
## Basic R Plot with shinyAce
```{r, echo=FALSE}
library(shinyAce)
shinyApp(
shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
aceEditor("code", mode="r",
value="plot(1:5, 1:5,\nxlab = 'This is side 1',\nylab = 'This is side 2',\nmain = 'This is side 3',\nsub = 'Subtitle or Caption',\naxes = TRUE,\ntype = 'p',\nlas = 0)"),
actionButton("eval", "Evaluate")),
mainPanel(list(tags$head(tags$style("body {background-color: rgba(255,130,255,1); }"))), plotOutput("output"))))),
shinyServer(function(input, output, session) {
output$output <- renderPlot({
par(bg = NA)
input$eval
return(isolate(eval(parse(text=input$code))))
})
})
)
```

Clickable links in Shiny Datatable

I created a table containing some HTML links using Shiny's renderDataTable. The links are not clickable, though, instead they render literally:
https://samizdat.shinyapps.io/zakazky/
Do you have any idea what could be wrong? It worked fine before upgrading Shiny to the version 0.11... Thanks!
I had the same problem. The escape = FALSE option for renderDataTable solved it, as you mentioned in the comments.
Here is complete code for an app with a table that has links.
If you are doing this, you will want each link to be unique based on a value in the table. I move this code into a function so its cleaner.
#app.R#
library(shiny)
createLink <- function(val) {
sprintf('Info',val)
}
ui <- fluidPage(
titlePanel("Table with Links!"),
sidebarLayout(
sidebarPanel(
h4("Click the link in the table to see
a google search for the car.")
),
mainPanel(
dataTableOutput('table1')
)
)
)
server <- function(input, output) {
output$table1 <- renderDataTable({
my_table <- cbind(rownames(mtcars), mtcars)
colnames(my_table)[1] <- 'car'
my_table$link <- createLink(my_table$car)
return(my_table)
}, escape = FALSE)
}
shinyApp(ui, server)

Resources