Debugging R#

See below, or our wiki.

Library installation problems#

You are not allowed to install packages in C: because you do not have administrator rights.

This will happen on computers where you are a guest (not the owner), for instance CCSS Cloud or RedCloud.

Solution:#

Run the following in an R console (or the console in Rstudio):

dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)  # create personal library

Then restart R, and try the install again.

Using different versions of R#

For Linux, see R on Linux.

For Windows and MacOS, when using Rstudio, see Rstudio help pages.

Pathnames#

Use single forward slashes everywhere (source):

For a Windows path that would read F:\\rschfs1x\userRS\F-J\XXXX_RS\Documents\Workspace\app.20160004, use

lest<-read.dta("F:/rschfs1x/userRS/F-J/XXXX_RS/Documents/Workspace/app.20160004/ddd_long.dta")

Even better, use the file.path() function:

rootdir <- "F:/rschfs1x/userRS/F-J/XXXX_RS/Documents/Workspace/app.20160004"
lest    <- read.dta(file.path(rootdir,"ddd_long.dta")

Even better better, use the here() function used in the config.R (see notes in the file on how to create the .here file).

rootdir <- here()
lest    <- read.dta(file.path(rootdir,"ddd_long.dta")

Package Dependency - if you want to use an earlier version of a package#

The trick is to use the COPY of the CRAN repository at Microsoft, called the MRAN. Posit/Rstudio, called the “Posit Package Manager”.

At the top of any R code add the following lines:

ppm.date <- "2019-09-01"
ppm.url  <- "https://packagemanager.posit.co/cran/"
options(repos=paste0(ppm.url,mran.date,"/"))

Note

Even better, the config.R already has lines like this, use them!

You should use a judicious “ppm.date”, probably based on the date JUST prior to the release of the next version of R. You can look up those dates here.

So for instance, if you want the last MRAN date prior to the release of R 4.0, you might choose “2020-03-15” (mid-March, prior to the release of R 4.0). At that time, the latest R version was R 3.6.3.

You should then be able to install packages in a version that is compatible with R 3.6.3 from the MRAN.

All package install functions in R should work “as-is”, unless they explicitly download github versions or similar (”remotes::install_github()” or similar).

Special note for BioHPC#

On BioHPC, which runs a Linux version called “Rocky 9.0”, you should use the following instead:

ppm.date <- "2019-09-01"
ppm.url  <- "https://packagemanager.posit.co/cran/__linux__/rhel9/"
options(repos=paste0(ppm.url,ppm.date,"/"))

This will (hopefully) install pre-compiled packages, rather than trying to compile every package from source, which is a LOT faster.

trying to use CRAN without setting a mirror#

For example,

install.packages("reshape2")
Installing package into 'C:/Users/Mbrown_RS/AppData/Local/R/win-library/4.2'
(as 'lib' is unspecified)
Error in contrib.url(repos, "source") :
  trying to use CRAN without setting a mirror

If you get this error, you need to set a CRAN repository within R. The best way is to set it globally, once and for all (as per this note. You can do this by manually editing the .Rprofile file, or simply running the following command:

Note

Even better, this is already configured in the config.R - use it!

echo '# set the default repository
local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cran.rstudio.com/"
  options(repos = r)
})
# create the local library if it does not exist
if ( !file.exists(Sys.getenv("R_LIBS_USER") ) ) {
   dir.create(Sys.getenv("R_LIBS_USER"),recursive=TRUE)
} ' > $HOME/.Rprofile

but see about the correct setting for r["CRAN"] here