2

I'm following this example to plot multiple graphs depending of different parameters (as data frame columns). So the case is that the number of plots to do will vary each day.

I have modified the code to use Highcharter to get javascript charts instead of basic plots but it doesn't work.

Also I would like to know what I have to add to this code to plots charts in 2,3 or 4 columns?

Thanks

ui.R

fluidPage(
  # Application title
  titlePanel("Hello World!"),

  # Show a plot
  fluidRow(      
    column(width = 6,
           highchartOutput("hcontainer", height = "400px")
    ) 
  )
)

server.R

get_plot_output_list <- function() {
    plot_output_list <- lapply(1:NCOL(df), FUN = function(i) {
      plot_output_object <- highchartOutput("hcontainer")
      plot_output_object <- renderHighchart({
        hc <- highchart() %>%
          hc_add_serie(name = "df name", data = df)
        return(hc)
      })
    })
    do.call(tagList, plot_output_list) # needed to display properly.
    return(plot_output_list)
  }

observe({
    output$hcontainer <- renderUI({ get_plot_output_list() })
    #output$hcontainer <- renderHighchart({ get_plot_output_list() })
  })
Carlos Espeleta
  • 129
  • 1
  • 1
  • 12

1 Answers1

2

Hi you can try this solution, it do not use the same function as you but one by @jenesaisquoi (found here), this function create several plots and handle the layout correctly :

# Packages
library("highcharter")
library("shiny")

# data
df <- data.frame(
  var1 = rnorm(10),
  var2 = rnorm(10),
  var3 = rnorm(10),
  var4 = rnorm(10),
  var5 = rnorm(10),
  var6 = rnorm(10),
  var7 = rnorm(10)
)

# Fun by @jenesaisquoi (modified with highchartOutput)
makePlotContainers <- function(n, ncol=2, prefix="plot", height=100, width="100%", ...) {
  ## Validate inputs
  validateCssUnit(width)
  validateCssUnit(height)

  ## Construct plotOutputs
  lst <- lapply(seq.int(n), function(i)
    highchartOutput(sprintf('%s%g', prefix, i), height=height, width=width))

  ## Make columns
  lst <- lapply(split(lst, (seq.int(n)-1)%/%ncol), function(x) column(12/ncol, x))
  do.call(tagList, lst)
}

You can use @jenesaisquoi's function directly in the ui, and use lapply in the server for define as many outputs as the number of cols :

# App
ui <- fluidPage(
  # Application title
  titlePanel("Hello World!"),

  # Show plots
  makePlotContainers(n = ncol(df), ncol = 3, prefix = "hcontainer", height = "400px")
)

server <- function(input, output) {
  lapply(
    X = seq_len(ncol(df)),
    FUN = function(i) {
      output[[paste0("hcontainer", i)]] <- renderHighchart({
        highchart() %>%
          hc_add_serie(name = paste("df name", i), data = df[[i]])
      })
    }
  )
}
shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • In my case I have two files ui.R and server.R. In this case I have used global.R to share the same dataset in both parts. http://shiny.rstudio.com/articles/scoping.html – Carlos Espeleta Apr 20 '16 at 10:25