Hide Print Expression in RMarkdown

Is there a way to hide print statements in RMarkdown? I wrote a function that prints progress on learning an algorithm on console R. Here is an example:

f <- function() {
  print("Some printing")
  return(1)
}

In RMarkdown, I have

```{r, eval = TRUE, results = "show"}
res = f()
print(res)
```

This adds "Some Print" and 1 to the RMarkdown output file. Is there a way to suppress "Some Printing" but keep the function output (here 1)? There are options for warnings, errors and messages, but I cannot find them for print statements. A.

+4
source share
1 answer

If you use messagein your function instead print, you can suppress the message

```{r} 
f <- function() {
    message("Some printing")   # change this line
    return(1) 
}

res <- f()    
print(res)    # original prints both  
```
#> Some printing
#> [1] 1

either explicitly with suppressMessages:

```{r} 
res <- suppressMessages(f())
print(res) 
```
#> [1] 1

or using the message=FALSEchunk option :

```{r, message=FALSE} 
res <- f()
print(res) 
```
#> [1] 1

, . print, ( ) capture.output :

```{r}
f <- function() {
    print("Some printing")
    return(1)
}

trash <- capture.output(res <- f())   
print(res)
```
#> [1] 1

... .

+1

Source: https://habr.com/ru/post/1687163/


All Articles