15

Standard R output looks like this

> 3
[1] 3

To remove the prefix 1 you can use

> cat(3)
3

Is there a way to remove this globally? Or do you have to wrap cat() around everything?

Further to that, I'm using this within knitr, so if there isn't an R global setting, there may be a knitr wide setting, I did look, but couldn't see one.

Edit: 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).

enter image description here

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.

You would get:

There are 7 cars with 6 cylinders.

As your output, not:

There are [1] 7 cars with 6 cylinders.

joran
  • 169,992
  • 32
  • 429
  • 468
nzcoops
  • 9,132
  • 8
  • 41
  • 52
  • What platform are you on? R, RStudio? Version? Are you in Linux or Windows, Mac? – Rich Scriven Mar 20 '14 at 07:12
  • I'm using R(3.0.2) in RStudio (0.98.501) on a Mac (10.9.1). I know on a Mac you get the [1] whether you're using RStudio, R (in R) or R (in terminal). Looks to be the same in Linux terminal too. – nzcoops Mar 20 '14 at 08:03
  • Kintr has [hooks](http://yihui.name/knitr/hooks). Instead using `cat`, I would suggest giving a try to the `pander` method in my `pander` package, that returns the markdown representation of the R objects. See more details e.g. at http://stackoverflow.com/questions/15599948/replacing-the-print-function-in-knitr-chunk-evaluation/15600673#15600673 – daroczig Mar 20 '14 at 08:11
  • Thanks @Thell, not a duplicate though, as there is no solution in there to globally remove the [1] from output, only via a wrapper function, and that was focused on copying the output, this is for use within reproducible research etc. Feel free to remove your comment. – nzcoops Mar 21 '14 at 00:51
  • @daroczig, thanks. See my (new) additional info in the question. I'm guessing that is the result of hook settings, so I'll have a look down that path. – nzcoops Mar 21 '14 at 00:52

2 Answers2

29

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]
Thell
  • 5,883
  • 31
  • 55
  • Brilliant, thanks @Thell. Clearly not a trivial solution, but great that you wrote something that can literally be dropped in. – nzcoops Mar 25 '14 at 01:01
  • Great solution! Any tips on what can also be done to remove the quotation marks around outputted strings (e.g., "my string")? Looking to implement a `quote=NA` option. – Clokman Nov 25 '22 at 09:14
  • Not that I can think of off the top of my head yet I am confident it can be done. – Thell Nov 26 '22 at 15:44
-2

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 2
    Thanks for the feedback @Spacedman! I've edited the question to add a why. But from your first sentence I'm guessing if it can't be done, the why becomes a bit irrelevant :) – nzcoops Mar 21 '14 at 00:20
  • 5
    We don't always write for R users. – Neil Sep 16 '17 at 15:26