Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Last active August 10, 2022 21:03
Show Gist options
  • Save matt-dray/b4ff92f01093f512dae99b670e3eddb7 to your computer and use it in GitHub Desktop.
Save matt-dray/b4ff92f01093f512dae99b670e3eddb7 to your computer and use it in GitHub Desktop.
R function to check for open RStudio Projects that have the same name (macOS only for now). Provides a warning in the console on start-up and (optionally) uses speech to text to tell you which ones are duplicated. You could put this in your .Rprofile so it runs on startup.
# See blogpost for details:
# https://www.rostrum.blog/2022/07/08/rproj-dupes/
check_rproj_dupes <- function(speak = FALSE) {
os <- .Platform$OS.type
if (os == "unix") {
ps_out <- system("ps -e", intern = TRUE)
ps_rproj <- ps_out[grepl(".Rproj", ps_out)]
ps_split <- strsplit(ps_rproj, "\\s")
rproj_paths <- lapply(ps_split, function(x) x[grepl(".Rproj$", x)])
rproj_basenames <- lapply(rproj_paths, basename)
rproj_dupes <- sort(unlist(rproj_basenames[duplicated(rproj_basenames)]))
}
if (os == "windows") {
stop("Sorry, check_rproj_dupes() doesn't work on Windows yet :-(")
}
if (length(rproj_dupes) > 0) {
if (speak & os == "unix") {
dupes_string <- paste(rproj_dupes, collapse = ", ")
dupes_string_say <- gsub("\\.Rproj", " dot ar proj ", dupes_string)
message <- paste(
"say ha, you fool, you have more than one open RStudio Project with",
ifelse(length(rproj_dupes) == 1, "this name:", "these names:"),
dupes_string_say
)
system(message)
}
warning(
"You've got open RStudio Projects with the same name:\n",
paste("-", rproj_dupes, collapse = "\n"), "\n",
call. = FALSE
)
}
}
check_rproj_dupes()
rm(check_rproj_dupes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment