Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If I have an install.packages line:

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

when I click Knit HTML, an error is returned:

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

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?

like image 502
Kyle Avatar asked Jun 20 '15 23:06

Kyle


People also ask

Why is knit not working in R?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

Why is R not letting me install packages?

Changing the configuration in R Studio to solve install packages issue. Go To Tools -> Global option -> Packages. Then uncheck the option “Use secure download method for HTTP”. For other RStudio issues refer to official Troubleshooting Guide here.

How do you knit in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.


1 Answers

You don't need install.package() line everytime.

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).

Just use library(ggplot2)

  library(ggplot2);

Hope it helps

like image 93
Rahul Saxena Avatar answered Nov 15 '22 20:11

Rahul Saxena