Find location of current .R file

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

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:

回答1:

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 

Option 2 (interactively)

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

FILE 

THEN:

DIR  

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



回答2:

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 


回答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 


回答4:

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 

Associate .Rwd files with this script.

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!