Skip to content

Commit

Permalink
Fix startup:::rprofile_user()
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed May 8, 2024
1 parent 0f2a576 commit d79c8db
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: startup
Version: 0.21.0-9005
Version: 0.21.0-9006
Title: Friendly R Startup Configuration
Depends: R (>= 2.14.0)
Suggests: commonmark, tools, tcltk
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

* Now `sysinfo()` report also on Visual Studio Code (VSCode) webR via
flags `vscode` and `webr`.


* Now `install()` and `uninstall()` respect environment variable
`R_PROFILE_USER`, if specified.


# Version 0.21.0 (2023-12-11)

Expand Down
27 changes: 20 additions & 7 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#' startup directories by appending / removing one line of code to the
#' \file{~/.Rprofile} file.
#'
#' @param path The path where to create / update the \file{.Rprofile} file.
#' @param file The pathname where to create or update the \file{.Rprofile}
#' file.
#'
#' @param backup If `TRUE`, a timestamped backup copy of the original file is
#' created before modifying / overwriting it, otherwise not. If the backup
Expand All @@ -15,6 +16,9 @@
#' appends the startup code to the end of the file. is overwritten. If `TRUE`,
#' any pre-existing R startup file is overwritten.
#'
#' @param path The folder where to create \file{.Renviron.d} and
#' \file{.Rprofile.d} directory.
#'
#' @param make_dirs If `TRUE` (default), directories \file{.Renviron.d/} and
#' \file{.Rprofile.d/} are created in folder `path`, if missing.
#'
Expand All @@ -27,8 +31,8 @@
#' the \file{.Rprofile} file, which is created if missing.
#'
#' @export
install <- function(path = "~", backup = TRUE, overwrite = FALSE,
make_dirs = TRUE, quiet = FALSE) {
install <- function(file = rprofile_user(), backup = TRUE, overwrite = FALSE,
path = dirname(file), make_dirs = TRUE, quiet = FALSE) {
if (quiet) notef <- function(...) NULL

if (make_dirs) {
Expand All @@ -45,7 +49,6 @@ install <- function(path = "~", backup = TRUE, overwrite = FALSE,
}
}

file <- file.path(path, ".Rprofile")
if (is_installed(file)) {
msg <- sprintf("startup::startup() already installed: %s", squote(file))
notef(msg)
Expand Down Expand Up @@ -77,10 +80,9 @@ install <- function(path = "~", backup = TRUE, overwrite = FALSE,

#' @describeIn install Remove calls to `startup::startup()` and similar.
#' @export
uninstall <- function(path = "~", backup = TRUE, quiet = FALSE) {
uninstall <- function(file = rprofile_user(), backup = TRUE, quiet = FALSE) {
if (quiet) notef <- function(...) NULL

file <- file.path(path, ".Rprofile")
if (!is_installed(file)) {
msg <- sprintf("startup::startup() not installed: %s", squote(file))
notef(msg)
Expand All @@ -106,7 +108,18 @@ uninstall <- function(path = "~", backup = TRUE, quiet = FALSE) {
}


is_installed <- function(file = file.path("~", ".Rprofile")) {
rprofile_user <- function() {
file <- Sys.getenv("R_PROFILE_USER", NA_character_)
if (!is.na(file) && nzchar(file)) {
names(file) <- "R_PROFILE_USER"
return(file)
}
file <- file.path("~", ".Rprofile")
names(file) <- "~/.Rprofile"
file
}

is_installed <- function(file = rprofile_user()) {
if (!file.exists(file)) return(FALSE)
bfr <- readLines(file, warn = FALSE)
bfr <- gsub("#.*", "", bfr)
Expand Down
11 changes: 8 additions & 3 deletions man/install.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d79c8db

Please sign in to comment.