11.12 Remove leading hashes in text output

11.12 Remove leading hashes in text output

By default, R code output will have two hashes ## inserted in front of the text output. We can alter this behavior through the comment chunk option, which defaults to a character string "##". We can use an empty string if we want to remove the hashes. For example:

```{r, comment=""}
1:100

Of course, you can use any other character values, e.g., `comment = "#>"`. Why does the `comment` option default to hashes? That is because `#` indicates comments in R. When the text output is commented out, it will be easier for you to copy all the code from a code chunk in a report and run it by yourself, without worrying about the fact that text output is not R code. For example, in the code chunk below, you can copy all four lines of text and run them safely as R code:

1 + 1

[1] 2

2 + 2

[1] 4


If you remove the hashes via `comment = ""`, it will not be easy for you to run all the code, because if you copy the four lines, you will have to manually remove the second and fourth lines:

1 + 1 [1] 2 2 + 2 [1] 4


One argument in favor of `comment = ""` is that it makes the text output look familiar to R console users. In the R console, you do not see hashes in the beginning of lines of text output. If you want to truly mimic the behavior of the R console, you can actually use `comment = ""` in conjunction with `prompt = TRUE`, e.g.,

1 + 1
if (TRUE) {
  2 + 2
}

The output should look fairly familiar to you if you have ever typed and run code in the R console, since the source code contains the prompt character `>` and the continuation character `+`:

1 + 1 [1] 2 if (TRUE) { + 2 + 2 + } [1] 4 ```