-
-
Save sbalci/8b5991d2e8d4029ef29ab9c8d93d321a to your computer and use it in GitHub Desktop.
Revisions
-
jasonsychau revised this gist
May 25, 2020 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal 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") { 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)) -
jasonsychau revised this gist
May 12, 2020 . 1 changed file with 10 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal 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) 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 (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") } } -
jasonsychau revised this gist
May 11, 2020 . No changes.There are no files selected for viewing
-
jasonsychau revised this gist
May 11, 2020 . No changes.There are no files selected for viewing
-
jasonsychau created this gist
May 11, 2020 .There are no files selected for viewing
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 charactersOriginal 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") } }