Last active
June 17, 2019 05:44
-
-
Save ymattu/bad61e6c4f2eab906e4376734d4927e4 to your computer and use it in GitHub Desktop.
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
#' | |
#' @importFrom magrittr %>% | |
#' @importFrom stringr str_which str_subset str_replace | |
#' @importFrom rstudioapi isAvailable getActiveDocumentContext getSourceEditorContext | |
library(stringr) | |
library(purrr) | |
library(rstudioapi) | |
suppressMessages(library(here)) | |
whoAmI <- function() { | |
# http://stackoverflow.com/a/32016824/2292993 | |
res <- NA | |
cmdArgs <- commandArgs(trailingOnly = FALSE) | |
needle <- "--file=" | |
matches <- str_which(cmdArgs, needle) | |
if (length(matches) > 0) { | |
# Rscript via command line | |
res <- cmdArgs %>% | |
str_subset(., needle) %>% | |
str_replace(., needle, "") %>% | |
normalizePath() | |
} else if (isAvailable()) { | |
# on RStudio | |
res <- getSourceEditorContext()$path %>% | |
normalizePath() | |
} else { | |
print(here() | |
} | |
return(res) | |
} | |
whereAmI <- function(filepath) { | |
return(dirname(filepath)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment