我有一个foo.R文件,其中包含
library("ggplot2")
cat("Its working")我试图使用Rscript命令Rscript --default-packages=ggplot2 foo.R通过命令行运行foo.r,这给了我以下错误:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘ggplot2’
2: package ‘ggplot2’ in options("defaultPackages") was not found 
Error in library("ggplot2") : there is no package called ‘ggplot2’
Execution halted任何关于如何在运行"Rscript“时加载包的帮助都是非常感谢的。
发布于 2015-06-22 07:52:11
对于以后的引用,您可以使用函数require而不是library来避免此错误:如果没有安装包而不是抛出错误,require只会返回FALSE并引发警告。因此,您可以按以下方式进行构造:
if(!require(ggplot2)){install.packages("ggplot2")}它所做的是尝试加载软件包,如果没有安装,则安装它。
发布于 2018-04-25 17:34:33
或者你可以用这个,
# --------- Helper Functions ------------ #
# Ref: https://gist.github.com/smithdanielle/9913897
# check.packages function: install and load multiple R packages.
# Check to see if packages are installed. Install them if they are not, then load them into the R session.
check.packages <- function (pkg) {
  print("Installing required packages, please wait...")
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) {
    install.packages(new.pkg, dependencies = TRUE)
  }
  sapply(pkg, library, character.only = TRUE)
}
# Usage example
# packages<-c("ggplot2", "afex", "ez", "Hmisc", "pander", "plyr")
# check.packages(packages)
check.packages("tidyverse")https://stackoverflow.com/questions/30893829
复制相似问题