在命令行上运行r代码时包错误。

时间:2021-02-26 20:38:08

I have some code that I run that includes this part:

我运行的一些代码包括以下部分:

if (!require("yaml")) {
  install.packages("yaml") 
  library("yaml")
}

When I run in it rstudio, everything runs seamlessly and there are no bugs. However, when I try running my code on the command line, I get this error:

当我在rstudio中运行时,一切都是无缝运行的,没有bug。但是,当我尝试在命令行上运行代码时,我得到了这个错误:

$ Rscript.exe file.R
Loading required package: yaml
Installing package(s) into ‘/usr/lib/R/site-library’
(as ‘lib’ is unspecified)
Error in contrib.url(repos, type) :
  trying to use CRAN without setting a mirror
Calls: install.packages -> grep -> contrib.url
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called ‘yaml’
Execution halted

1 个解决方案

#1


52  

RStudio sets a default repository when you call install.packages from within RStudio. When you run the script via the command line, you have to tell R which repository to use (or set a global default repository).

在调用install时,RStudio设置一个默认的存储库。从RStudio内部包。当您通过命令行运行脚本时,您必须告诉R使用哪个存储库(或者设置一个全局默认存储库)。

You can easily fix this problem by telling R to use your favorite repository.

您可以通过告诉R使用您最喜欢的存储库来轻松解决这个问题。

For example, if you want to use RStudio's package repository, set repos="http://cran.rstudio.com/" inside the install.packages call.

例如,如果您想使用RStudio的包存储库,请在安装中设置repos=“http://cran.rstudio.com/”。包的电话。

if (!require("yaml")) {
  install.packages("yaml", repos="http://cran.rstudio.com/") 
  library("yaml")
}

This should work!

这应该工作!

#1


52  

RStudio sets a default repository when you call install.packages from within RStudio. When you run the script via the command line, you have to tell R which repository to use (or set a global default repository).

在调用install时,RStudio设置一个默认的存储库。从RStudio内部包。当您通过命令行运行脚本时,您必须告诉R使用哪个存储库(或者设置一个全局默认存储库)。

You can easily fix this problem by telling R to use your favorite repository.

您可以通过告诉R使用您最喜欢的存储库来轻松解决这个问题。

For example, if you want to use RStudio's package repository, set repos="http://cran.rstudio.com/" inside the install.packages call.

例如,如果您想使用RStudio的包存储库,请在安装中设置repos=“http://cran.rstudio.com/”。包的电话。

if (!require("yaml")) {
  install.packages("yaml", repos="http://cran.rstudio.com/") 
  library("yaml")
}

This should work!

这应该工作!