I am in the process of developing interactive reports with Rmd files, which specify a shiny runtime. I have, however, run into the following issue.
When plotlyfying ggplots that have larger height specifications, they start overlaying other aspects of the html output (e.g. in the exaple here its the text). So far my research has yielded that a fixed setting of height == 400px
in the poltly container is responsible for this.
---
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(tidyverse)
library(shiny)
library(plotly)
```
```{r}
fluidRow(
column(3,
selectInput("myselectorID",
"Select:",
selected = "setosa",
choices = c("setosa", "versicolor", "virginica")))
)
renderPlotly({
data <- iris %>% filter(Species == input[["myselectorID"]])
ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width))
geom_point()
ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END
If I supply the following css
(e.g. via the yaml header) then the problem is supposedly solved:
.plotly {
height: 100% !important;
}
I would prefer a solution where I pass the parameter to R functions though. Any ideas? Thank you!
CodePudding user response:
Someone answered this and deleted their answer which was working... Not sure why but here's what they posted for the record since it seemed to do the trick:
---
runtime: shiny
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(tidyverse)
library(shiny)
library(plotly)
```
```{r}
fluidRow(
column(3,
selectInput("myselectorID",
"Select:",
selected = "setosa",
choices = c("setosa", "versicolor", "virginica")))
)
# create a specific plotly output with the parameter set as desired
plotlyOutput("testplot", height = "100%")
# address the plotly output
output$testplot <- renderPlotly({
data <- iris %>% filter(Species == input[["myselectorID"]])
ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width))
geom_point()
ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END
CodePudding user response:
To expand on this solution, you can programmatically insert ggplotly
outputs of specified heights like this:
- set one ore more ids at which to place the plot output with
insertUI
later:
uiOutput('some_plots')
- render the output and insert it at the specified output id; specify plot height as an argument to
plotlyOutput()
:
Example (inserting three ggplotly outputs of different heights at id "some_plots"):
plot_heights = c(200, 150, 350)
for(plot_index in 1:3){
plot_name = paste0('plot_', plot_index)
my_plot <- qplot(data = iris,
x = Sepal.Length, y = Sepal.Width,
geom = 'point') %>% ggplotly()
output[[plot_name]] <- renderPlotly(my_plot) ## add to output list
insertUI(selector = "#some_plots",
where = "afterEnd",
ui = plotlyOutput(plot_name,
## specify plot height:
height = paste0(plot_heights[plot_index],'px')
)
)
}