2016-08-17 64 views
0

我写了一个函数,在多个幻灯片上使用for循环创建一个rChart。 但是,调用该函数不会创建多个图表。我只得到最后一张图。这是我的职责是什么样子:rChart,for循环

s1Rev <- function(total1){ 
a <- rCharts:::Highcharts$new() 
a$chart(type = "column") 
a$xAxis(categories = total1$sources) 
a$yAxis(title = list(text = "Revenue")) 
a$data(total1) 
a$print('chart1', cdn=TRUE, include_assets=TRUE) 
} 

for(e in 1:length(impEvents)){ 
    cat("\n\n## Total Revenue: ", eventName[e], "##\n") 
    s1Rev(impEvents[e]) 
} 

一旦我编,我看到仅在第一张幻灯片并没有在幻灯片的其余部分的最后一个情节。我究竟做错了什么?

+0

我不能共享数据,但它是结构如同'''data.frame(HairEyeColor)'''我试过'''回报(a)''''和'''return($ print('chart1',cdn = TRUE,include_assets = TRUE)'''我认为解决方案在于理解rCharts如何在for循环中工作。 – krthkskmr

回答

1

我解决了这个问题。我只需要在循环中更改图表的名称(我不知道该调用哪个参数)。这让我正确的结果的代码如下:

s1Rev <- function(total1){ 
    a <- rCharts:::Highcharts$new() 
    a$chart(type = "column") 
    a$xAxis(categories = total1$sources) 
    a$yAxis(title = list(text = "Revenue")) 
    a$data(total1) 
return(a) 
} 

for(e in 1:length(impEvents)){ 
    cat("\n\n## Total Revenue: ", eventName[e], "##\n") 
    p1 <- s1Rev(impEvents[e]) 
    p1$print(paste0('chart',e), cdn=TRUE, include_assets=TRUE) 
}