如何在 Rmarkdown 中“装箱”“summary()”的输出

2023-12-22

我看过这个帖子:如何在 R 代码块内创建 R-markdown 部分?具有正确的代码显示 https://stackoverflow.com/questions/47414027/how-to-create-r-markdown-sections-inside-a-r-code-chunk-with-proper-code-displa

其中一位响应者塞德里克(Cedric)对他或她对输出进行装箱的方式给我留下了深刻的印象summary()R 中的命令

据我了解,用户使用 Sweave 创造了如此出色的输出。 我很好奇是否有一种方法可以使用 Knit 以类似的方式输出到 PDF 文件。

谢谢你!


Sweave 与 R Markdown 没有什么特别之处。您可以复制并粘贴 LaTeX 代码您链接到的塞德里克的回答 https://stackoverflow.com/a/47617773/8386140到一个文件中example.sty并使用以下 R Markdown 文件

---
title: "Stack Overflow Answer"
author: "duckmayr"
header-includes:
    - \usepackage{example}
output: pdf_document
---


```{r boxed-summary, results='asis'}
for (i in 1:10) {
  cat("\\section{Part:", i, "}")
  cat("\\begin{lstlisting}")
  print(summary(lm(data=mtcars, mtcars[,1]~ mtcars[,i])))
  cat("\\end{lstlisting}")
  cat(paste0("$\\\\alpha$ = ", mtcars[1,i]))  
}
```

创造

或者,您可以将 LaTeX 放入您正在编辑的 R Markdown 文档中:

---
title: "Stack Overflow Answer"
author: "duckmayr"
header-includes:
    - \usepackage{listings}
    - \usepackage[usename,dvipsnames]{xcolor}
    - \definecolor{mygreen}{rgb}{0,0.6,0} 
    - \definecolor{mygray}{rgb}{0.5,0.5,0.5}
    - \definecolor{mymauve}{rgb}{0.58,0,0.82}
output: pdf_document
---

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\footnotesize\ttfamily, % the size of the fonts that are used for the
  % code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},      % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=R,                       % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray},   % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it is 1, each line will be numbered
  stringstyle=\color{mymauve},      % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

```{r boxed-summary, results='asis'}
for (i in 1:10) {
  cat("\\section{Part:", i, "}")
  cat("\\begin{lstlisting}")
  print(summary(lm(data=mtcars, mtcars[,1]~ mtcars[,i])))
  cat("\\end{lstlisting}")
  cat(paste0("$\\\\alpha$ = ", mtcars[1,i]))  
}
```

这会产生相同的输出。

归属说明

我想在这里再次强调,我没有想出创建盒装列表的 LaTeX 代码;塞德里克就是按照上面提到的那样做的。我只是在这里为 OP 演示如何在 R Markdown 文档中使用该代码,而不是通过 Sweave,如链接答案中演示的那样。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rmarkdown 中“装箱”“summary()”的输出 的相关文章

  • 计算一列中正数和负数的数量

    我想计算数据帧的一列中正值和负值的数量 我在 R 中该怎么做 例如 这里是数据框 logFC logCPM LR PValue FDR Bra15066 5 630822 5 184586 73 79927 8 647868e 18 4 0
  • R 中带有文件名的 For 循环

    我有一个文件列表 例如 nE pT sbj01 e2 2 csv nE pT sbj02 e2 2 csv nE pT sbj04 e2 2 csv nE pT sbj05 e2 2 csv nE pT sbj09 e2 2 csv nE
  • 使用 R Shiny 从 XLConnect 下载 Excel 文件

    有没有人尝试过使用 R Shiny 中的下载处理程序通过 XLConnect 下载新创建的 Excel 文件 在 ui R 中有一行不起眼的行 downloadButton downloadData Download 在 server R
  • decompose() 的周期太少[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 错误看起来像这样 decompose
  • 从 R 中的向量中选择所有可能的元组

    我正在尝试用 R 编写一个程序 当给定一个向量时 将返回所有可能的tuples http en wikipedia org wiki Tuples该向量中的元素 例如 元组 c a b c c a b c 出租车 c a c c b c c
  • 将命名参数列表传递给函数?

    我想编写一个小函数来从适当的分布生成样本 例如 makeSample lt function n dist params values lt makeSample 100 unif list min 0 max 10 values lt m
  • 将 Instagram/youtube 嵌入 Shiny R 应用程序

    我想通过点击图表来播放 Instagram 或 Youtube 视频 例如显示异常值等 到目前为止 明确告诉 Shiny 视频内容是有效的 require shiny require ggplot2 data df lt data fram
  • LDA with topicmodels,如何查看不同文档属于哪些主题?

    我正在使用 topicmodels 包中的 LDA 我已经在大约 30 000 个文档上运行它 获取了 30 个主题 并获得了主题的前 10 个单词 它们看起来非常好 但我想看看哪些文档属于哪个主题的概率最高 我该怎么做 myCorpus
  • 重复测量引导统计数据,按多个因素分组

    我有一个看起来像这样的数据框 但显然还有更多行等 df lt data frame id c 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 cond c A A B B A A B B A A B B A A B B co
  • 正则表达式字符串中第一个和最后一个非点的位置

    我希望找到字符串的第一个和最后一个非点元素的位置 理想情况下我想这样做regex在基地R 我已经写过R解决问题的代码 不过 我对一个感兴趣regex解决方案 感谢您的任何建议 这是一个示例数据集和R代码以获得所需的结果 此代码拆分字符串并使
  • 选择 R 中的数据表中隐藏时(在绿色加号下方)列的显示顺序

    Context 使用 DataTables 库制作交互式表格时 当屏幕宽度对于列的数量和宽度来说太窄时 列将隐藏在绿色 号下 我有一个非常宽的表格 有 20 多列 其中一些内容非常冗长 因此某些列在所有屏幕宽度下总是隐藏的 每次隐藏新列时
  • 通过间接引用列来修改数据框中的某些值

    我正在整理一些数据 我们将失败的数据分类到垃圾箱中 并按批次计算每个分类箱的有限产量 我有一个描述排序箱的元表 这些行按升序测试顺序排列 一些排序标签带有非语法名称 sort tbl lt tibble tribble weight lab
  • 尝试读取 CSV 文件时出现“无法识别的字符串转义”

    我正在尝试导入一个 csv文件 以便我可以观看此视频 R ggplot2 图形直方图 http www youtube com watch v 47kWynt3b6M 我安装了所有正确的软件包 包括ggplot以及相关的包 视频中的第一个说
  • R独特的列或行与NA无可比拟

    有谁知道如果incomparables的论证unique or duplicated 曾经被实施过incomparables FALSE 也许我不明白它应该如何工作 无论如何 我正在寻找一个巧妙的解决方案 以仅保留与另一列相同的唯一列 或行
  • Purrr::map_df() 删除 NULL 行

    使用时purrr map df 我偶尔会传递一个数据框列表 其中一些项目是NULL 当我做 map df 返回行数少于原始列表的数据框 我想发生的事情是这样的map df calls dplyr bind rows 它忽略了NULL价值观
  • 使用 R 选择第一个非 NA 值

    df lt data frame ID c 1 1 1 2 3 3 3 test c NA 5 5 6 4 NA 7 3 NA 10 9 我想创建一个名为 value 的变量 它是每个单独 ID 测试的第一个非 NA 值 对于只有NA的个体
  • R 中的列乘以子字符串

    假设我有一个数据框 其中包含多个组件及其在多个列中列出的属性 并且我想对这些列运行多个函数 我的方法是尝试将其基于每个列标题中的子字符串 但我无法弄清楚如何做到这一点 下面是数据框的示例 Basket F Type 1 F Qty 1 F
  • ddply 和aggregate 之间的区别

    有人可以通过以下示例帮助我了解聚合和 ddply 之间的区别 数据框 mydat lt data frame first rpois 10 10 second rpois 10 10 third rpois 10 10 group c re
  • 在 r 中的 group_by 之后建模后取消列表列的嵌套

    我想对所有组进行线性回归group by 将模型系数保存在列表列中 然后使用 unnest 扩展列表列 这里我用的是mtcars以数据集为例 注 我想用do here becausebroom tidy 不适用于所有型号 mtcars gt
  • 文本挖掘 pdf 文件/词频问题

    我正在尝试挖掘一篇具有丰富 pdf 编码和图表的文章的 pdf 我注意到 当我挖掘一些 pdf 文档时 我得到的高频词是 phi taeoe toe sigma gamma 等 它与某些 pdf 文档配合良好 但与其他文档配合使用时却得到这

随机推荐