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
  • Save sbalci/8b5991d2e8d4029ef29ab9c8d93d321a to your computer and use it in GitHub Desktop.
Save sbalci/8b5991d2e8d4029ef29ab9c8d93d321a to your computer and use it in GitHub Desktop.

Revisions

  1. @jasonsychau jasonsychau revised this gist May 25, 2020. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions this_file_path.R
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    # 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)
    @@ -12,8 +13,16 @@ thisPath <- function() {
    # Rscript/R console option
    scriptPath <- normalizePath(dirname(sub("^--file=", "", cmdArgs[grep("^--file=", cmdArgs)])))[1]
    } else if (Sys.getenv("RSTUDIO") == "1") {
    # RStudio
    dirname(rstudioapi::getSourceEditorContext()$path)
    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))
  2. @jasonsychau jasonsychau revised this gist May 12, 2020. 1 changed file with 10 additions and 12 deletions.
    22 changes: 10 additions & 12 deletions this_file_path.R
    Original file line number Diff line number Diff line change
    @@ -2,24 +2,22 @@
    # 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
    stub <- function() {}
    thisPath <- function() {
    cmdArgs <- commandArgs(trailingOnly = FALSE)
    needle <- "--file="
    match <- grep(needle, cmdArgs)
    if (length(match) > 0) {
    # Rscript
    dirname(normalizePath(sub(needle, "", cmdArgs[match])))
    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") {
    # RStudio
    dirname(rstudioapi::getSourceEditorContext()$path)
    } else if (length(grep("^-f$", args)) > 0) {
    } else if (is.null(attr(stub, "srcref")) == FALSE) {
    # 'source'd via R console
    args <- commandArgs(trailingOnly = F)
    normalizePath(dirname(args[grep("^-f", args) + 1]))[1]
    } else if (length(grep("^--file=", args)) > 0) {
    args <- commandArgs(trailingOnly = F)
    scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))[1]
    dirname(normalizePath(attr(attr(stub, "srcref"), "srcfile")$filename))
    } else {
    stop("no file found")
    stop("Cannot find file path")
    }
    }
  3. @jasonsychau jasonsychau revised this gist May 11, 2020. No changes.
  4. @jasonsychau jasonsychau revised this gist May 11, 2020. No changes.
  5. @jasonsychau jasonsychau created this gist May 11, 2020.
    25 changes: 25 additions & 0 deletions this_file_path.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # 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
    thisPath <- function() {
    cmdArgs <- commandArgs(trailingOnly = FALSE)
    needle <- "--file="
    match <- grep(needle, cmdArgs)
    if (length(match) > 0) {
    # Rscript
    dirname(normalizePath(sub(needle, "", cmdArgs[match])))
    } else if (Sys.getenv("RSTUDIO") == "1") {
    # RStudio
    dirname(rstudioapi::getSourceEditorContext()$path)
    } else if (length(grep("^-f$", args)) > 0) {
    # 'source'd via R console
    args <- commandArgs(trailingOnly = F)
    normalizePath(dirname(args[grep("^-f", args) + 1]))[1]
    } else if (length(grep("^--file=", args)) > 0) {
    args <- commandArgs(trailingOnly = F)
    scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))[1]
    } else {
    stop("no file found")
    }
    }