在R Console或RStudio的输出中自动添加注释符号

时间:2017-04-20 09:00:48

标签: r console rstudio

目前我们执行以下操作:

> head(mtcars,n=3)
               mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

有没有办法调整控制台(R或RStudio)以便输出:

> head(mtcars,n=3)
#               mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

1 个答案:

答案 0 :(得分:4)

使用reprex包:

library(reprex)

reprex({head(mtcars, n = 3)}, comment = "#",  venue = "so")
# Rendered reprex is on the clipboard.

结果保存到剪贴板,粘贴后如下所示:

<!-- language-all: lang-r -->
  <br/>

  head(mtcars, n = 3)
#                mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1