I have some R code that executes perfectly in RStudio, but when I run it in cmd I get the below error.
Installing package into 'C:/Users/Anish/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
I am including my package in my code as install.packages("plyr")
. I have also tried using repos and source inside install.package
function.
Use
install.packages('plyr', repos = "http://cran.us.r-project.org")
The reason is most likely because RStudio configures R so it knows how to to check for system packages and where to download them if not locally available, whereas from the command line your R is likely missing that configuration. Check with R.home(component = "home")
from the command line. In the returned folder look for a file like Rprofile
. In my system the line was commented (sight):
$ grep -i "options(repos" /usr/lib64/R/library/base/R/Rprofile
# options(repos = c(CRAN="@CRAN@"))
I experienced the same error re-running an R markdown document on a computer different from the one I had originally written it. To fix it I explicitly set the repos
option in the first R chuck of the document and then knitr
started to work instead of getting stuck in this error. The error means that your R session is attempting a package installation using the contrib.url package but the R-language options do not tell it where to get packages from.
Here is the line I introduced in the first R chunk of the Rmd document. I got the idea from https://github.com/eddelbuettel/littler/issues/23 .
options(repos = list(CRAN="http://cran.rstudio.com/"))
This would give you a closer behaviour from the command line as from the RStudio environment in terms of package downloads anyway.
I suggest setting the option at the top of the script you are running. Of course, the best practice is to configure the R language to your expectations and share that configuration with your audience-users via the R.profile file, read how to customize R .
I added all the suggestions listed and I'm still getting the same error
Error in contrib.url (repos, "source"): trying to use Cran without setting a mirror Calls: withVisible -> eval -> eval -> install.oackages -> contrib.url Execution halted.
i haved same error but i have fixed it with this video information:
https://youtu.be/SX41BbfKZxA Big thanks to Solutions Cloud
In simple words:
Initially i had this:
install.packages("ggplot2")
library("ggplot2")
install.packages("palmerpenguins")
And i change it by
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
library("ggplot2")
install.packages("palmerpenguins", repos = "http://cran.us.r-project.org")
library("palmerpenguins")
And Voila, i got it
and now i'm freaking happy bros
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.