plots not showing in flexdashboard tabs - r

When I render the flexdashboard the plotly chart only shows on the first tab, and not on the remaining tabs. The reproducible example below uses the flexdashboard example for .tabset use. I am running R 3.6.1
I have used plotly and tabset in the past, and I am not sure why it is not working now.
---
title: "Tabset Column"
output: flexdashboard::flex_dashboard
---
Column
-------------------------------------
### Chart 1
```{r}
library(ggplot2)
library(plotly)
library(dplyr)
da <-data.frame(ns=rep(0:1,6), month=factor(1:12), a=letters[1:12])
p <- ggplot(da) +
geom_bar(aes(x=month,y=ns,fill=ns),stat = "identity") +
facet_wrap(~a )+
theme_bw(12) + ylab("CYER") +
scale_fill_viridis_c(option="D",direction = -1)
p2 <- ggplot(da) +
geom_bar(aes(x=month,y=ns,fill=ns),stat = "identity") +
facet_wrap(~a )+
theme_bw(12) + ylab("CYER") +
scale_fill_viridis_c(option="C",direction = -1)
```
Column {.tabset}
-------------------------------------
### Chart 2
```{r}
ggplotly(p2)%>%layout(
margin = list(b = 100, l = 100,r = 150))
```
### Chart 3
```{r}
ggplotly(p)%>%layout(
margin = list(b = 100, l = 100,r = 150))
```

Related

Plotly chart is not displayed in Shiny app

I am trying to implement an option with a drop-down menu in a simple Shiny application. With this drop-down list, you can choose between two charts. The first chart is prepared with the ggplot2 library, while the second is with the Plotly library. Below you can see my code :
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(plotly)
# Data
data <- mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("clusterNum",
label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1"
)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1 <- ggplot(data, aes(x = wt, y = mpg)) +
geom_point()
Chart2 <- plot_ly(data, x = ~wt, y = ~mpg, type = 'bar')
renderPlot({
switch(input$clusterNum,
"Chart1" = Chart1,
"Chart2" = Chart2
)
})
```
After executing this code, I saw that Chart 1, which was prepared with ggplot2, works well, while Chart 2 with Plotly is not displayed. So can anybody help me how to solve this problem and to see Chart 2 after selection with the drop-down list?
To render the plotly chart you have to use renderPlotly, while for the ggplot we have to stick with renderPlot. As a consequence switching conditionally between the two render functions requires some more effort and involves wrapping in renderUI and displaying the chart via uiOutput:
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(plotly)
# Data
data <- mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("clusterNum",
label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1"
)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1 <- ggplot(data, aes(x = wt, y = mpg)) +
geom_point()
Chart2 <- plot_ly(data, x = ~wt, y = ~mpg, type = 'bar')
```
```{r}
observeEvent(input$clusterNum, {
output$plot <- switch(input$clusterNum,
"Chart1" = renderUI({renderPlot(Chart1)}),
"Chart2" = renderUI({renderPlotly(Chart2)})
)
})
uiOutput("plot")
```

R ggplot2 rmd returning an empty html

Based on the code and data below, the rmd knitted fine initially with the code and output (default option), but then I just changed the chunk option to show output only. And, since then I am getting an empty html. I tried reverting back to default, I even cleared the environment and restarted RStudio, but it does not fix it.
What could be causing the issue?
Code + data:
---
title: "Gantt Chart"
author: "Ed"
date: "2022-08-12"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(include = FALSE)
library(tidyverse)
library(plotly)
```
```{r Gantt Chart, echo=FALSE}
df = data.frame(task=c("Explore Ideas", "Finalize Idea", "Make Plots with\n geom_line", "Make Plots with\n Plan Package", "Add Writeups", "Refine Plots", "Review Tutorial", "Submit Tutorial", "Get Feedback\n and Update"),
start=c("2019-10-15", "2019-10-18", "2019-10-19", "2019-10-22", "2019-10-24", "2019-10-25", "2019-10-26", "2019-10-27", "2019-10-28"),
end=c("2019-10-18", "2019-10-19", "2019-10-24", "2019-10-26", "2019-10-27", "2019-10-27", "2019-10-28", "2019-10-28", "2019-10-31"),
owner=c("Harish", "Phani", "Harish", "Phani", "Phani", "Harish", "Phani", "Harish", "Phani"))
head(df)
df = df %>%
mutate(start = as.Date(start), end = as.Date(end))
df_tidy = df %>%
gather(key=date_type, value=date, -task, -owner)
gantt_plot = ggplot() +
geom_line(data=df_tidy, mapping=aes(x=fct_rev(fct_inorder(task)), y=date, color=owner), size=10) +
geom_hline(yintercept=as.Date("2019-10-27"), colour="black", linetype="dashed") +
coord_flip() +
scale_y_date(date_breaks = "1 day") +
labs(title="Community Contribution Gantt Chart",
x = "Task",
y = "Date",
colour = "Owner") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90),
panel.grid.minor = element_line(colour="white", size=0.5),
legend.position="right",
plot.title = element_text(hjust = 0.5))
ggplotly(gantt_plot)
```

Flex Dashboard not working when I add the world map code in it

I have created a flex dashboard using R Markdown in RStudio for 'COVID-19 WORLDWIDE TRACKER'. I have created one line plot, one area plot and 2 column graphs. I want to add a world heat map in it.
The data is here:
mydata
and
x. Code is as follows:
---
title: "COVID-19 WORLDWIDE TRACKER"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(readxl)
library(rworldmap)
library(dplyr)
library(maps)
library(plyr)
library(gridExtra)
x <- read_excel("owid-covid-data.xlsx")
```
Page 1
===
row {data-height = 350}
---
### World Hotspots
```{r}
library(maps)
library(ggplot2)
mydata <- readxl::read_excel("mapdata1.xlsx")
mydata$Country[mydata$Country == "United States"] <- "USA"
mydata$Country[mydata$Country == "United Kingdom"] <- "UK"
world_map <- map_data("world")
world_map <- subset(world_map, region != "Antarctica")
ggplot(mydata) +
geom_map(
dat = world_map, map = world_map, aes(map_id = region),
fill = "white", color = "#7f7f7f", size = 0.25
) +
geom_map(map = world_map, aes(map_id = Country, fill = Cases), size = 0.25) +
scale_fill_gradient(low = "#fff7bc", high = "#cc4c02", name = "Total Cases") +
expand_limits(x = world_map$long, y = world_map$lat)
```
row {data-height=300}
-----------------------------------------------------------------------
### Top 10 Countries
```{r}
p <- ggplot(x, aes(date, total_cases, color = location)) + geom_line()
ggplotly(p)
```
row {data-height=300}
-----------------------------------------------------------------------
### Daily Comparison of New Cases and Deaths
```{r}
a <- ggplot(x, aes(date, new_cases, fill = location)) + geom_area()
ggplotly(a)
```
row {data-height=400}
-----------------------------------------------------------------------
### Daily Comparison of Total Cases and Deaths
```{r}
b <- ggplot(x, aes(date, total_cases, fill = total_deaths)) + geom_col(position = "dodge")
ggplotly(b)
```
### Top Countries
```{r}
c <- ggplot(x, aes(total_cases_per_million, location)) + geom_col(position = "dodge")
ggplotly(c)
```
Without the world map code, the dashboard works perfectly fine. But when I add the world map code to it, the dashboard shows a white screen and no graphs at all. I am not able to understand why this is happening. Please help!

Rmarkdown to knit html - graphs not showing

I have the following lines of code:
ggplot(data = subset(gapminder1, year %in% c(seq(1900, 1990, by = 10))), aes(x = year, y = lifeExp)) + geom_boxplot(aes(group = year))
ggplot(subset(gapminder1,country=="United States"),aes(x = year, y = lifeExp)) + geom_line()
ggplot(subset(gapminder1,country %in% c("China","India","United States","Indonesia","Brazil")) , aes(x = year, y = lifeExp)) + geom_line(aes(color=country))
The graphs show up fine in the rmd file when I run the code. However, when I knit the document the graphs do not show up (a blank graph comes up). Can anyone tell me what I could do?
Please see the code below knitted into HTML file:
---
title: "check"
author: "Artem"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Check graph
```{r gapminder, echo=FALSE}
gapminder <- read.csv("https://raw.githubusercontent.com/birdsarah/pydata-nc/master/tutorial/assets/gapminder.csv", header = TRUE)
gapminder1 <- setNames(gapminder, c(c("country", "year", "lifeExp", "population", "income", "region"
)))
library(ggplot2)
gpm_sub <- subset(gapminder1, country %in% c("China","India","United States","Indonesia","Brazil"))
g1 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) +
geom_line(aes(color=country))
gpm_sub_us <- subset(gapminder1,country=="United States")
g2 <- ggplot(gpm_sub_us,aes(x = year, y = lifeExp)) +
geom_line()
g3 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) +
geom_line(aes(color=country))
library(gridExtra)
grid.arrange(g1, g2, g3, nrow = 2)
```
Output (No problems experienced).:

A Kableextra table and a ggplot plot on same row in Rmarkdown (PDF - not Flexdashboard)

I have been experimenting with R Markdown to create some PDF reports. I am having difficulty in getting the layout right. Basically, I need to have a KableExtra created table (dataframe) and a ggplot plot on the same row. I have explored some grid packages, but couldn't get it to work.
Here is my code:
---
title: "Untitled"
author: ""
date: "14 June 2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(reshape2)
library(dplyr)
library(kableExtra)
```
## R Markdown
```{r chart, echo=FALSE}
Years <- c("2016","2016","2016","2016",
"2017","2017","2017","2017")
Quarters <- c("Q1","Q2","Q3","Q4",
"Q1","Q2","Q3","Q4")
Series1 <- c("100","200","300","400","500","600","700","800")
Series1 <- as.numeric(Series1)
df <- data.frame(Years,Quarters, Series1)
library(ggplot2)
ggplot(df) +
geom_point(aes(x = Quarters, y = Series1)) +
facet_wrap( ~ Years, strip.position = "bottom",scales = "free_x") +
theme(panel.spacing = unit(0,"lines"), strip.background =
element_blank(),
strip.placement = "outside")
```
```{r table, echo=FALSE}
Table <- dcast(df, Years ~ Quarters, fun.aggregate = sum, value.var =
"Series1")
Table <- Table %>%
kable(format = "latex", caption = "Balances", booktabs = TRUE) %>%
kable_styling(latex_options = c("striped","hold_position","condensed"),
font_size = 10)
Table
```
If you are not strongly depending on kable() I can provide this gridExtra solution. When using tableGrob(kable(.)) the latex code won't be executed somehow, maybe somebody else comes up with how to execute latex code within a tableGrob().
```{r chart, echo=FALSE, message=FALSE}
df <- data.frame(Years=rep(2016:2017, each=4),
Quarters=rep(paste0("Q", 1:4), 2),
Series1=seq(100, 800, 100))
library(ggplot2)
p1 <- ggplot(df) +
geom_point(aes(x=Quarters, y=Series1)) +
facet_wrap( ~ Years, strip.position="bottom", scales="free_x") +
theme(panel.spacing=unit(0, "lines"),
strip.background=element_blank(),
strip.placement="outside",
aspect.ratio=1) # set aspect ratio
Table <- dcast(df, Years ~ Quarters, fun.aggregate=sum, value.var="Series1")
library(gridExtra)
t1 <- tableGrob(Table, theme=ttheme_minimal(), rows=NULL) # transform into a tableGrob
grid.arrange(p1, t1, nrow=1)
```
Produces:

Resources