I would like to print single charts created in a for loop as tabbed content using the packages rmarkdown and highcharter.
So far i have only succeeded in displaying all the graphics in one tab.
The problem seems to be known (offline plots in for loops). However, I did not manage to apply the suggested solutions to my code.
Here is a minimal example:
---
title: "test"
output: html_document
---
```{r setup, include=FALSE}
#libraries
library(tidyverse)
library(highcharter)
library(htmltools)
#data
mydata <- data.frame(year = c("2018", "2018", "2019", "2020"),
license = c("CC BY", "CC BY-NC", "CC BY", "CC BY"),
cost = c(1000, 1500, 2000, 1000))
#functions
group_by_license <- function(x) {
x %>%
group_by(license) %>%
summarise(cost = sum(cost))
}
```
### license {.tabset .tabset-fade}
```{r mydata-license, results='asis', echo=F}
#data transformation
mydata_year <- split(mydata, mydata$year)
mydata_license <- lapply(mydata_year, group_by_license)
#chart
mychart <- function(i) {
hchart(mydata_license[[i]],"pie", hcaes(x = license, y = cost)) %>%
hc_title(text = paste0("My Data ", i))
}
#loop
a <- tagList()
for(i in unique(mydata$year)) {
cat("#### ", i, "\n \n")
a[[i]] <- mychart(i)
}
a
```
All three created charts are displayed in the last tab whereas the first two tabs are empty:
plots generated in the last tab
I would like to display each plot in the corresponding tab (graphic for 2018 in the 2018 tab etc.).
I also tried print
which did not work and either showed no result or the HTML code of the chart but no graphic: see tried solutions here.