问题描述
标准R输出看起来像这样
Standard R output looks like this
> 3
[1] 3
要删除前缀 1 ,您可以使用
To remove the prefix 1 you can use
> cat(3)
3
是否有一种方法可以全局删除?还是您必须将cat()
包裹起来?
Is there a way to remove this globally? Or do you have to wrap cat()
around everything?
此外,我在knitr中使用了此功能,因此,如果没有R全局设置,则可能会有一个knitr宽的设置,我确实看过,但看不到.
有人问为什么要这样做,就像您想要构造一个如下所示的报告那样. [1]
只是不需要的,对R用户(也就是听众)没有任何意义.
It was asked why one would want this, something like if you wanted to structure a report like the below. The [1]
is just not needed and means nothing to none R users (aka, the audience).
其他信息:在knitr中,您可以使用\ Sexpr {}或r ...
来在线评估某些内容,在这种情况下,它不会打印[1]
.例如,如果您有:
Additional Info: In knitr you can use \Sexpr{} or r ...
to evaluate something in line, and in that scenario it doesn't print the [1]
. For example if you had:
There are `r sum(mtcars$cyl==6)` cars with 6 cylinders.
您会得到:
有7辆6缸汽车.
作为您的输出,而不是:
As your output, not:
有[1]
7辆6缸汽车.
There are [1]
7 cars with 6 cylinders.
推荐答案
没有什么是不可能的.看看使用针织钩子可以做什么.
玩得开心!
Nothing is impossible. Take a look at what can be done with knitr hooks.
Have fun!
# A Prefix nulling hook.
# Make sure to keep the default for normal processing.
default_output_hook <- knitr::knit_hooks$get("output")
# Output hooks handle normal R console output.
knitr::knit_hooks$set( output = function(x, options) {
comment <- knitr::opts_current$get("comment")
if( is.na(comment) ) comment <- ""
can_null <- grepl( paste0( comment, "\\s*\\[\\d?\\]" ),
x, perl = TRUE)
do_null <- isTRUE( knitr::opts_current$get("null_prefix") )
if( can_null && do_null ) {
# By default R print output aligns at the right brace.
align_index <- regexpr( "\\]", x )[1] - 1
# Two cases: start or newline
re <- paste0( "^.{", align_index, "}\\]")
rep <- comment
x <- gsub( re, rep, x )
re <- paste0( "\\\n.{", align_index, "}\\]")
rep <- paste0( "\n", comment )
x <- gsub( re, rep, x )
}
default_output_hook( x, options )
})
knitr::opts_template$set("kill_prefix"=list(comment=NA, null_prefix=TRUE))
正常:
```{r}
print( 1:50 )
```
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
空前缀
```{r, null_prefix=TRUE}
print( 1:50 )
```
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## 47 48 49 50
设置为默认值(并删除注释字符串):
Set as default (and remove comment string):
```{r}
knitr::opts_chunk$set(opts.label="kill_prefix")
```
```{r}
print( 1:50 )
```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50
确保我们不会杀死带有[:digit:]模式的字符串.
Ensure we aren't killing strings with [:digit:] patterns.
```{r}
print( paste0( paste0("[", 1:50), "]" ),quote = FALSE)
```
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
[15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28]
[29] [30] [31] [32] [33] [34] [35] [36] [37] [38] [39] [40] [41] [42]
[43] [44] [45] [46] [47] [48] [49] [50]
这篇关于如何删除knitr输出中的前缀(索引指示器)[1]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!