Render mulitple `dygraph` charts, created within function

I'd like to make a Rmarkdown report, and use a function to encapsulate making of multiple grouped dygraph charts. I cannot figure out how to have all of the charts rendered when called from within a function. The following snippet only renders only the last of the two dygraph plots I create. I am looking for how I display both plots in the ensuing markdown by calling the function foo().

I have an answer/response below that gets most of the way there, but I cannot figure out how to control the height/width appropriately, which is laid out there.

#' ---
#' 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
options(width = 188)

library(xts)
library(lubridate)
library(dygraphs)
library(dplyr)


#' # Run it
#' 
#+

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))
    
    dygraph(xts(y, order.by = tsv)
            , main="Y"
            , group=1)
    
    dygraph(xts(z, order.by = tsv)
            , main="Z"
            , group=1)
}
foo()

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() 

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.