-
Notifications
You must be signed in to change notification settings - Fork 7
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
Is it possible to derive a series of plots by looping over a list of data.frames? #12
Comments
Hi and thanks for using the package. I don't really understand what you are trying to accomplish here and I cannot try it out because I don't know what your Could you paste the output from |
The target is to create multiple plots, each from an independent data.frame, of the same structure of course, in one go. Using your ```{r bar}
chartjs(height = "200px") %>%
cjsBar(labels = LETTERS[1:10]) %>%
cjsSeries(data = c(abs(c(rnorm(8))), NA, 0.5), label = "Series 1") %>%
cjsSeries(data = abs(c(rnorm(10))), label = "Series 2") %>%
cjsSeries(data = abs(c(rnorm(10))), label = "Series 3") %>%
cjsLegend()
\``` imagine you have a series of similar data to plot. Say the example above plus another two: ```{r bar-2}
chartjs(height = "200px") %>%
cjsBar(labels = LETTERS[1:10]) %>%
cjsSeries(data = c(abs(c(rnorm(7))), NA, NA, 0.6), label = "Series 1") %>%
cjsSeries(data = abs(c(rnorm(9)), NA), label = "Series 2") %>%
cjsSeries(data = abs(c(rnorm(10))), label = "Series 3") %>%
cjsLegend()
\``` ```{r bar-3}
chartjs(height = "200px") %>%
cjsBar(labels = LETTERS[1:10]) %>%
cjsSeries(data = c(abs(c(rnorm(6))), 0.5, 0.6, NA, 0.5), label = "Series 1") %>%
cjsSeries(data = abs(c(rnorm(8)), NA, NA), label = "Series 2") %>%
cjsSeries(data = abs(c(rnorm(9)), 0.7), label = "Series 3") %>%
cjsLegend()
\``` Now, let's say the data (Series 1, Series 2, Series 3) for each of 'bar', 'bar-2', 'bar-3', are stored each in a data.frame. In turn, the data.frames are packed in a list. Well, we have a list of data.frames. How do we loop over these to plot them in one go? To avoid, of course, code repetition and hardcoded stuff. Something like (kind of pseudo-code): ```{r loop}
for (df in list.of.dfs){
LABELS = ... # get plot labels to feed labels
DATA.? = ...# get plot data to feed data, where ? = 1, 2, 3
chartjs(height = "200px") %>%
cjsBar(labels = LABELS) %>%
cjsSeries(data = DATA.1, label = "Series 1") %>%
cjsSeries(data = DATA.2, label = "Series 2") %>%
cjsSeries(data = DATA.3, label = "Series 3") %>%
cjsLegend()
}
\``` I hope this is more clear now. Thanks. ps- not sure how to escape the "closing" three backticks |
Thanks a lot for the detailed writeup ! It is clearer now, I thought you were trying to plot multiple series on the same graph and this does not look so well with polar area charts. This is more of a knitr/rmarkdown interacting with htmlwidgets issue and it seems to be a bit tricky. I'll see what I can find out about this tonight. |
Alright, this issue showed me it is actually pretty simple, try this gist out and let me know if it works for you (it does on my end). |
If it matters, here my # ---- knitr-options ----
library(knitr)
# root
# opts_knit$set(root.dir=normalizePath('../'))
# opts_knit$set(root.dir='../')
# set global chunk options: images will be 7x5 inches
exported_plots_directory <- "plots/"
# settings for figures
knitr::opts_chunk$set(echo=FALSE,
fig.path=exported_plots_directory, dpi=72, dev="png",
fig.width=12, fig.height=12,
out.width='500px', out.height='500px')
#
echo=TRUE
#
options(digits = 4)
#
tidy=TRUE
tidy.opts=list(blank=FALSE, width.cutoff=60)
# the default output hook
hook_output = knit_hooks$get('output')
knit_hooks$set(output = function(x, options) {
if (!is.null(n <- options$out.lines)) {
x = unlist(stringr::str_split(x, '\n'))
if (length(x) > n) {
# truncate the output
x = c(head(x, n), '....\n')
}
x = paste(x, collapse = '\n') # paste first n lines together
}
hook_output(x, options)
})
\``` which I source in my ```{r echo=FALSE, cache=FALSE}
knitr::read_chunk('knitr_options.R')
\``` I don't see, however, how the plot options would be relevant here. Checking your gist right after... |
Done 👍 |
For example, why isn't the following working:
ps- Thanks for the great work.
The text was updated successfully, but these errors were encountered: