我在图表中使用了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
答案 0 :(得分: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)
}