rCharts - 像 gvisMerge 一样组合两个 rCharts

标签 r merge shiny rcharts

无论如何,我可以组合两个 rChart,例如 gvisMerge(obj1, obj2)。我意识到 rCharts 对象是函数。有没有办法结合R函数。我正在使用一个 Shiny 的应用程序,我想在其中渲染两个 rChart。

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

在 ui.R 中

output$mytabs = renderUI({
  tabs = tabsetPanel(
         tabPanel('Plots', h4("Plots"), chartOutput("chart"))
  })

最佳答案

如果您使用Shiny,我建议您使用它的布局功能来布局您的页面,然后将图表放置在您想要的位置。这是一个最小的示例(您必须正确设置图表的宽度以避免重叠)

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')
    })
  }
))

关于rCharts - 像 gvisMerge 一样组合两个 rCharts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412831/

相关文章:

r - 通过列名的字符向量对data.table进行排序

r - 宽格式数据还是长格式数据效率更高?

r - KnitR,Text 和 Code block 的 Latex 条件显示

javascript - R Shiny 在带有侧面板/条件面板的选项卡之间构建链接

r - 如何确定简单回文 R 代码的时间复杂度?

git - 在git中删除 merge 的分支是否安全

GIT - merge 后分支不关闭

MySQL将多个列合并为一个 'temporary'列

r - Shiny:如何在同一张传单 map 上绘制多个参数?

Rselenium 无法连接到正在运行的 Shiny 应用程序