.Rprofile and .Renviron are files loaded at the start-up of each R session. They can be modified to contain code and variables respectively, so that users do not have to explicitly execute the same commands every single time. These functions can help the users edit .Rprofile and .Renviron files without actually opening them.
Usage
get_startup_path(type = "environ")
read_startup(type = "environ")
remove_startup_line(oldline, type = "environ")
add_startup_line(newline, oldline = newline, type = "environ")
Arguments
- type
character. The type, one of 'environ' or 'profile'.
- oldline
character. Line to remove before adding the new one.
- newline
character. Line to add in the file.
Details
There are multiple locations in which .Rprofile and .Renviron files are stored. This function can only modify the 'user' files. The examples show all possible locations of the startup files. See also Startup.
Argument oldline
is used to remove duplicate lines of code. It can be
specified as a regex to look for complex code expressions and remove them.
The examples show one such instance.
Functions
get_startup_path()
: Returns the .Renviron or .Rprofile file path.read_startup()
: Read the .Renviron or .Rprofile file.remove_startup_line()
: Remove a line of code from the .Renviron or .Rprofile file.add_startup_line()
: Add a line of code from the .Renviron or .Rprofile file.
Examples
if (FALSE) {
# Locations of .Rprofile files in loading order
Sys.getenv("R_PROFILE")
file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site")
Sys.getenv("R_PROFILE_USER")
file.path(getwd(), ".Rprofile")
file.path(Sys.getenv("HOME"), ".Rprofile") # 'user' file
# Locations of .Renviron files in loading order
Sys.getenv("R_ENVIRON")
file.path(Sys.getenv("R_HOME"), "etc", "Renviron.site")
Sys.getenv("R_ENVIRON_USER")
file.path(getwd(), ".Renviron")
file.path(Sys.getenv("HOME"), ".Renviron") # 'user' file
# Get the path
type <- "environ"
get_startup_path(type = type)
# Edit the file
newline1 <- "my_variable=1"
newline2 <- "my_variable=2"
add_startup_line(newline1, type = type)
file_startup_1 <- read_startup(type)
add_startup_line(newline2, oldline = 'my_variable=', type = type)
file_startup_2 <- read_startup(type)
# Use the package usethis to edit the files directly
library(usethis)
edit_r_profile(scope = "user")
edit_r_environ(scope = "user")
}