Find Location of Current .R File

Get the path of current script

In RStudio, you can get the path to the file currently shown in the source pane using

rstudioapi::getSourceEditorContext()$path

If you only want the directory, use

dirname(rstudioapi::getSourceEditorContext()$path)

If you want the name of the file that's been run by source(filename), that's a little harder. You need to look for the variable srcfile somewhere back in the stack. How far back depends on how you write things, but it's around 4 steps back: for example,

fi <- tempfile()
writeLines("f()", fi)
f <- function() print(sys.frame(-4)$srcfile)
source(fi)
fi

should print the same thing on the last two lines.

Find location of current .R file

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

Getting path of an R script

Use source("yourfile.R", chdir = T)

R command for setting working directory to source file location in Rstudio

To get the location of a script being sourced, you can use utils::getSrcDirectory or utils::getSrcFilename. So changing the working directory to that of the current file can be done with:

setwd(getSrcDirectory()[1])

This does not work in RStudio if you Run the code rather than Sourceing it. For that, you need to use rstudioapi::getActiveDocumentContext.

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

This second solution requires that you are using RStudio as your IDE, of course.

Command to see 'R' path that RStudio is using

(Edited to reflect fact that this is apparently a Windows-specific solution.)

Here on Windows, I'd use the following, for reasons discussed here by Henrik Bengtsson near the start of a long thread on the subject.

file.path(R.home("bin"), "R")

This is better than using file.path(R.home(), "bin", "R") in several settings alluded to in the "Value" section of this snippet from help(R.home):

Details:

The R home directory is the top-level directory of the R
installation being run.

[...]

Value:

A character string giving the R home directory or path to a
particular component. Normally the components are all subdirectories
of the R home directory, but this may not be the case in a Unix-like
installation. [...] The return value for "modules" and on Windows
"bin" is to a sub-architecture-specific location.

Where is the .R script file located on the PC?

See FAQ 7.40 How do I access the source code for a function?

In most cases, typing the name of the function will print its source
code. However, code is sometimes hidden in a namespace, or compiled.
For a complete overview on how to access source code, see Uwe Ligges
(2006), “Help Desk: Accessing the sources”, R News, 6/4, 43–45
(http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf).

Determining current file's location in R to include file from same directory?

source("/Users/myuser/workspace/myproject/my-utils.r", chdir=TRUE)

When chdir option is set to true and the source file parameter is a full path, the directory of file will be used as the working directory while sourcing the file.

NOTE: cur_dir <- sys.get_current_file_path() doesn't make much sense because pathnames are not unique.

get filename and path of `source`d file

Just FYI, knitr will setwd() to the dir of the input file when (and only when) evaluating the code chunks, i.e. if you call knit('path/to/input.Rnw'), the working dir will be temporarily switched to path/to/. If you want to know the input dir in code chunks, currently you can call an unexported function knitr:::input_dir() (I may export it in the future).



Related Topics



Leave a reply



Submit