Learn R Programming

this.path (version 2.2.0)

this.path-package: Get Script's Path

Description

Determine the path of the executing script. Compatible with a few popular GUIs: ‘Rgui’, ‘RStudio’, ‘VSCode’, ‘Jupyter’, and ‘Rscript’ (shell). Compatible with several functions and packages: source(), sys.source(), debugSource() in ‘RStudio’, compiler::loadcmp(), box::use(), knitr::knit(), plumber::plumb(), shiny::runApp(), targets, and testthat::source_file().

Arguments

Author

tools:::Rd_package_author("this.path")

Maintainer: tools:::Rd_package_maintainer("this.path")

Details

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

  • this.path() returns the normalized path of the script in which it is written.

  • this.dir() returns the directory of this.path().

  • here() constructs file paths against this.dir().

  • this.proj() constructs file paths against the project root of this.dir().

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

  • path.join(), basename2(), and dirname2() are drop in replacements for file.path(), basename(), and dirname() which better handle drives and network shares.

  • splitext(), removeext(), ext(), and ext<-() split a path into root and extension, remove a file extension, get an extension, or set an extension for a file path.

  • path.split(), path.split.1(), and path.unsplit() split the path to a file into components.

  • relpath(), rel2here(), and rel2proj() turn absolute paths into relative paths.

New additions to this.path include:

  • with_site.file() and with_init.file() allow this.path() and related to be used in the site-wide startup profile file or a user profile.

  • LINENO() returns the line number of the executing expression.

  • set.sys.path() implements this.path() for any source()-like functions outside of source(), sys.source(), debugSource() in ‘RStudio’, compiler::loadcmp(), box::use(), knitr::knit(), plumber::plumb(), shiny::runApp(), targets, and testthat::source_file().

  • shFILE() looks through the command line arguments, extracting FILE from either of the following: -f FILE or --file=FILE