package:this.path provides a mechanism for an R script to retrieve its own path without the explicit need to write it elsewhere.


Install it from CRAN:


Install the development version from GitHub:

    repos = "")

## or:



The most important functions from package:this.path are this.path(), this.dir(), here(), and this.proj():

New additions include:

package:this.path also provides functions for constructing and manipulating file paths:


This package started from a stack overflow posting:

If you like this package, please consider upvoting my answer so that more people will see it! If you have an issue with this package, please use = "this.path") to report your issue.


If you are unhappy with the performance of package:this.path, or would like to know other solutions, here are some alternatives:

Alternative 1: Other Packages That Determine Current R Script

There are a few other packages and functions that provide the ability to retrieve the path of the current R script:

These are lacking in functionality compared to package:this.path:

These packages also have some objectively incorrect coding issues such as:

Alternative 2: Packages That Determine Project Root

package:here provides function here::here() with the ability to retrieve the project root, but it lacks functionality:

package:rprojroot is the R package upon which package:here is built. It provides a list of project root criteria rprojroot::criteria. It is not useful on its own since, like here::here(), it does not work when the working directory is set outside the context of the project. However, you can combine it with package:this.path to get the best results:

## replace 'rprojroot::is_r_package' with desired criterion

fix_file <- this.path::make_fix_file(rprojroot::is_r_package)

If the default criteria are not sufficient for your needs, you can make your own using:

package:rprojroot recommends using <criterion>$find_file(path = whereami::thisfile()) for this purpose, but as mentioned in section Alternative 1, whereami::thisfile() is seriously lacking compared to this.path::this.path().

Alternative 3: package:box

package:box provides two related functions:

These both lack the same functionality as the packages listed in Alternative 1. box::file() should not be used in favour of this.path::here(). However, box::use() is still extremely useful, it just needs to be combined with package:this.path to get the best results:

    <import 1>,
    <import 2>,

This explicitly tells package:box the path of the current script so that relative imports will work correctly.

Alternative 4: Always Change Working Directory

The working directory could always be changed to the directory of the executing script before running it. This would be:

cd /path/to
Rscript ./file.R


source("/path/to/file.R", chdir = TRUE)

This fails when moving throughout files in different directories, it fails for executable R scripts, and it ignores the simple fact that it is sometimes convenient to have the working directory set elsewhere.

Alternative 5: Source References

utils::getSrcFilename() provides the ability to retrieve the filename of a source reference. Everywhere this.path() would be used, replace it with utils::getSrcFilename(function() NULL, full.names = TRUE). This comes with some issues such as:


If you think I have overlooked some of the alternatives, or think there are any improvements I could make to package:this.path, please let me know, I am open to suggestions! I hope this package serves you well!