Error R Studio Knit HTML with install.packages line - r

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?

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

I was also getting same error while using the Knit document and I did below things in the R script :
Run the command in console to set your default repository :
options(repos=structure(c(CRAN="http://cran.r-project.org")))
Add the below code in your R studio :
options(repos="https://cran.rstudio.com" )
Add the url reference for packages needed, for example :
install.packages("pscl", repos = "https://cran.rstudio.com")

Related

Error when executing R script from Command prompt

I am trying to run R script that works fine when running in Rstudio in command prompt, the reason why is that later I want to run it in Talend ETL tool.
However when I run it in command I obtain the following error:
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: install.packages -> contrib.url
Execution halted
Any idea how to solve that?
Note: I tried to install the packages contrib.url or contriburl but it is not compatible with my version of R.
Thanks.

New to RStudio, issues with knit to HTML

I'm new to RStudio and programming in general. When I click on knit to HTML I get this error message. How can I fix this?
Quitting from lines 231-248 (lesson3_student.rmd)
Error in contrib.url(repos, "source") :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... withVisible -> eval -> eval -> install.packages -> contrib.url
The error message should be fixed, when you set a mirror as argument of the install.packages function:
install.packages("imputeTS",repos = "https://cloud.r-project.org/")
As mentioned in the comments, you only need to install packages once. So you do not necessarily need to do this in the .Rmd document.
Potential user of your .Rmd document will probably see the library("package") calls at the beginning and install the packages on their own.
If you still want to include the install.packages you can do something like this to load the package and only install if necessary:
if(!require("ridge")) {
install.packages("ridge",repos = "https://cloud.r-project.org/")
}

R Error in contrib.url(repos, "Source) upon trying to install a package, without specifying the repo

Hi all I am working in R trying to run some code using RMarkdown, this is the snippet of code that seems to be giving me a problem.
install.packages("car")
install.packages("bestNormalize")
library(readr)
library(dplyr)
library(leaps)
library(MASS)
library(car)
library(plyr)
library(alr4)
Its pretty standard stuff. I receive the following error when trying to run the code.
Error in contrib.url(repos, "source"):trying to use CRAN without setting a mirror Calls:
<Anonymous> ... withVisible
-> eval -> eval -> install.packages -> contrib.url Execution halted
I recently bought a new laptop and installed R. In my old laptop when I specified an install("package_name") I never received this error. Is there a reason why this is occurring? I have already seen proposed solutions to be specifying the repo in the following way:
install.packages("car", repos="http://cran.us.r-project.org")
I am not content with this solution however as I would not like to specify the repos every time I try to install a package. I thought about perhaps finding some way to set the mirror to a default one but have no idea if this can be done. any ideas?
I have found a solution. I did the following: I located and opened my .RProfile by running the command:
file.edit(file.path("~", ".Rprofile"))
Then I navigated to https://cran.r-project.org and found the mirror that was closest to my location. Then in the .Rprofile I wrote the following command in order to have R studio acknowledge my preference when opened
options(repos=structure(c(CRAN="https:theclosest_mirror_to_yourlocation.com")))
Saving this into the .Rprofile and running the initial code shows that it was successful.

I keep getting a knit error with my markdown file in R

I am receiving this error when trying to knit my R markdown sheet
Error in contrib.url(repos, "source") : trying to use CRAN without setting a mirror calls: ... withVisible -> eval -> eval -> install.packages -> contrib.url Execution halted
Does anyone have any experience with this?
Without the exact code, it is difficult to assess the problem. However, as #NelsonGon writes, this is usually the error from running an installation (install.packages() inside a chunk.
Instead, the library should be installed beforehand and loaded with library(example) inside a chunk to make it available for the markdown environment.

How to fix "yaml.load unused argument" in Rmarkdown

When using R Markdown, R Studio warned me the following message : "Rendering R Markdown documents requires an updated version of the yaml package". So I installed the latest version of the package yaml (2.2.0). But since then, I cannot knit any document in Rmarkdown (even the template or documents that were working just fine before).
I tried downloading older versions of yaml but I get the same message as at the beginning ("requires an updated version").
When I knit a document, I get the following error :
Error in yaml::yaml.load(..., eval.expr = TRUE) :
unused argument (eval.expr = TRUE)
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
I had the unused argument (eval.expr = TRUE) problem after updating RStudio. Somehow it messed up with the R installation and an .Rmd that worked before, stopped working with that error.
What did the trick for me was removing yaml and installing it again.
> remove.packages("yaml")
> install.packages("yaml")
>

Resources