Note that there are some explanatory texts on larger screens.

plurals
  1. POR knitr Markdown: Output Plots within For Loop
    primarykey
    data
    text
    <p>I would like to create an automated knitr report that will produce histograms for each numeric field within my dataframe. My goal is to do this without having to specify the actual fields (this dataset contains over 70 and I would also like to reuse the script). </p> <p>I've tried a few different approaches:</p> <ul> <li>saving the plot to an object, <code>p</code>, and then calling <code>p</code> after the loop <ul> <li>This only plots the final plot</li> </ul></li> <li>Creating an array of plots, <code>PLOTS &lt;- NULL</code>, and appending the plots within the loop <code>PLOTS &lt;- append(PLOTS, p)</code> <ul> <li>Accessing these plots out of the loop did not work at all</li> </ul></li> <li>Even tried saving each to a <code>.png</code> file but would rather not have to deal with the overhead of saving and then re-accessing each file</li> </ul> <p>I'm afraid the intricacies of the plot devices are escaping me. </p> <h2>Question</h2> <p>How can I make the following chunk output each plot within the loop to the report? Currently, the best I can achieve is output of the final plot produced by saving it to an object and calling that object outside of the loop.</p> <p>R markdown chunk using <code>knitr</code> in RStudio:</p> <pre><code>```{r plotNumeric, echo=TRUE, fig.height=3} suppressPackageStartupMessages(library(ggplot2)) FIELDS &lt;- names(df)[sapply(df, class)=="numeric"] for (field in FIELDS){ qplot(df[,field], main=field) } ``` </code></pre> <p>From this point, I hope to customize the plots further. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload