Last active
February 10, 2025 15:01
-
-
Save nanxstats/55f1909bf787a304e5e40572fd4ad215 to your computer and use it in GitHub Desktop.
Remove ProjectId field added by RStudio from .Rproj files at R startup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remove ProjectID from .Rproj files if freshly added | |
local({ | |
xfun <- requireNamespace("xfun", quietly = TRUE) | |
rproj_files <- list.files(pattern = "\\.Rproj$", full.names = TRUE) | |
if (!xfun || length(rproj_files) == 0L) return(invisible(NULL)) | |
lapply(rproj_files, function(f) { | |
diff_cmd <- system(paste("git diff --", shQuote(f)), intern = TRUE) | |
diff_out <- tryCatch(diff_cmd, error = function(e) character(0)) | |
if (any(grepl("^\\+ProjectId: ", diff_out))) { | |
dcf_old <- xfun::read_utf8(f) | |
dcf_new <- dcf_old[!grepl("^ProjectId: ", dcf_old)] | |
xfun::write_utf8(dcf_new, f) | |
} | |
}) | |
invisible(NULL) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment