4

Taken from this question, I would like to know how to show a dy_graph list object in shiny. The piece of code below creates it but I am not an expert in html and reading the htmltools manual did not help. Essentially I need this part htmltools::browsable(htmltools::tagList(dy_graph)) altered for rendering in Shiny.

# create the time series
temperature <- ts(frequency = 12, start = c(1980, 1),
              data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
                       25.2, 26.5, 23.3, 18.3, 13.9, 9.6))
rainfall <- ts(frequency = 12, start = c(1980, 1),
           data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 
                    135.6, 148.5, 216.4, 194.1, 95.6, 54.4))

# create a list of dygraphs objects
library(dygraphs)
library(htmltools)
dy_graph <- list(
  dygraphs::dygraph(temperature, group="temp_rain", main="temperature"),
  dygraphs::dygraph(rainfall, group="temp_rain", main="rainfall")
)  # end list

# render the dygraphs objects using htmltools
htmltools::browsable(htmltools::tagList(dy_graph))

EDIT: Please look at @SBista 's answer. mucho blank space!

MLavoie
  • 9,671
  • 41
  • 36
  • 56
J. Doe.
  • 1,255
  • 1
  • 12
  • 25

1 Answers1

6

You can do that using uiOutput and renderUI. You can do something like this:

library(shiny)
library(dygraphs)

ui <- fluidPage(

  uiOutput("dygraph")
)


server <- function(input, output)
{

  output$dygraph <- renderUI({
    # create the time series
    temperature <- ts(frequency = 12, start = c(1980, 1),
                      data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
                               25.2, 26.5, 23.3, 18.3, 13.9, 9.6))
    rainfall <- ts(frequency = 12, start = c(1980, 1),
                   data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 
                            135.6, 148.5, 216.4, 194.1, 95.6, 54.4))

    dy_graph <- list(
      dygraphs::dygraph(temperature, group="temp_rain", main="temperature"),
      dygraphs::dygraph(rainfall, group="temp_rain", main="rainfall")
    )  

    tagList(dy_graph)
  })

}

shinyApp(ui = ui, server = server)
SBista
  • 7,479
  • 1
  • 27
  • 58
  • Thank you for this! It works, but I get a lot of whitespace... Please have a quick look above at my edit. Any ideas why?? – J. Doe. Jan 17 '18 at 11:46
  • Nevermind! I found it https://stackoverflow.com/questions/39507710/remove-white-space-on-right-side-in-dygraphs – J. Doe. Jan 17 '18 at 11:54
  • I guess I was too quick to yell "case closed". The option rightgap does not work in our list-style instance. – J. Doe. Jan 17 '18 at 12:00
  • Ok, I got it. Use the width and height options in `dygraphs::dygraph` as such: `dygraphs::dygraph(z, main = "xxx", group = dataset_name, width = "100%", height = "400px" ) ` – J. Doe. Jan 17 '18 at 12:31