I have this dataframe
products <- c("Product1", "Product2", "Product3")
departments <- c("Dept1", "Dept2", "Dept3")
campaigns <- c("Campaign1", "Campaign2", "Campaign3", "Campaign4", "Campaign5")
agents <- c("Agent1", "Agent2", "Agent3", "Agent4", "Agent5")
combinations <- expand.grid(Product = products,
Department = departments,
Campaign = campaigns,
Agent = agents)
grouped_data <- combinations %>%
group_by(Product, Department, Campaign, Agent) %>%
summarize(Total_Sales = sum(Sales),
Average_Sales = mean(Sales))
I'd like to print in Rmarkdown programmatically without manually creating so many sections in my document so I created this function:
render_content <- function(dataframe) {
split_data <- split(dataframe, f = list(dataframe$Product, dataframe$Campaign))
for (product in unique(dataframe$Product)) {
cat(paste0("<h2>", product, "</h2>\n"))
product_data <- split_data[grepl(product, names(split_data))]
for (campaign in unique(dataframe$Campaign)) {
campaign_key <- paste(product, campaign, sep = ".")
if (campaign_key %in% names(product_data)) {
cat(paste0("<h3>", campaign, "</h3>\n"))
table_html <- reactable(product_data[[campaign_key]],
defaultPageSize = 5,
minRows = 0)
cat(as.character(htmltools::as.tags(table_html)))
}
}
}
}
I put this chunk to my rmarkdown document but only h2 and h3 tags are rendered, I believe it has something to do with the necessity to include tags in documents so I tried several variants of it, but nothing helps.
{r, include=FALSE}
htmltools::tagList(reactable())
{r results='asis', echo=FALSE}
render_content(grouped_data)

Here is a reproducible example of one approach how you can achieve your aims. It uses a child document.
The next part is your main .Rmd file.
The next part is a separate .Rmd file called
so-content.Rmd.