Skip to content

Instantly share code, notes, and snippets.

@yonicd
Created February 21, 2019 18:49
Show Gist options
  • Save yonicd/03a471001bc4ad0a3e0b7b4b21f252e2 to your computer and use it in GitHub Desktop.
Save yonicd/03a471001bc4ad0a3e0b7b4b21f252e2 to your computer and use it in GitHub Desktop.
reliably find where script is run from in R
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