-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
offline plots in for loops #273
Comments
It looks like ggvis suffers from the same problem. I have a feeling it's because knitr can only call
|
Looks like @yihui will provide a solution for us 😊 |
Here is the easy way:
|
Here is the current way to do this:
Or, if you prefer a for loop:
|
Nice workarround. Unfortunately, I'm not able to use it in my current automated analysis where in each iteration of |
@GegznaV, what are you using to make the tables? Do you have a small code sample? This can still be made to work with a combination of |
simple example of
library(ggplot2)
library(plotly)
library(magrittr)
library(pander) for(i in c("vs", "am", "gear", "carb")){
print(i)
mtcars$am <- as.factor(mtcars[[i]])
# plot 1
{ggplot(mtcars,aes(x = mpg, y = disp, color = am)) + geom_point()} %>%
ggplotly
# plot 2
{ggplot(mtcars,aes(x = mpg, fill = am)) + geom_density(alpha = .3)} %>%
ggplotly
# table 1
summary(mtcars[,c("mpg","disp",i)]) %>% pander
# table 2: Kruskal-Wallis test
kruskal.test(mtcars$mpg, mtcars[[i]]) %>% pander
} May be it's possible to save images as |
@GegznaV, this is a hack in the ugliest of forms, but it does work. Perhaps, @jcheng5, @ramnath_vaidy, or @yihui can demonstrate an easier way.
|
@timelyportfolio For printing out html widgets and other things created in a for loop, this process is incredibly involved. Still, it is the closest fit to how I've been using rmarkdown to create dynamic documents. If I want to extend the example given to new options, like datatables or character output without a table in it, what's the best way to figure out what I should looking to put into the print statement? How do you debug such things when testing them out. Edit: Ok, I think I have datatables figured out. They work the same as ggplotly objects when stored as widgets. By the way thanks for posting this solution. |
Thank you @timelyportfolio for your solution. I was trying to get some ggplotly outputs rendered as tabsets from an rmarkdown to html_output, but couldn't figure it out. It was driving me mad. I was able to plot everything with modified form to your solution. Cheers! |
Quite an easy way to add tables and plots (and more plots and titles etc.) in a loop in RMarkdown is:
You only need to watch out that the plotly libs get included. Therefore I first create a plot outside the loop. |
Dear all, I am trying an approach combining solutions from @timelyportfolio and @ghost. test.Rmd below
The plots will be shown when I do render("test.Rmd") and open test.html from my browser. However, it won't appear when I put the Rmd file in the shiny server. I wonder what is the difference between these two. Any way to modify it so it will also work in the shiny server. Thanks! |
Hi @yihui, @cpsievert and everyone, Thank you for your quick fix with However, when we combine this with native collapsible sections in markdown, the plotly plots don't render and just churn out empty div under the collapsible sections. However, as proposed above, printing 1 plotly plot outside the collapsible sections renders them correctly. However, with our current implementation we want to have all the sections collapsible in the rmd report. Any suggestions to solve this is highly appreciated. Thank you so much for your time. |
@mayank7j-shell Simply add the following chunk at the start of your RMarkdown document-
This ensures the Plotly JS dependencies are loaded before Plotly functions are called in a |
Hi @HDash Thank you so much for your assistance and quick reply. It works. Best Regards, |
a current workaround is:
maybe there is an easier way?
The text was updated successfully, but these errors were encountered: