6

如何使用闪亮和 rchart 运行多个图表来覆盖输出显示选项,以便输出结果是 2x2 矩阵类型网格布局。

require(rCharts)
require(shiny)
require(data.table)
runApp(list(
  ui = mainPanel( span="span6", 
    showOutput("chart2", "Highcharts"),
    showOutput("chart3", "Highcharts"),
    showOutput("chart4", "Highcharts")
    ),
  server = function(input, output){
      output$chart3 <- renderChart({
      a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "bubble", title = "Zoom demo", subtitle = "bubble chart", size = "Age", group = "Exer")
      a$chart(zoomType = "xy")
      a$chart(backgroundColor = NULL)
      a$set(dom = 'chart3')
      return(a)
    })
    output$chart2 <- renderChart({
      survey <- as.data.table(MASS::survey)
      freq <- survey[ , .N, by = c('Sex', 'Smoke')]
      a <- hPlot(x = 'Smoke', y = 'N', data = freq, type = 'column', group = 'Sex')
      a$chart(backgroundColor = NULL)
      a$set(dom = 'chart2')
      return(a)
    })
    output$chart4 <- renderChart({
      survey <- as.data.table(MASS::survey)
      freq <- survey[ , .N, by = c('Smoke')]
      a <- hPlot(x = "Smoke", y = "N", data = freq, type = "pie")
      a$plotOptions(pie = list(size = 150))
      a$chart(backgroundColor = NULL)
      a$set(dom = 'chart4')
      return(a)
    })
  }
))
4

2 回答 2

6

将 ui 更改为:

ui = bootstrapPage(mainPanel( 
      div(class = "row",
        div(showOutput("chart2", "Highcharts"), class = "span4"),
        div(showOutput("chart3", "Highcharts"), class = "span4")
      ),
      div(class = "row",
        div(showOutput("chart4", "Highcharts"), class = "span4")
      )
    ))

添加bootstrapPage告诉闪亮使用引导库。查看http://getbootstrap.com/2.3.2/scaffolding.html以了解“脚手架”。mainPanel有一个默认为 8 的宽度选项。这是 bootstrap 中的 span8。上面的代码并不理想,但希望它是一个开始。

编辑:全屏

ui = bootstrapPage(mainPanel(width = 12,  
      div(class = "row",
        div(showOutput("chart2", "Highcharts"), class = "span6"),
        div(showOutput("chart3", "Highcharts"), class = "span6")
      ),
      div(class = "row",
        div(showOutput("chart4", "Highcharts"), class = "span6")
      )
    ))

请注意, mainPanel(..., width = width) 只是具有宽度跨度的 div 的便利功能。

结果截图:

在此处输入图像描述

于 2014-02-20T00:38:33.847 回答
1

R Shiny 原生的另一个解决方案(没有 HTML 的外部知识)是使用列的想法。

ui = mainPanel(fluidPage(
    fluidRow(
      column(width=6, 
             showOutput("chart2", "Highcharts"),
             showOutput("chart3", "Highcharts")
    ),
    fluidRow(
      column(width=6, 
             showOutput("chart4", "Highcharts") 
        )
      )
    )
  )),
于 2015-12-15T04:47:27.753 回答