我是RStudio的新用户,在使用.rmd文件和Knit HTML时遇到错误
如果我有一个install.packages行:
install.packages('ggplot2');
library(ggplot2);
当我单击Knit HTML时,会返回错误:
contrib.url中的错误(repos,“source”):尝试在不设置镜像调用的情况下使用CRAN:... withVisible - > eval - > eval - > install.packages - > contrib.url执行暂停
我能够解决这个问题:
if (!require('ggplot2'))
{
install.packages('ggplot2');
library(ggplot2);
}
如果我正在编写.rmd,每次安装新软件包时是否需要使用 if (!require(
行?有没有办法避免这种情况所以我只能写 install.packages(
?
2 回答
使用Knit文档时我也遇到了同样的错误,我在R脚本中做了以下事情:
在控制台中运行命令以设置默认存储库:options(repos = structure(c(CRAN =“http://cran.r-project.org”)))
在R studio中添加以下代码:options(repos =“https://cran.rstudio.com”)
为所需的包添加url引用,例如:install.packages("pscl",repos =“https://cran.rstudio.com”)
每次都不需要
install.package()
行 .通常你应该在控制台或单独的交互式会话中安装软件包,或者在安装该库之后删除该行(这里是ggplot) .
只需使用库(ggplot2)
希望能帮助到你