Silently source R script with optional message and warning suppression
Source:R/general_source_silent.R
source_silent.Rd
Sources an R script file with options to suppress messages and/or warnings. Useful for running scripts that generate unwanted console output.
Arguments
- file
Character. Path to the R script file to be sourced.
- messages
Logical. If
TRUE
(default), messages are shown. IfFALSE
, messages are suppressed.- warnings
Logical. If
TRUE
(default), warnings are shown. IfFALSE
, warnings are suppressed.- ...
Additional arguments passed to
base::source()
.
Examples
# Create a temporary R script
script_file <- tempfile(fileext = ".R")
writeLines(
c("message('This is a message')", "warning('This is a warning')",
"print('Output')"),
script_file)
# -------------------------------------------
# source with default settings (show messages and warnings)
source_silent(script_file)
#> This is a message
#> Warning: This is a warning
# suppress messages only
source_silent(script_file, messages = FALSE)
#> Warning: This is a warning
# suppress warnings only
source_silent(script_file, warnings = FALSE)
#> This is a message
# suppress both messages and warnings
source_silent(script_file, messages = FALSE, warnings = FALSE)
# clean up
fs::file_delete(script_file)