本文介绍了R knitr Markdown:For 循环内的输出图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自动 knitr 报告,该报告将为我的数据框中的每个数字字段生成直方图.我的目标是无需指定实际字段即可执行此操作(此数据集包含 70 多个,我还想重用该脚本).

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).

我尝试了几种不同的方法:

I've tried a few different approaches:

  • 将绘图保存到一个对象,p,然后在循环后调用p
    • 这只绘制了最终的情节
    • 在循环之外访问这些图根本不起作用

    我担心情节设备的复杂性正在逃避我.

    I'm afraid the intricacies of the plot devices are escaping me.

    如何将循环中的每个图块输出到报告中?目前,我能实现的最好结果是通过将其保存到一个对象并在循环外调用该对象而生成的最终图的输出.

    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.

    在 RStudio 中使用 knitr 的 R markdown 块:

    R markdown chunk using knitr in RStudio:

    ```{r plotNumeric, echo=TRUE, fig.height=3}
    suppressPackageStartupMessages(library(ggplot2))
    FIELDS <- names(df)[sapply(df, class)=="numeric"]
    for (field in  FIELDS){
      qplot(df[,field], main=field)
    }
    ```
    

    从这一点来看,我希望进一步定制情节.

    From this point, I hope to customize the plots further.

    推荐答案

    qplot 包裹在 print 中.

    knitr 会为你做这件事,但是(至少我安装的版本)在循环内没有检测到这个(这与 R 命令行的行为一致).

    knitr will do that for you if the qplot is outside a loop, but (at least the version I have installed) doesn't detect this inside the loop (which is consistent with the behaviour of the R command line).

    这篇关于R knitr Markdown:For 循环内的输出图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:38