I've looked through many questions similar to this (see end of post), but I haven't found any solutions that actually accomplish what I need. I code on either Windows or Fedora depending on the project, and I code for people who use Windows and several Linux distributions.
Part of my job is making R scripts for people that automatically analyze data and create graphs. Most commonly, I'll just send them the script and it will generate the graph. This way, if the data changes or expands, I don't need to re-run the script for them (also, they can make changes as needed).
The problem is that I don't know how to get an R-script to find out where itself is located. It would be very convenient to just be able to create code that works as follows:
This question only deals with Step 2. Everything else flows nicely as long as I can accomplish that. It would be nice to have something such as:
setwd(FindThisScriptsLocation())
The line: source(..., chdir = T) has been suggested here, but it can't be used for a script to reference itself unless it knew its own path.
Here are some related questions:
Where is the .R script file ... (deals with packages)
How to get R to recognize your working directory ... (setting default working directory)
Rscript: Determine path of executing script (one script calling others; no answer found)
You can use getwd() (to find the current path of the R process) or file. choose() to interactively find the file (it will return a character of the full path to the file). If you don't want to learn from this and/or do it with different files all the time, then you can short-circuit it with read.
The current working directory is displayed by the RStudio IDE within the title region of the Console pane. You can also check your current working directory by running the command getwd() in the console. There are a number of ways to change the current working directory: Use the setwd R function.
Location of the R Script FileThe R script file is located in the R script's directory, as specified in the _RScriptFile parameter for the R script's metric in MicroStrategy.
Had the same problem, here's what I came up with. It works with source() and rmarkdwon::render() on windows and on linux.
Update: the function get_scriptpath() is now available as part of my envDocument package on CRAN. See https://cran.r-project.org/package=envDocument
#' Get the path of the calling script #' #' \code{get_scriptpath} returns the full path of the script that called this function (if any) #' or NULL if path is not available #' #' @examples #' mypath <- get_scriptpath() #' @export #' get_scriptpath <- function() { # location of script can depend on how it was invoked: # source() and knit() put it in sys.calls() path <- NULL if(!is.null(sys.calls())) { # get name of script - hope this is consisitent! path <- as.character(sys.call(1))[2] # make sure we got a file that ends in .R, .Rmd or .Rnw if (grepl("..+\\.[R|Rmd|Rnw]", path, perl=TRUE, ignore.case = TRUE) ) { return(path) } else { message("Obtained value for path does not end with .R, .Rmd or .Rnw: ", path) } } else{ # Rscript and R -f put it in commandArgs args <- commandArgs(trailingOnly = FALSE) } return(path) }
somewhere in the "load the script" process, you are passing the name and path of the R script. I'm suggesting to capture that information and then use a wrapper script to execute your main script.
the wrapper function that takes as an argument the path and fiile name of the script to execute
FILE <- "~/Desktop/myFolder/InHere/myScript.R"
at the start of your wrapper function, let the user click through to the file:
FILE <- file.choose()
DIR <- dirname(FILE)
and there you have your directory/folder and you can execute your script as normal passing DIR
as a parameter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With