My personal R administration on Windows 10
There is a lot to keep track of to install and maintain R and R adjacent software and configuration files. Here is what my Windows 10 set up looks like, with associated code if available. Your set up might differ from mine, but you can use the associated code to figure it out. 🤗
Item | Code | Location |
---|---|---|
Home | ||
Home drive | Sys.getenv("HOMEDRIVE") | C:/ |
User home directory | Sys.getenv("USERPROFILE"), Sys.getenv("HOMEPATH"), fs::path_home() | C:/Users/pileggis |
R home directory | Sys.getenv("HOME"), Sys.getenv("R_User"), fs::path_home_r() | C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents |
Software | ||
R | R.home(), Sys.getenv("R_HOME") | C:/Program Files/R/R-4.2.0 |
R packages | .libPaths() | C:/Program Files/R/R-4.2.0/library |
RStudio | C:/Program Files/RStudio | |
Rtools | devtools::find_rtools(), devtools::has_devel() | C:/rtools42 |
Git | C:/Program Files/Git | |
Quarto | C:/Program Files/Quarto | |
Configuration | ||
.Renviron | usethis::edit_r_environ() | C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.Renviron |
.Rprofile | usethis::edit_r_profile() | C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.Rprofile |
.gitconfig | usethis::edit_git_config() | C:/Users/pileggis/.gitconfig |
.shrtcts.R | shrtcts::locate_shortcuts_source() | C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.shrtcts.R |
When Travis Gerke tweeted about the breaker of chains keyboard shortcut, I was eager to try it out!
Cheat code for quickly viewing data frames as you build them in #rstats: assign a keyboard shortcut to @MilesMcBain's {breakerofchains} (quick how-to in thread) pic.twitter.com/P9P6hLa7Xx
— King Ranch (@travisgerke) March 14, 2022
However, it was not immediately clear to me where to save the the
shrtcts.R
configuration file. This took a bit of trial and
error to figure out, and led me to document where everything R and R
adjacent is located.
In this post I refer to two packages and two books.
usethis
for helpers that automate
repetitive tasks during project set up and development.
fs
for cross-platform file system
operations.
Happy Git
with R, a manual for integrating Git with R; abbreviated to
Happy Git
for the remainder of this post.
What they Forgot to
Teach You About R, is in progress but still extremely helpful
documentation on best practices when working in R; abbreviated to
WTF
for the remainder of this post.
I am working on a computer supplied by employer with a Windows 10 operating system.
When getting started with a Windows computer, I recommend changing default settings such that:
Display the full path in the title bar
Show hidden files, folders, and drives
Some Windows 🪟 defaults I always change on a new computer💻🧐
— Shannon Pileggi (@PipingHotData) October 19, 2021
Do you have other defaults you change on Windows?
gotta love the switcheroos between hide/display & check/uncheck pic.twitter.com/X1XZ24ltz1
Much of how you work in Windows depends on your level of rights. Fortunately, I was granted administrator privileges on my work laptop, which allows me to install software and save configuration files where I want to.
If you do not have administrator privileges, knowing this can still help you work with IT to complete your set up or find alternate viable locations.
On Mac OS, the “user home directory” and the “R home directory” are the same, but on Windows OS they differ. On Windows, the R home directory tends to be associated with the user’s documents directory, whereas the user home directory is a system level directory.
You can locate your home drive through environment variables. See the
help file for Sys.getenv
and environment variables for more information.
Sys.getenv("HOMEDRIVE")
C:/
These three commands all point to the “R home directory”.
Sys.getenv("HOME")
Sys.getenv("R_User")
fs::path_home_r()
C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents
Here, we find the “user home directory”.
Wrapping the function in fs::path_real()
allows you to
see a consistent full path, rather than the short
file path or any other path specification returned by the
environment variables.
fs::path_real(Sys.getenv("USERPROFILE"))
fs::path_real(Sys.getenv("HOMEPATH"))
fs::path_home()
C:/Users/pileggis
From the R.home()
help file:
The R home directory is the top-level directory of the R installation being run. The R home directory is often referred to as R_HOME, and is the value of an environment variable of that name in an R session.
fs::path_real(R.home())
fs::path_real(Sys.getenv("R_HOME"))
C:/Program Files/R/R-4.2.0
.libPaths()
shows where R is looking for packages in the
current session. If more than one .libPaths()
is present, R
attaches packages from the libraries in the order shown (searches first
library first).
For Windows users:
Under R < 4.2.0
My packages were initially being installed to a default location
in my R home directory (given by fs::path_home_r()
, the
“Documents” directory).
You may have more than one path present, which likely correspond to a directory specific to the user and a directory available to all users.
With R >= 4.2.0 (released 2022-04-22), the default personal
library for Windows is “now a subdirectory of local application data
directory (usually a hidden directory
C:/Users/username/AppData/Local
)” as discussed in the
Jumping Rivers New features in R 4.2.0 post.
These locations may work well for some users, and others may want to
change the default location. I modified my default location to be under
my R installation using the .Renviron
configuration file
(demonstrated below).
"C:/Program Files/R/R-4.2.0/library"
Lastly, if you are using the renv
package for
reproducible environments, all utilized packages are installed in global package cache which is shared across
all projects. My cache is located at
C:/Users/pileggis/AppData/Local/renv
.
Rtools is a collection of tools required to build source packages on Windows. Basically, if you want to do anything with packages beyond CRAN (install from github, build locally) on Windows, you need R tools.
In order to work, R tools must be installed at
"C:/rtools42"
which is where mine is, too. (rtools 42
is required for
R 4.2.0.)
You can confirm if your installation worked properly if
devtools::find_rtools()
returns TRUE
. You can further confirm readiness with
devtools::has_devel()
which returns
Your system is ready to build packages!
As recommended in Happy Git Ch 6.2 Install Git on Windows, git is installed at
C:/Program Files/Git
.
Quarto is an open source publishing system backed by RStudio that began to receive public endorsement in April 2022.
The Quarto installation allowed me to choose between a user specific versus an all-user installation, which can be very helpful depending on your administrative privileges. I chose the latter, and for me Quarto is installed at
C:/Program Files/Quarto
.
.Renviron
The .Renviron
file contains environment variables that
are set in R Sessions (this does not contain R code).
To edit this file, submit:
usethis::edit_r_environ()
The user-level .Renviron
file lives in the base of the
user’s home directory. For me, that means
C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.Renviron
My .Renviron
file has has been modified to establish
default library locations for R package installations.
R_LIBS_USER = "C:/Program Files/R/library/%v"
As explained in WTF
Ch 8.4 How to transfer your library when updating R, the
%v
wildcard automatically adjusts the installation folder
when you update to a new R version.
As stated in WTF Ch 7.2 .Rprofile
The
.Rprofile
file contains R code to be run when R starts up. It is run after the.Renviron
file is sourced.
Again, you can edit this file with
usethis::edit_r_profile()
and mine lives at
C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.Rprofile
.
My .Rprofile
has been modified two ways:
Establish a default location for R projects created via
usethis::create_from_github()
or
usethis::use_course()
.
To enable RStudio shortcuts with the shrtcts package.
# ------------------------------------------------------------------------------
# 1/ Establish a default location for R projects created via
# `usethis::create_from_github()` or `usethis::use_course()`
options(usethis.destdir = "C:/Users/pileggis/Documents/gh-personal")
# ------------------------------------------------------------------------------
# 2/ enable create RStudio shortcuts with the shrtcts package
if (interactive() && requireNamespace("shrtcts", quietly = TRUE)) {
shrtcts::add_rstudio_shortcuts(set_keyboard_shortcuts = TRUE)
}
You can open your .gitconfig
file for editing with
usethis::edit_git_config()
My .gitconfig
file lives at:
"C:/Users/pileggis/.gitconfig"
and has been modified to allow me to switch between work and personal github identities when working on R projects, which is described in more detail in GitHub ssh and config with multiple accounts by Travis Gerke.
The .shrtcts.R
file contains R code that defines the
enabled shortcuts. Mine is located in my R home directory at
shrtcts::locate_shortcuts_source()
"C:/Users/pileggis/OneDrive - Memorial Sloan Kettering Cancer Center/Documents/.shrtcts.R"
I currently have two shortcuts enabled:
breaker of chains (mentioned in Background), and
automatically end comments with dashes to 80 characters - see this tweet for details.
This topic probably isn’t exciting for many, but having this understanding can save you time during installation and troubleshooting. And maybe next I get set up on a new OS it will go quicker. 😊
Thanks to Travis Gerke for inspiring me to try new things in R! Thanks to Omar Chua, who provided hours of IT support as I was waiting for administrative privileges. And thank you to Jenny Bryan and David Aja for their feedback on this post.
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Pileggi (2022, June 2). PIPING HOT DATA: Locating R and R Adjacent Software and Configuration Files. Retrieved from https://www.pipinghotdata.com/posts/2022-06-02-locating-r-and-r-adjacent-software-and-configuration-files/
BibTeX citation
@misc{pileggi2022locating, author = {Pileggi, Shannon}, title = {PIPING HOT DATA: Locating R and R Adjacent Software and Configuration Files}, url = {https://www.pipinghotdata.com/posts/2022-06-02-locating-r-and-r-adjacent-software-and-configuration-files/}, year = {2022} }