Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the path of current script

Tags:

r

rstudio

I would like to set the working directory to the path of current script programmatically but first I need to get the path of current script.

So I would like to be able to do:

current_path = ...retrieve the path of current script ... setwd(current_path)  

Just like the RStudio menu does: RStudio set working directory

So far I tried:

initial.options <- commandArgs(trailingOnly = FALSE) file.arg.name <- "--file=" script.name <- sub(file.arg.name, "", initial.options[grep(file.arg.name, initial.options)]) script.basename <- dirname(script.name) 

script.name returns NULL

source("script.R", chdir = TRUE) 

Returns: Error in file(filename, "r", encoding = encoding) : cannot open the connection In addition: Warning message: In file(filename, "r", encoding = encoding) : cannot open file '/script.R': No such file or directory

dirname(parent.frame(2)$ofile) 

Returns: Error in dirname(parent.frame(2)$ofile) : a character vector argument expected ...because parent.frame is null

frame_files <- lapply(sys.frames(), function(x) x$ofile) frame_files <- Filter(Negate(is.null), frame_files) PATH <- dirname(frame_files[[length(frame_files)]]) 

Returns: Null because frame_files is a list of 0

thisFile <- function() {     cmdArgs <- commandArgs(trailingOnly = FALSE)     needle <- "--file="     match <- grep(needle, cmdArgs)     if (length(match) > 0) {         # Rscript         return(normalizePath(sub(needle, "", cmdArgs[match])))     } else {         # 'source'd via R console         return(normalizePath(sys.frames()[[1]]$ofile))     } } 

Returns: Error in path.expand(path) : invalid 'path' argument

Also I saw all answers from here, here, here and here. No joy.

Working with RStudio 1.1.383

EDIT: It would be great if there was no need for an external library to achieve this.

like image 290
Panos Kal. Avatar asked Oct 31 '17 20:10

Panos Kal.


People also ask

How do I find the path of a script?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.

How do I get the current file path in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

How do I find the path of a script in Unix?

How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems? basename command – Display filename portion of pathname. dirname command – Display directory portion of pathname.

How do I get the directory of a PowerShell script?

PowerShell Get Current Directory of Script File To get current directory of script file or running script, use $PSScriptRoot automatic variable.


1 Answers

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.

like image 106
user2554330 Avatar answered Sep 19 '22 08:09

user2554330