2016-08-17 66 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) 
}