Package error when running r code on command line

前端 未结 1 1445
醉话见心
醉话见心 2021-02-01 15:07

I have some code that I run that includes this part:

if (!require(\"yaml\")) {
  install.packages(\"yaml\") 
  library(\"yaml\")
}

When I run i

1条回答
  •  梦如初夏
    2021-02-01 15:26

    RStudio sets a default repository when you call install.packages from within RStudio. When you run the script via the command line, you have to tell R which repository to use (or set a global default repository).

    You can easily fix this problem by telling R to use your favorite repository.

    For example, if you want to use RStudio's package repository, set repos="http://cran.rstudio.com/" inside the install.packages call.

    if (!require("yaml")) {
      install.packages("yaml", repos="http://cran.rstudio.com/") 
      library("yaml")
    }
    

    This should work!

    0 讨论(0)
提交回复
热议问题