使用highcharter在循环内绘制 [英] Plot inside a loop using highcharter

查看:86
本文介绍了使用highcharter在循环内绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为图表使用highcharter.我有不同的课程,所有课程都需要绘制相同的图形.我正在尝试在课程上建立一个for循环并将这些图形绘制在我的循环中. 它不会在循环内绘制任何内容,并且可以在循环外正常工作的问题.

I am using highcharter for my graphs. I have different courses, which I need to draw same graphs for all. I am trying to have a for loop over courses and plot these graphs inside my loop. The problem that it doesnt plot anything inside the loop and it works fine from outside the loop.

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

推荐答案

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

简而言之:自动打印会循环关闭,因此您需要显式地print.

In short: Automatic printing is turned off in a loop, so you need to explicitly print something.

使用print(hc)代替hc.

Instead of hc use print(hc).

我没有您的数据,但是此代码可以正常工作:

I do not have your data, but this code works fine:

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)
}

这篇关于使用highcharter在循环内绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆