3

Is there anyway, that I can combine two rCharts, like gvisMerge(obj1, obj2). I realized the rCharts objects are functions. Is there any way to combine R functions. I am using a shiny application in which I want to render two rCharts.

 output$chart = renderChart({
 a <- rHighcharts:::Chart$new()
 a$title(text = "Report 1")
 a$xAxis(categories = as.character(df1$Date))
 a$yAxis(title = list(text = "Report 1"))

 a$data(x = df1$Date, y = df1$Val1, type = "line", name = "Values 1")
 a$data(x = df1$Date, y = df1$Val2, type = "column", name = "Values 2")
 a

 b <- rHighcharts:::Chart$new()
 b$title(text = "Report 2")
 b$xAxis(categories = as.character(df2$Week))
 b$yAxis(title = list(text = "Report 2"))

 b$data(x = df2$Week, y = df2$Val3, type = "line", name = "Values 3")
 b$data(x = df2$Week, y = df2$Val4, type = "column", name = "Values 4")
 b
 return(a,b) # Can we combine both and return
 })

In ui.R

output$mytabs = renderUI({
  tabs = tabsetPanel(
         tabPanel('Plots', h4("Plots"), chartOutput("chart"))
  })
BigDataScientist
  • 1,045
  • 5
  • 17
  • 37

1 Answers1

3

If you are using Shiny, I would recommend using its layout functions to layout your page and then place the charts where you desire. Here is a minimal example (you will have to set the widths of the charts correctly to avoid overlap)

library(shiny)
library(rCharts)

runApp(list(
  ui = fluidPage(
    title = 'Multiple rCharts',
    fluidRow(
      column(width = 5, chartOutput('chart1', 'polycharts')),
      column(width = 6, offset = 1, chartOutput('chart2', 'nvd3'))
    )
  ),
  server = function(input, output){
    output$chart1 <- renderChart2({
      rPlot(mpg ~ wt, data = mtcars, type = 'point')
    })
    output$chart2 <- renderChart2({
      nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
    })
  }
))
Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • Thanks for the answer, but I want to send through one output only. I am using rHighcharts as well. Now I added sample data in my question. – BigDataScientist Jan 28 '14 at 18:38
  • No. `renderChart` only returns one object. Any reasons you want to send only one output, when it is trivial to send as many as you want? Second, the functionality of `rHighcharts` has been absorbed into `rCharts` and future development will happen in `rCharts`. – Ramnath Jan 28 '14 at 18:53
  • The reason is I am also rendering tabs from server.R, I want to send multiple plots in one tab. Please let me know if there is any other method. Thats great to hear the functionality is included in rCharts. It would be great if you could also include drill-down facility like this one http://www.highcharts.com/demo/column-drilldown – BigDataScientist Jan 28 '14 at 19:00
  • Drilldowns require explicitly coding the javascript. There might be a way to abstract away the computations, but it doesnt look straightforward. – Ramnath Jan 28 '14 at 19:40