-
DescriptionI've tried to mix different content: markdown headers, static graphs and html widgets (tables and graphs). The reason behind this is to build parametrised reports where the titles will be the comparisons and there might be different types of output generated. I've tried tons of options because I wasn't sure about how to approach this, and so far, only one option (out of 26) seems to be working. It's important to mention that what I see as output in the IDE does not match what I see as output after doing Quarto rendering (likely because it treats each part separately). I come here to see if there's a "correct"/official way to do this, and if not, I'll use method 23, which is renderTags + cat(x$html). Working solution:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
@joanbadia Please provide a small reproducible example. There is no way someone can help you with a one thousand lines of code. You likely need |
Beta Was this translation helpful? Give feedback.
-
From my understanding of your example this is how it would be done
This child doc would be used in main doc through ```{r}
#| results: asis
#| echo: false
#| message: false
#| warning: false
for(content_type in content_types) {
res <- knitr::knit_child(
"_child.qmd",
quiet = TRUE
)
cat(res, sep = "\n")
}
``` Full example ---
title: "Markdown header + HTML Widget + Static Graph"
format:
html:
embed-resources: true
html-table-processing: none
---
```{r}
#| label: setup
#| echo: false
#| message: false
library(dplyr)
library(reactable)
library(htmltools)
library(purrr)
library(knitr)
library(ggplot2)
library(plotly)
# Create test reactable function
create_test_table <- function(data) {
reactable(
data,
defaultPageSize = 5,
striped = TRUE,
compact = TRUE,
style = list(fontSize = "12px"),
theme = reactableTheme(
color = "#333",
backgroundColor = "#f9f9f9",
borderColor = "#ddd"
)
)
}
# Create small ggplot
create_ggplot <- function(data) {
ggplot(data, aes(x = Category, y = Value)) +
geom_col(fill = "steelblue", alpha = 0.7) +
theme_minimal() +
theme(
text = element_text(size = 8),
axis.text = element_text(size = 6),
plot.title = element_text(size = 9)
) +
labs(title = "Values by Category")
}
# Create small plotly
create_plotly <- function(data) {
plot_ly(data, x = ~Score, y = ~Value, type = 'scatter', mode = 'markers',
marker = list(size = 6, color = 'red', opacity = 0.7)) %>%
layout(
title = list(text = "Score vs Value", font = list(size = 10)),
xaxis = list(title = list(text = "Score", font = list(size = 8))),
yaxis = list(title = list(text = "Value", font = list(size = 8))),
width = 300,
height = 200,
margin = list(l = 40, r = 20, t = 40, b = 30)
) %>%
config(displayModeBar = FALSE)
}
# Generate single test dataset
set.seed(42)
test_data <- data.frame(
ID = paste0("Item_", 1:8),
Name = paste("Test", letters[1:8]),
Value = round(runif(8, 10, 100), 1),
Category = sample(c("A", "B", "C"), 8, replace = TRUE),
Score = round(rnorm(8, 50, 15), 1)
)
# Content types for realistic loop testing
content_types <- c("HTML table", "static graph", "html graph")
```
## Creating Section with `knit_child()`
```{r}
#| results: asis
#| echo: false
for(content_type in content_types) {
res <- knitr::knit_child(
"_child.qmd",
quiet = TRUE
)
cat(res, sep = "\n")
}
``` This could be done different in the child document with several chunk and conditional evaluation based on
The important part of all this is that I don't know what are you others 20+ methods, but this is how I would do it. Hope it helps |
Beta Was this translation helpful? Give feedback.
From my understanding of your example this is how it would be done
_child.qmd
This child doc would be used in main doc through
knitr::knit_child()
Full example