简体   繁体   English

错误R Studio使用install.packages行编织HTML

[英]Error R Studio Knit HTML with install.packages line

I am a new user to RStudio, and have encountered an error when using a .rmd file and Knit HTML 我是RStudio的新用户,在使用.rmd文件和Knit HTML时遇到错误

If I have an install.packages line: 如果我有一个install.packages行:

install.packages('ggplot2');
library(ggplot2);

when I click Knit HTML, an error is returned: 当我单击Knit HTML时,会返回错误:

Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror calls: ... withVisible -> eval -> eval -> install.packages -> contrib.url Execution halted contrib.url中的错误(repos,“source”):尝试在不设置镜像调用的情况下使用CRAN:... withVisible - > eval - > eval - > install.packages - > contrib.url执行暂停

I was able to work around this using: 我能够解决这个问题:

if (!require('ggplot2')) 
{
  install.packages('ggplot2');
  library(ggplot2);
}

If I'm writing a .rmd, do I need to use the if (!require( line every time I install a new package? Is there a way to avoid this so I can write install.packages( only? 如果我正在写一个.rmd,我是否需要使用if (!require(每次安装新包时都行?)有没有办法避免这种情况所以我可以编写install.packages(仅限?

I was also getting same error while using the Knit document and I did below things in the R script : 使用Knit文档时我也遇到了同样的错误,我在R脚本中做了以下事情:

  1. Run the command in console to set your default repository : options(repos=structure(c(CRAN="http://cran.r-project.org"))) 在控制台中运行该命令以设置默认存储库:options(repos = structure(c(CRAN =“http://cran.r-project.org”)))

  2. Add the below code in your R studio : options(repos="https://cran.rstudio.com" ) 在R studio中添加以下代码:options(repos =“https://cran.rstudio.com”)

  3. Add the url reference for packages needed, for example : install.packages("pscl", repos = " https://cran.rstudio.com ") 添加所需包的url引用,例如:install.packages(“pscl”,repos =“ https://cran.rstudio.com ”)

You don't need install.package() line everytime. 每次都不需要install.package()行。

Normally you should install packages in console or a separate interactive session or delete that line after installation of that library (here it's ggplot). 通常你应该在控制台或单独的交互式会话中安装软件包,或者在安装该库之后删除该行(这里是ggplot)。

Just use library(ggplot2) 只需使用库(ggplot2)

  library(ggplot2);

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM