在循环中使用地块highcharter(Plot inside a loop using highc

2019-09-29 19:40发布

我使用highcharter我的图表。 我有不同的课程,这是我需要绘制图表一样对所有。 我想有一个for循环在课程和我的绘制循环中这些图形。 问题在于它没有情节的环里的任何东西,它工作正常,从外循环。

for(i in unique(my_data$CourseID)){
  courseData<-subset(my_data, my_data$CourseID== i)

  #Course by Majer
  byMajer <- table(courseData$MajerName)
  #barplot(byMajer, main="Students by Majer", xlab="Majers") 

  byM <- aggregate(courseData$MajerName, by=list(courseData$MajerName), FUN=length)

  hc <- highchart(debug = TRUE) %>% 
  hc_title(text = courseData$CourseName[1]) %>% 
  hc_chart(type = "column") %>% 
  hc_xAxis(categories = byM$Group.1) %>% 
  hc_add_series(data = byM$x)

  #this is not working. it shows nothing.
  hc

  #but if I try to have this, it works, I will use below variables outside the loop
  assign(paste("hc",i,sep=""), hc)
}

#Below graphs showing in the output. I need to get rid of them and use the one inside the loop.
hc1; hc2; hc3; hc4; hc5; hc6; hc7; hc8; hc9; hc10; hc11; hc12; hc13; hc14; hc15; hc16; hc17; hc18

Answer 1:

这个问题的答案就在这里: https://stackoverflow.com/a/4716380/4046096

总之:自动打印在循环关闭,所以你需要明确print东西。

取而代之的hc使用print(hc)

我没有你的数据,但是这个代码工作正常:

for(i in c(1,2,3,4,5)){
  hc <- highchart(debug = TRUE) %>% 
  hc_chart(type = "column") %>% 
  hc_add_series(data = list(1, i, i * 2, 5))

  print(hc)
}


文章来源: Plot inside a loop using highcharter
标签: r highcharts