How can I remove the prefix (index indicator) [1] in knitr output?

感情迁移 提交于 2019-12-03 11:02:34

Nothing is impossible. Take a look at what can be done with knitr hooks.
Have fun!
Rmarkdown script gist

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

Normal:

```{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

Null prefix

```{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

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]

Can't be done. But I would hesitate to either do it, or just wrap everything in a cat. Why?

Firstly, R users expect to see the [1] label, and its helpful when the vector is longer than one line, or if its a matrix with rows and column labels. Why do you want to do this?

Secondly, now you know why you want to do this, write a function named after the reason you want to do this, that does this, and wrap your outputs in that function. Because I reckon if you want fine control over the output you are pretty soon going to go "Hmmm cat isn't quite cutting it for me, I want to change this and that and this and that" and that's easier if you have wrapped your output in a function you can change. At first it might just be cleanprint=function(...){cat(...)} but then its easier to move on from there by redefining cleanprint than tweaking cat. You might want to use sprintf, or set the number of decimal digits or something.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!