R - Markdown avoiding package loading messages

前端 未结 4 696
礼貌的吻别
礼貌的吻别 2020-12-02 04:40

I have been using Knitr via R-Studio, and think it is pretty neat. I have a minor issue though. When I source a file in an R-Chunk, the knitr output includes external commen

相关标签:
4条回答
  • 2020-12-02 04:51

    My best solution on R Markdown was to create a code chunk only to load libraries and exclude everything in the chunk.

    {r results='asis', echo=FALSE, include=FALSE,}
    knitr::opts_chunk$set(echo = TRUE, warning=FALSE)
    #formating tables
    library(xtable)
    
    #data wrangling
    library(dplyr)
    
    #text processing
    library(stringi)
    
    0 讨论(0)
  • 2020-12-02 04:57

    You can use include=FALSE to exclude everything in a chunk.

    ```{r include=FALSE}
    source("C:/Rscripts/source.R")
    ```
    

    If you only want to suppress messages, use message=FALSE instead:

    ```{r message=FALSE}
    source("C:/Rscripts/source.R")
    ```
    
    0 讨论(0)
  • 2020-12-02 04:58
    ```{r results='hide', message=FALSE, warning=FALSE}
    library(RJSONIO)
    library(AnotherPackage)
    ```
    

    see Chunk Options in the Knitr docs

    0 讨论(0)
  • 2020-12-02 05:04

    This is an old question, but here's another way to do it.

    You can modify the R code itself instead of the chunk options, by wrapping the source call in suppressPackageStartupMessages(), suppressMessages(), and/or suppressWarnings(). E.g:

    ```{r echo=FALSE}
    suppressWarnings(suppressMessages(suppressPackageStartupMessages({
    source("C:/Rscripts/source.R")
    })
    ```
    

    You can also put those functions around your library() calls inside the "source.R" script.

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