2014-03-13 39 views
1

我想生成多个rCharts饼图基于选择Group,这个Group可以改变取决于数据帧。我想在这里动态地添加rCharts到网页使用闪亮

https://gist.github.com/wch/5436415/

someDF = structure(list(Variable.Type = c("Apple", "Orange", "Banana", 
"Apple", "Orange", "Banana"), Total = c(2, 1, 3, 6, 5, 4), Market = c("Pete", 
"Pete", "Pete", "Sams", "Sams", "Sams")), .Names = c("Variable.Type", 
"Total", "Market"), row.names = c(NA, -6L), class = "data.frame") 

list = unique(someDF$Market) 

使用类似的例子如在server.R

output$somePieCharts = renderUI({ 
list = unique(someDF$Group) 
plot_output_list = lapply(1:length(list), function(i){ 
    plotData = filter(someDF, Group==list[i]) 
    chartOutput(hPlot(Total~Variable.Type, data=plotData, type='pie')) 
} 
          ) 
do.call(tagList, plot_output_list) 
}) 

在ui.R

uiOutput('somePieCharts') 
+0

提供一些数据以使事情更具体。 – Ramnath

+0

当我们再次给你答案时,你会删除这个问题吗? ;) –

+0

@JulienNavarre,为什么我会删除这个问题? – BigDataScientist

回答

1

这里是你如何修改与hPlot一起使用的动态地块示例。我刚刚用chartOutputrenderPlotrenderChart2替换了plotOutput。其余的变化是不言自明的。

library(shiny); library(rCharts) 
Markets = unique(someDF$Market) 
server = function(input, output) { 

    # Insert the right number of plot output objects into the web page 
    output$plots <- renderUI({ 
    plot_output_list <- lapply(1:2, function(i) { 
     plotname <- paste("plot", i, sep="") 
     chartOutput(plotname, "highcharts") 
    }) 

    # Convert the list to a tagList - this is necessary for the list of items 
    # to display properly. 
    do.call(tagList, plot_output_list) 
    }) 

    # Call renderPlot for each one. Plots are only actually generated when they 
    # are visible on the web page. 
    for (i in 1:length(Markets)) { 
    # Need local so that each item gets its own number. Without it, the value 
    # of i in the renderPlot() will be the same across all instances, because 
    # of when the expression is evaluated. 
    local({ 
     my_i <- i 
     plotname <- paste("plot", my_i, sep="") 

     output[[plotname]] <- renderChart2({ 
     print(my_i) 
     plotData = subset(someDF, Market == Markets[my_i]) 
     print(plotData) 
     hPlot(Total ~ Variable.Type, data = plotData, type='pie') 
     }) 
    }) 
    } 
} 

ui = pageWithSidebar(
    headerPanel("Dynamic number of plots"), 

    sidebarPanel(), 

    mainPanel(
    # This is the dynamic UI for the plots 
    uiOutput("plots") 
) 
) 

runApp(list(ui = ui, server = server))