Skip to content

Instantly share code, notes, and snippets.

@ymattu
Last active June 17, 2019 05:44
Show Gist options
  • Save ymattu/bad61e6c4f2eab906e4376734d4927e4 to your computer and use it in GitHub Desktop.
Save ymattu/bad61e6c4f2eab906e4376734d4927e4 to your computer and use it in GitHub Desktop.
#'
#' @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