Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - Plotly charts in a for loop

I am converting ggplot2 charts using plotly and putting them in a knitr html output. Here is a working copy of the report. http://rpubs.com/kausti/NSEFO-23-Mar-2016 The first chart is plotly chart and the following ones are not. I would like them to be plotly charts but I am stuck.

In the code below, plotChart function returns a ggplot2 plot. This code works!

for (tick in tradeOps$ticker)
{
  tmpDF = filter(tradeOps, ticker == tick)
  p = plotChart(tick = tick,Entry = tmpDF$Entry, Target =     tmpDF$Target, SL= tmpDF$SL, TradeType = tmpDF$TradeType, data = wd)
  p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y =   tmpDF$Entry, label = tmpDF$Entry, colour = "blue")
  p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$Target, label = tmpDF$Target)
  p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$SL, label = tmpDF$SL)

  print(p)

}

Now if I change the last print statement to print(ggplotly(p)) it just won't print the charts in html. The code works outside knitr perfectly.

Also, print(ggplotly(p)) works perfectly outside loops. Am I missing something?

EDIT:

Including a reproducible example below. Following is my rmd file.

---
title: "tmp"
output: html_document
---

```{r, echo=FALSE, message=FALSE, fig.width=8, fig.height=6}
library(ggplot2)
library(plotly)
library(dplyr)
s = NULL
a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
for(i in unique(a$z)){
  s = a[a$z == i,]
  print(ggplot(s,aes(x = x, y = y)) + geom_point())
}

```

This works perfectly with the output html showing three graphs. Now, just changing the last print statement to print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point())) produces a blank html without any errors.

Running the same code in terminal works perfectly though

library(ggplot2)
library(plotly)
library(dplyr)
s = NULL
a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
for(i in unique(a$z)){
  s = a[a$z == i,]
  print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))
}

Appreciate any help.

Thanks, Kaustubh

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It turns out the problem is solved here https://github.com/ropensci/plotly/issues/273

knitr has known issues printing plotly charts in a loop. Quoting the final answer cpsievert from here

```{r}
l <- htmltools::tagList()
for (i in 1:3) {
  l[[i]] <- as.widget(plot_ly(x = rnorm(10)))
}
l
```

This solved my problem. Though I ended up porting everything from ggplot2 to plotly R api before I found this solution.

MLavoie, thanks for your help. I had generated a dummy dataset just to create a reproducible example. Though your solution probably solves the dummy example, it is not all that relevant to me in the current problem. I think the solution provided here should be generic.

Thanks, Kaustubh


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...