Skip to main content
  1. About
  2. For Teams
Asked
Viewed 38k times
Part of R Language Collective
27

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:

  1. User saves script to folder containing data, then runs script.
    • I usually just email the script to the person I'm working with.
    • They save the script to a folder containing the data they want analyzed/graphed.
    • Ideally, they would just launch R, load the script, then run the script.
  2. Script determines its own location, then sets that as the working directory.
  3. Script analyzes data inside its own directory.
  4. Script generates graph(s) and saves it to its own directory.

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:

13
  • 2
    Please provide links to the similar questions, and tell us what specifically about them does not work in this case.
    Joshua Ulrich
    –  Joshua Ulrich
    2013-08-01 17:30:04 +00:00
    Commented Aug 1, 2013 at 17:30
  • 4
    In step 1, how is the user running the script? That is where that information can get passed to the function.
    Ricardo Saporta
    –  Ricardo Saporta
    2013-08-01 17:30:24 +00:00
    Commented Aug 1, 2013 at 17:30
  • 1
    Related: Getting path of an R script, get filename and path of sourced file
    Joshua Ulrich
    –  Joshua Ulrich
    2013-08-01 17:43:18 +00:00
    Commented Aug 1, 2013 at 17:43
  • 1
    @HongOoi The people I work with have expressed that they specifically want me to do this. Currently, we have to manually set the working directory every time it changes hands or location.
    Will Beason
    –  Will Beason
    2013-08-01 17:57:34 +00:00
    Commented Aug 1, 2013 at 17:57
  • 1
    This seems overly-complicated. Why not just send them a workspace file (created via save.image) that contains a function they can call? As long as the .RData files are associated with R, they can just double-click the file after saving it in whatever directory, then call the function.
    Joshua Ulrich
    –  Joshua Ulrich
    2013-08-01 18:21:11 +00:00
    Commented Aug 1, 2013 at 18:21

5 Answers 5

14

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)
}
Sign up to request clarification or add additional context in comments.

7 Comments

This is the only of the proposed answers that works for me. Running Lubuntu 16.04.
I am confused about how to use this function..what is the argument I pass to it? When calling get_scriptpath() with no arguments I get error: Error in get_scriptpath() : No path information available
There are no arguments to this function. It will ONLY work when called from a running script (e.g., if you run a script or RMD file using Rscript, source, rmarkdown::render). If you call it from the R console, it won't work because the console isn't a script. The envDocument package at cran.r-project.org/package=envDocument provides a version of this function with improved documentation and error handling.
Why filter on file extension? In what cases have you encountered a calling file that did not have one of the anticipated extensions?
Paranoia - if for some reason the system call to run the script/report isn't where I expect it then I wanted to get a heads up; I'm trusting that it will always be in the same position in the sys.call return value. in retrospect I might even escalate this to a warning or error. And I don't want to imagine if for some reason there's an incorrect value that has an expected extension...
|
5

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.

Option 1 (programatically)

the wrapper function that takes as an argument the path and fiile name of the script to execute

FILE <- "~/Desktop/myFolder/InHere/myScript.R"

Option 2 (interactively)

at the start of your wrapper function, let the user click through to the file:

FILE <- file.choose()

THEN:

DIR  <- dirname(FILE)

and there you have your directory/folder and you can execute your script as normal passing DIR as a parameter

Comments

3

Hey I have a possible solution that's a little bit of extra initial work, but should be able to do what you need.

First make your R script take in a parameter which will be the location of the script. Next you just need to create a Bash/Batch (one each for windows and unix) that will

1) get its own directory

2) search the directory for an R script (simple *.R search)

3) call the R script with it's own directory from step 1.

Then you simply package the Bash and Batch scripts with the folder you give to your clients and ask them to just run the relevant script for their environment.

You should, in theory, only have to create the Bash/Batch scripts once.

EDIT: I've created a simple bash script that works for this problem, see below

#!/bin/bash

#Modify the search string to narrow the search
SEARCH_STRING="*.R"

#Get the current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo ${DIR}

#Get the name of the R script
R_SCRIPT=`find $DIR -name ${SEARCH_STRING} -type f -exec basename {} \;`
echo ${R_SCRIPT}

Rscript ${R_SCRIPT} ${DIR}

I'm not so well versed in the Windows Shell so you'll have to do that one yourself :P

You can test it with this R script;

options(echo = FALSE) #So we don't get the script echo'd

arguments <- commandArgs(trailingOnly = TRUE) #getting the arguments

working_directory <- arguments[1]

setwd(working_directory)

getwd() #print out to test

Comments

1

I assume this is Windows.

Following up Ricardo's suggestion: Have the client's systems set up in a way that, if a script is double-clicked, the R interpreter is started in the directory of the script. You could also assign a special extension for this behavior (say, .Rwd for "R script setting the work directory"). Then, you don't need to setwd() within the script.

For starters, the following command line script might do (untested):

pushd %~d1%~p1
R --vanilla < "%1"

Associate .Rwd files with this script.

If you need to source() other scripts, consider using the chdir=T argument.

2 Comments

I mostly use Windows, but I do also use Fedora depending on the project. I write code for people who run Windows and a few Linux distributions. Your answer definitely will work (I'll learn how to do what you said, it sounds very useful), but it isn't completely ideal.
You can abbreviate %~d1%~p1 as %~dp1
-2

If you're using RStudio, simply hold the cursor over the script and the path will appear

enter image description here

2 Comments

@stevec- the question related to batch execution not to use of the Rstudio GUI
@DonJ that may be true, but I arrived here searching for how to view the location of an R file in RStudio, so I'd guess others will end up here from the same google search. Hence it's pretty harmless to include this info here

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.