2016-12-01 1 views
1

Ich verwende highcharter für meine Graphen. Ich habe verschiedene Kurse, die ich für alle gleich zeichnen muss. Ich versuche eine for Schleife über Kurse zu haben und zeichne diese Graphen innerhalb meiner Schleife. Das Problem, dass es nichts innerhalb der Schleife plotten und es funktioniert gut von außerhalb der Schleife.In einer Schleife mit Highcharter plotten

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 

Antwort

1

Antwort auf das Problem ist hier: https://stackoverflow.com/a/4716380/4046096

Kurz: Automatischer Druck wird in einer Schleife ausgeschaltet, so dass Sie explizit print etwas brauchen.

Anstelle von hcprint(hc) verwenden.

Ich habe Ihre Daten nicht, aber dieser Code funktioniert:

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

Vielen Dank .. es funktioniert. – shanyour

+0

@shanyour Wenn diese Antwort Ihre Frage beantwortet, können Sie sie als akzeptiert markieren - wenn nicht, bitte geben Sie weitere Informationen darüber, was unbeantwortet bleibt. –

Verwandte Themen