R Studio Error Knit HTML with install.packages line

I am a new RStudio user and encountered an error while using .rmd and Knit HTML file

If I have the install.packages line:

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

when I click the "Knit HTML" button, an error is returned:

Error in contrib.url (repos, "source"): attempt to use CRAN without setting mirror calls: ... withVisible → eval → eval → install.packages → contrib.url Execution is paused

I managed to get around this using:

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

If I write .rmd, I need to use the line if (!require( every time I install a new package? Is there a way to avoid this, so I can write install.packages( only?

+5
source share
2 answers

You do not need the install.package() every time.

Usually you should install packages in a console or a separate interactive session or delete this line after installing this library (here it is ggplot).

Just use the library (ggplot2)

  library(ggplot2); 

Hope this helps

+1
source

I also got the same error when using a Knit document, and I did something below in an R script:

  • Run the command in the console to set the default repository: Options (REPO = structure (with (CRAN = "http://cran.r-project.org")))

  • Add the code below to your R studio: options (repos = "https://cran.rstudio.com")

  • Add a link to the URL for the required packages, for example: install.packages ("pscl", repos = " https://cran.rstudio.com ")

+1
source

All Articles