Shiny Document: how to center align output of RenderPlot - r

I am creating a Shiny Document in R and want the plot generated with Shiny RenderPlot to be centered, or right, or height = 50%. However, the fig.align=center or right or out-width = "50%" in the r chunk does not affect the output (I suppose it is for figures generated directly in R, not via Shiny). How to center?
Try to change out.width, or fig.align, and it won't change a thing.
--
title: "Untitled"
author: "Gahis"
date: "8/4/2021"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r eruptions, echo=FALSE, fig.align='right', out.width="50%"}
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")
})
```
Thanks!

Here is how you can reduce the width of the plot and centering it. Add this CSS:
<style>
.center50 {
margin: auto;
width: 50%;
}
</style>
Then include you renderPlot in a div with the class center50:
---
title: "Test"
output: html_document
runtime: shiny
---
<style>
.center50 {
margin: auto;
width: 50%;
}
</style>
```{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)
)
```
```{r}
div(class = "center50",
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")
})
)
```

Related

Darkly Theme RMarkdown

I am having problems setting a Darkly theme to an RMarkdown document. I am getting a light greenish background instead. Is there a solution for it? See below
---
title: "Untitled"
author:
date: "July 29, 2020"
output:
flexdashboard::flex_dashboard:
theme: darkly
runtime: shiny
---
```{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)
)
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")
})
```

Interpreting the 'eruptions' variable option for knitr chunk in RMarkdown

I have the following knitr chunk obtained from RStudio base Rmarkdown-Shiny runtime template:
```{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)
)
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")
})
```
What is the use of eruptions option in {r eruptions, echo=FALSE}
It is the name of the chunk.
You cannot give same name to different chunks.

Include reactive text in a R markdown shiny documents

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.

Full size shiny app in an ioslides markdown presentation slide

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">

Reloading Rmd Html Document with Shiny Widets

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:

Resources