Skip to content

Instantly share code, notes, and snippets.

@sbalci
Forked from jasonsychau/this_file_path.R
Created December 3, 2022 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbalci/8b5991d2e8d4029ef29ab9c8d93d321a to your computer and use it in GitHub Desktop.
Save sbalci/8b5991d2e8d4029ef29ab9c8d93d321a to your computer and use it in GitHub Desktop.
R - get this file path
# a combination of
# https://stackoverflow.com/questions/1815606/determine-path-of-the-executing-script/15373917#15373917
# https://stackoverflow.com/questions/1815606/determine-path-of-the-executing-script/7585599#7585599
# https://stackoverflow.com/questions/47044068/get-the-path-of-current-script/47045368#47045368
# https://stackoverflow.com/questions/53512868/how-to-automatically-include-filepath-in-r-markdown-document/53516876#53516876
stub <- function() {}
thisPath <- function() {
cmdArgs <- commandArgs(trailingOnly = FALSE)
if (length(grep("^-f$", cmdArgs)) > 0) {
# R console option
normalizePath(dirname(cmdArgs[grep("^-f", cmdArgs) + 1]))[1]
} else if (length(grep("^--file=", cmdArgs)) > 0) {
# Rscript/R console option
scriptPath <- normalizePath(dirname(sub("^--file=", "", cmdArgs[grep("^--file=", cmdArgs)])))[1]
} else if (Sys.getenv("RSTUDIO") == "1") {
if (rstudioapi::isAvailable(version_needed=NULL,child_ok=FALSE)) {
# RStudio interactive
dirname(rstudioapi::getSourceEditorContext()$path)
} else if (is.null(knitr::current_input(dir = TRUE)) == FALSE) {
# Knit
knitr::current_input(dir = TRUE)
} else {
# R markdown on RStudio
getwd()
}
} else if (is.null(attr(stub, "srcref")) == FALSE) {
# 'source'd via R console
dirname(normalizePath(attr(attr(stub, "srcref"), "srcfile")$filename))
} else {
stop("Cannot find file path")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment