Created
February 21, 2019 18:49
-
-
Save yonicd/03a471001bc4ad0a3e0b7b4b21f252e2 to your computer and use it in GitHub Desktop.
reliably find where script is run from in R
This file contains 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
runloc <- function(path_expand = FALSE){ | |
src <- 'Untitled' | |
if(rstudioapi::getActiveDocumentContext()$id=='#console') | |
src <- 'Console' | |
if(length(getSrcFilename(sys.call(sys.nframe()-1)))>0) | |
src <- file.path( | |
getSrcDirectory(sys.call(sys.nframe()-1)), | |
getSrcFilename(sys.call(sys.nframe()-1)) | |
) | |
if(nchar((rstudioapi::getActiveDocumentContext())$path)>0) | |
src <- path.expand((rstudioapi::getActiveDocumentContext())$path) | |
if(!path_expand) | |
src <- gsub(getwd(),'.',src) | |
src | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment