Error in contrib.url(repos, "source") in R trying to use CRAN without setting a mirror Calls: install.packages -> contrib.url Execution halted

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.