How to show code but hide output in RMarkdown?

前端 未结 6 545
生来不讨喜
生来不讨喜 2020-12-13 01:58

I want my html file to show the code, but not the output of this chunk:

```{r echo=True, include=FALSE}
fun <- function(b)
    {
    for(a in b)
        {         


        
相关标签:
6条回答
  • 2020-12-13 02:23

    As @ J_F answered in the comments, using {r echo = T, results = 'hide'}.

    I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!

    You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.

    Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = T,
                          results = "hide")
    ```
    

    Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.

    ```{r analysis, results="markup"}
    # code here
    ```
    
    0 讨论(0)
  • 2020-12-13 02:26

    For muting library("name_of_library") codes, meanly just showing the codes, {r loadlib, echo=T, results='hide', message=F, warning=F} is great. And imho a better way than library(package, warn.conflicts=F, quietly=T)

    0 讨论(0)
  • 2020-12-13 02:39

    To hide warnings, you can also do {r, warning=FALSE}

    0 讨论(0)
  • 2020-12-13 02:40

    For what it's worth.

    ```{r eval=FALSE}
    

    The document will display the code by default but will prevent the code block from being executed, and thus will also not display any results.

    0 讨论(0)
  • 2020-12-13 02:46

    For completely silencing the output, here what works for me

    ```{r error=FALSE, warning=FALSE, message=FALSE}
    invisible({capture.output({
    
    
    # Your code here
    2 * 2
    # etc etc
    
    
    })})
    ```
    

    The 5 measures used above are

    1. error = FALSE
    2. warning = FALSE
    3. message = FALSE
    4. invisible()
    5. capture.output()
    0 讨论(0)
  • 2020-12-13 02:49

    The results = 'hide' option doesn't prevent other messages to be printed. To hide them, the following options are useful:

    • {r, error=FALSE}
    • {r, warning=FALSE}
    • {r, message=FALSE}

    In every case, the corresponding warning, error or message will be printed to the console instead.

    0 讨论(0)
提交回复
热议问题