I am looking at using Shiny widgets in an R-markdown file. I find this example, ran it in markdown, and then saved it as Html. However when I load it, it renders the widgets twice, and then hangs.
Is there a way I can avoid this, or is it a bug/known limitation in the system?
Here is the code:
---
runtime: shiny
output: html_document
---
### Here are two Shiny widgets
```{r echo = FALSE}
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20)
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
```
### ...that build a histogram.
```{r echo = FALSE}
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
It works fine when I compile it, but when I save it and then reload it into Chrome, IE, Edge, etc... I get this:
Related
Say we are working with an rmarkdown document with the following yaml (example runtime: shiny "app"):
---
runtime: shiny
output: html_document
---
### Here are two Shiny widgets
```{r echo = FALSE}
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20)
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
```
### ...that build a histogram.
```{r echo = FALSE}
renderPlot({
hist(faithful$eruptions, probability = TRUE,
breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)",
main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
With a regular shiny app we can set the host and port by setting options: shinyApp(ui, server, options = list(host = '123.45.67.89', port = 1234))
Is it possible to establish and set a specific host and port in the YAML? Or somewhere else in the setup?
do this
option1
rmarkdown::run("test.Rmd", shiny_args = list(host = '123.45.67.89', port = 1234))
test.Rmd is the name of your doc. make sure your host 123.45.67.89 is valid. Usually is 127.0.0.1 for local testing or don't provide it to use the default.
option 2
options(shiny.port = 1234)
options(shiny.host = '127.0.0.1')
rmarkdown::run("test.Rmd")
I am trying to create an rmd html document which uses code folding as well as shiny embedding. I have tried to do this using the default shiny rmd doc but adding in code_folding: hide:
---
title: "Untitled"
author: "Author"
date: "3/29/2019"
output:
html_document:
code_folding: hide
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Inputs and Outputs
You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot.
```{r eruptions}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Which gives:
So as you can see, the code is not folded/hidden. I have successfully implemented each of these individually, but how can they both be used in the same document? I think they clash somehow - does anyone know of a workaround?
Thanks!
I am a little lost and am unable to add a reactive test (te) in the shiny output of an R markdown document. A minimal example based on an R studio example is paste below.
Many thanks in advance!
Jean-Pierre
---
title: "Untitled"
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r eruptions, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderText({te})
renderPlot({
startTime <- Sys.time()
# additional code goes here
endTime <- Sys.time() +1
te <- reactive(startTime - endTime)
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
I think you should use te <<- reactive(startTime - endTime) to define te outside the renderPlot, use renderText({te()}) instead of renderText({te}) because it is a reactive expression, and finally put renderText({te()}) to the end after it's definition.
Trying to figure out how to have a full size shiny app in an ioslides markdown. I have something almost working, but it is a bit ad-hoc. I am also not confident it will reproduce when I show the presentation on a larger resolution screen (as I use px in the div)?:
---
title: "My Title"
author: "My Name"
date: "29 March 2016"
runtime: shiny
output:
ioslides_presentation
---
##
<div style="margin-left:-50px; margin-top:-50px; width:80%; height:100%">
```{r, echo=FALSE, message=FALSE, fig.width=8}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
</div>
giving:
which kind of uses up some of the large margin space (as desired) but still does not cover the whole slide.
Try playing around with the values in your div<>. You can go over 100% and also set the size in pixels, e.g. <div style="margin-left:-120px; margin-top:-50px; width:900px">
BACKGOUND:
You can "ask" RStudio to generate an example R Markdown Shiny document, which contains this sample code:
## Inputs and Outputs
You can embed Shiny inputs and outputs in your document.
Outputs are automatically updated whenever inputs
change. This demonstrates how a standard R plot can be
made interactive by wrapping it in the Shiny
`renderPlot` function. The `selectInput` and
`sliderInput` functions create the input widgets used to
drive the plot.
```{r, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Note that this example does not make use of a folder containing ui.R and server.R.
PROBLEM:
If you copy this multiple times, the first one works as expected, and the later ones get displayed as well, but do not react to changes in the input parameters.
QUESTION:
How can you create an R Markdown document with multiple embedded plots like the above (without using external folders with ui.R and server.R), but ensuring that each one works interactively?
You must give different ids to your input elements, something like that :
First embedded shiny plot :
```{r}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Second embedded shiny plot :
```{r}
inputPanel(
selectInput("n_breaks2", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust2", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks2),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust2)
lines(dens, col = "blue")
})
```
As described for example in the RStudio Shiny Tutorial, the first parameter to widget functions is the widget name, which identifies the widget. Multiple widgets with the same name will not each be usable, which is why simply creating two copies of the example does not create two working copies.
To make it work, you must make the widget names unique in each inputPanel call, and then use this names in the renderPlot calls.