rreprex

Properly using reprex package when generating examples that load libraries


I would like to use a reprex package when generating an answer/question1 that utilises multiple packages. Say I want to provide the following answer

library("gdata")
dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
ls()
mv(from = "dfA", to = paste0("df", "B"))
ls()

If I pass this code directly to reprex the output will be as follows:

library("gdata")
#> gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
#> 
#> gdata: Unable to load perl libaries needed by read.xls()
#> gdata: to support 'XLSX' (Excel 2007+) files.
#> 
#> gdata: Run the function 'installXLSXsupport()'
#> gdata: to automatically download and install the perl
#> gdata: libaries needed to support Excel XLS and XLSX formats.
#> 
#> Attaching package: 'gdata'
#> The following object is masked from 'package:stats':
#> 
#>     nobs
#> The following object is masked from 'package:utils':
#> 
#>     object.size
#> The following object is masked from 'package:base':
#> 
#>     startsWith
dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
ls()
#> [1] "dfA"
mv(from = "dfA", to = paste0("df", "B"))
ls()
#> [1] "dfB"

Created on 2022-03-23 by the reprex package (v2.0.1)

There is a lost of irrelevant info related to the package load. An alternative is to suppress package startup messages:

suppressPackageStartupMessages(library("gdata"))
dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
ls()
#> [1] "dfA"
mv(from = "dfA", to = paste0("df", "B"))
ls()
#> [1] "dfB"

Created on 2022-03-23 by the reprex package (v2.0.1)

The above reprex output is generated via:

library("reprex")
reprex(x = {
    suppressPackageStartupMessages(library("gdata"))
    dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
    ls()
    mv(from = "dfA", to = paste0("df", "B"))
    ls()

}, venue = "so")

Question

Is there a cleaner way to do this? Reprex offers tidyverse_quiet but I'm wondering if there is an intelligent way to "stretch" that argument to apply to multiple packages? In a hypothetical answer that loads multiple packages, I would like to avoid writing suppressPackageStartupMessages a number of times just to suppress unnecessary outputs.

Writing package::function may be onerous when calling multiple functions from various packages.


1For reference, I started thinking on that that after contributing an answer to this question.


Solution

  • Another option to consider is to use knitr chunk options inside the reprex. Obviously, these would apply to all code within the chunk although you could use multiple chunks for more control.

    reprex(x = {
      #+ chunk1, message = FALSE
      library("gdata")
      dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
      ls()
      mv(from = "dfA", to = paste0("df", "B"))
      ls()
      }, venue = "so")
    

    Which gives:

    library("gdata")
    dfA <- data.frame(colA = c(1, 2), colB = c(3, 4))
    ls()
    #> [1] "dfA"
    mv(from = "dfA", to = paste0("df", "B"))
    ls()
    #> [1] "dfB"
    

    Created on 2022-03-23 by the reprex package (v2.0.1)