-
Notifications
You must be signed in to change notification settings - Fork 2
R tips
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).
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.
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")
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 - or, even better, at the top of the "config.R" - add the following lines:
mran.date <- "2019-09-01"
mran.url <- "https://packagemanager.posit.co/cran/"
options(repos=paste0(mran.url,mran.date,"/"))
You should use a judicious "mran.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).
On BioHPC, which runs a Linux version called "Rocky 9.0", you should use the following instead:
mran.date <- "2019-09-01"
mran.url <- "https://packagemanager.posit.co/cran/__linux__/rhel9/"
options(repos=paste0(mran.url,mran.date,"/"))
This will (hopefully) install pre-compiled packages, rather than trying to compile every package from source, which is a LOT faster.
pwd
Suppose that you would like to run a R script called "test.R" from the command line in CISER, and that you would like to create a log file, "test.Rout", for your run. Then in your shell (e.g. Git Bash), enter the following:
pwd
"C:\Program Files\R\R-4.2.0\bin\x64\R.exe" CMD BATCH "U:\AEAworkspace\test\test.R" "U:\AEAworkspace\test\test.Rout"
Note: We've added code to display your present working directory prior to running any code. This will help us debug any issues with running R on the command line.
Here, "U:\AEAworkspace\test\test.R
" and "U:\AEAworkspace\test\test.Rout"
should be replaced with your file paths to "test.R" and "test.Rout", respectively.
Alternatively, you can use relative paths to the script and log files. Please be sure to right-click and open your shell from the directory the script is located in. For instance, if you opened the Git Bash shell in the directory U:\AEAworkspace\test\
, you can run
pwd
"C:\Program Files\R\R-4.2.0\bin\x64\R.exe" CMD BATCH test.R
Pro Tip
If you use the Terminal in the RStudio interface, you do not need to prepend the entire R path! Depending on the terminal showing (CMD.exe or Bash), you should be able to run one of the following short variants:
Powershell or CMD.exe:
R.exe CMD BATCH test.R
Bash:
R CMD BATCH test.R
Note: If you do not specify the name of the log file (e.g. by omitting "U:\AEAworkspace\test\test.Rout"), a log file will still be produced once the R script stops running. This log file will be a .Rout file located in your present working directory. However, if the R script is running, or if it has run into an error, you will need to manually quit R from the command line in order to produce this automatic log file. You can do this by typing the following in your shell:
q()
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:
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
-
Training
-
Tips for authors
-
Tips for replicators
-
Questionnaires
-
Definitions
-
Generic workflow
-
Post-publication replications
-
Technical issues
-
Appendix