Hear, here()! Something so simple, it took me a while to appreciate it

here by @krlmlr crossed my eyeballs and my stupid reaction is *why do we need another getwd?

Well, here's a usecase.

uri <- paste0("file://",
              system.file(file.path("doc", "tm.pdf"), package = "tm"))

(I hope that @jennybryan doesn't ignite the keyboard that this example was composed on.)

The tidy way

library(here)
uri <- here("tm.pdf")

So, anytime you have a function signature looking for a full file path for something in your working directory just use the snippet here

8 Likes

here is a fabulous package! I teach with here() exclusively now, in part because it makes it easy to solve issues where data and .Rmd files are stored in different sub-directories of the same project:

|- data/
|---- data.csv
|- docs/
|---- analysis.Rmd
|- project.Rproj

Using here("data", "data.csv") gets students easily around any issues with constructing file paths.

This in itself is the other reason I like teaching with here() - my students, few of whom have a cs background, really struggle with file paths. The way that here() forces you to specify each layer of a folder hierarchy as a string seems to help them develop a sense of the anatomy of their projects in a way that specifying paths as a single string does not.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.