【问题标题】:Keeping chunk output format in pdf document same as the console output format - Rmarkdown保持 pdf 文档中的块输出格式与控制台输出格式相同 - Rmarkdown
【发布时间】:2020-04-28 20:30:30
【问题描述】:

以下代码块的输出在呈现的 pdf 文档中被分为 2 个块(图 1 和 2),第一个块包含两个第一列(Estimate 和 Std.Error),另一个包含其余列(t 值和 Pr(>|t|)) 。 控制台输出格式(一个块中的所有列,图 3)如何保存在输出 pdf 文档中?

代码块:

```{r model1}
lm1=lm(formula=Long.Term.Debt~.,data=donnees[,-c(1:3)])
summary(lm1)
```

pdf文档中的渲染输出:

Fig. 1: A first block containing 2 first columns

Fig. 2: Another block containing 2 remaining columns

期望的输出:

Fig. 3: Console output (one block containing all columns)

我尝试增加输出的显示区域并减小字体大小,但没有结果:

减小字体大小:

\tiny
```{r model1}
lm1=lm(formula=Long.Term.Debt~.,data=donnees[,-c(1:3)])
summary(lm1)
```

设置显示区域大小:

```{r model1, fig.width=80, fig.height=8}
lm1=lm(formula=Long.Term.Debt~.,data=donnees[,-c(1:3)])
summary(lm1)
```

提前致谢。

【问题讨论】:

  • fig.width=80, fig.height=8 不会更改代码的字体大小,因为这些选项用于图形输出,而不是您要在 PDF 文件中显示的文本输出。
  • 感谢您的评论@CarlosLuisRivera。实际上,我是在假设代码输出呈现为“某种图形”或“fig.width”和“fig.height”实际上参数化任何输出显示区域的假设下做到这一点的。

标签: r-markdown


【解决方案1】:

您可以使用knitr::kable()kableExtra() 以更优雅的方式显示结果摘要。以下代码的输出被视为表格。所以,如果你使用bookdown,它的表号也会自动生成。

```{r summary}
library(knitr)      # for kable()
library(knitrExtra) # for further customisation of the kable() output 
library(magrittr)   # for the pipe %>%, press "Ctrl/Cmd + Shift + m"

kable(summary(lm1)$coefficient[, 0:4], # by 0:4, you can select the columns from variable names to p-values 
      digits = 2,                      # You can reduce the digit number
      booktabs = TRUE,                 # If false, you will get a lattice-like table
      caption = 'Coefficients (2 not defined because of sigularities)'
      ) %>% 
kable_styling(font_size = 7)           # You can change the font size here
```

【讨论】:

  • 感谢@CarlosLuisRivera 的回答和附加功能。但是,这种方式不会打印系数的重要性星。当然,如果有必要,可以根据 p 值创建这些向量的向量,但我意识到最初的问题是由于数据框的列名过长(至于 Equity...subsidiary)所以重命名列可以解决问题。
  • 我不确定,但您可以将 kable(summary(lm1)$coefficient[, 0:4]) 更改为 kable(summary(lm1)$coefficient[, 0:5])。这会显示星星吗?
  • 我确实试过了(也试过kable(summary(lm1)$coefficients))但它不起作用。通过检查结构 str(summary(lm1))str(summary(lm1)$coefficients) ,重要性星似乎不是摘要或系数对象的属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 2013-11-19
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
相关资源
最近更新 更多