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