I believe I have a partial solution, based on this stackoverflow post: r markdown - For loop over dygraph does not work in R - Stack Overflow
However, I still suffer from the fact that the fig.width and fig.height values specified for the chunk are not obeyed, as per this post: r - dygraphs in Rmarkdown adjusting fig.height - Stack Overflow
This isn't quite right. The plots to not shrink, but they do overlap each other in the positions that would be right (they are just not scaled to be right height and width). Help on this part appreciated. EDIT: It looks you have to control this directly in the dygraph() call, and I have updated this code to reflect this. I've left the (now unhelpful) map call to wrap this in a div element in case that is useful to others if that changes other things.
This what I have so far:
#' ---
#' title: "scratchRmd.R"
#' author: "Matt"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' toc: true
#' toc_depth: 3
#' code_folding: hide
#' editor_options:
#' chunk_output_type: console
#' ---
#+message=F,warning=F
library(xts)
library(lubridate)
library(dygraphs)
library(purrr)
library(dplyr)
#' # With function
#'
#+fig.width=9,fig.height=2
foo <- function() {
tsv <-
seq(from=ymd("2022-01-01"), to=ymd("2022-01-08"), by="1 day")
y <-
runif(length(tsv))
z <-
runif(length(tsv))
list(
dygraph(xts(y, order.by = tsv)
, main="Y"
, group=1
, width=800
, height=150)
, dygraph(xts(z, order.by = tsv)
, main="Z"
, group=1
, width=800
, height=150)
)
}
foo() %>%
# map(~{htmltools::tags$div(.x, style = "padding:10px; width: 450px; height: 150px;")}) %>%
htmltools::tagList()