Configuring R/RStudio
Dec 22, 2019, updated Jul 25, 2021
R
Install a couple packages
The packages devtools
and usethis
will be two of the most important things you use when you start to develop your own packages. Install them:
install.packages("devtools")
install.packages("usethis")
Configure R to work well with git
After installing usethis
, introduce your copy of R to git. Do this by replacing my details with your name and email address in the line below and then run the code.
usethis::use_git_config(user.name = "Raymond R. Balise", user.email = "balise@miami.edu")
Setup your profile file
Type usethis::edit_r_profile()
to have R open your .Rprofile file. The code that you add into your .Rprofile file is run every time RStudio starts.
Add a couple packages to the configure file.
While you may be tempted to add a pile of packages, which you use every day, into your profile, don’t do it. If you rely on your profile file to automatically load packages your analysis code may not work when your main machine is in the shop and you try to run your analysis files on a new machine. The functions in the packages I mentioned above, devtools and usethis, are not normally saved into permanent programs. So, it is safe to add them to your configuration. Paste this code into your .Rprofile file and save it:
if (interactive()) {
suppressMessages(require(devtools))
suppressMessages(require(usethis))
}
Set options to tell R who you are
You can also add a few options to your profile. Replace my name, email and ORCID (if you have one) with your own information:
options(
usethis.full_name = "Raymond R. Balise",
usethis.description = list(
`Authors@R` = 'person(given = "Raymond",
family = "Balise",
role = c("aut", "cre"),
email = "balise@miami.edu",
comment = c(ORCID = "0000-0002-9856-5901"))'
)
)
If you are not a researcher you probably will not have an ORCID. You can erase the entire comment line but be sure to replace the comma at the end of the email line with the closing single quote ’ that is currently at the end of the comment line.
Set options to tell R to warn you about partial matching
As you work in R Studio it will prompt you to help autocomplete with the full name of things like functions and arguments. If you leave a partial match R will by default attempt to autocomplete. The wrong partial match, when you don’t realize there was a partial match can be a nightmare to debug. So, add one more block of code to you .Rprofile to tell R to warn you when it is using a partial match.
options(
warnPartialMatchArgs = TRUE,
warnPartialMatchDollar = TRUE,
warnPartialMatchAttr = TRUE
)
With these options set, once in a while, a package written by somebody else will throw a warning because the author didn’t type the full name of a function argument. For example, the author may have in their code a line like exportRecords(date=TRUE)
when they should have typed the s as part of the dates argument name exportRecords(dates=TRUE)
. You can ignore the warning but it is good form to send the author a note and mention that they forgot the last letter.
Install TeX/LaTeX
TeX/LaTeX is used to make PDF documents. You can install TeX before you install R/RStudio and RStudio will usually find it. A typical TeX install is huge. A great alternative is to install TeX using an R package called tinytex
. Run these to lines in the R Console and R will be able to save results into a PDF:
install.packages('tinytex')
tinytex::install_tinytex()