首页 > 解决方案 > 在命令行中加载库时如何使 R 静音?

问题描述

我在 mac os 11.2 上运行 R 4.0.2

如果我在 R Studio 中调用以下库,这一切都会按预期进行

requiredPackages = c('dplyr','data.table',
                     'stringr','tools',
                     'lubridate','docopt',
                     'geosphere','scales',
                     'rgdal','tidyverse',
                     'stringi')
for(p in requiredPackages){
  if(!require(p,character.only = TRUE)) install.packages(p)
  suppressWarnings(suppressMessages(require(p,character.only = TRUE)))
}

但是当我在命令行 R 脚本中复制这些行时

script.R:

#!/usr/bin/env Rscript
requiredPackages = c('dplyr','data.table',
                     'stringr','tools',
                     'lubridate','docopt',
                     'geosphere','scales',
                     'rgdal','tidyverse',
                     'stringi')
for(p in requiredPackages){
  if(!require(p,character.only = TRUE)) install.packages(p)
  suppressWarnings(suppressMessages(require(p,character.only = TRUE)))
}

并运行

./script.R

我回来了所有的冗长

    Loading required package: dplyr

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Loading required package: data.table

Attaching package: ‘data.table’

The following objects are masked from ‘package:dplyr’:

    between, first, last

Loading required package: stringr
Loading required package: tools
Loading required package: lubridate

Attaching package: ‘lubridate’

The following objects are masked from ‘package:data.table’:

    hour, isoweek, mday, minute, month, quarter, second, wday, week, yday, year

The following objects are masked from ‘package:base’:

    date, intersect, setdiff, union

Loading required package: docopt
Loading required package: geosphere
Loading required package: scales
Loading required package: rgdal
Loading required package: sp
rgdal: version: 1.5-23, (SVN revision 1121)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.1.4, released 2020/10/20
Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
GDAL binary built with GEOS: TRUE 
Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
Linking to sp version:1.4-5
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
Loading required package: tidyverse
── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✔ ggplot2 3.3.2     ✔ readr   1.3.1
✔ tibble  3.0.2     ✔ purrr   0.3.4
✔ tidyr   1.1.0     ✔ forcats 0.5.0
── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ lubridate::as.difftime() masks base::as.difftime()
✖ data.table::between()    masks dplyr::between()
✖ readr::col_factor()      masks scales::col_factor()
✖ lubridate::date()        masks base::date()
✖ purrr::discard()         masks scales::discard()
✖ dplyr::filter()          masks stats::filter()
✖ data.table::first()      masks dplyr::first()
✖ lubridate::hour()        masks data.table::hour()
✖ lubridate::intersect()   masks base::intersect()
✖ lubridate::isoweek()     masks data.table::isoweek()
✖ dplyr::lag()             masks stats::lag()
✖ data.table::last()       masks dplyr::last()
✖ lubridate::mday()        masks data.table::mday()
✖ lubridate::minute()      masks data.table::minute()
✖ lubridate::month()       masks data.table::month()
✖ lubridate::quarter()     masks data.table::quarter()
✖ lubridate::second()      masks data.table::second()
✖ lubridate::setdiff()     masks base::setdiff()
✖ purrr::transpose()       masks data.table::transpose()
✖ lubridate::union()       masks base::union()
✖ lubridate::wday()        masks data.table::wday()
✖ lubridate::week()        masks data.table::week()
✖ lubridate::yday()        masks data.table::yday()
✖ lubridate::year()        masks data.table::year()

有什么方法可以使命令行脚本静默吗?我希望脚本在缺少一些输入参数时说出来,所以我需要摆脱所有这些模糊冗长的内容。

标签: r

解决方案


推荐阅读