Using here() with downloaded files

Error Information:

I feel like I am missing something with the here() package. I am trying to upload some folders up to the Dataverse data repository and the .Rproj files are creating some problems with the ingest. So I'm trying to use here() without them. However, I don't think I'm getting it quite right.

When I download the necessary folders from my draft dataverse, they end up in the Downloads folders. But others might have their files save on their desktops, or their root directory. I get that here() is supposed to address this situation. When I open the .R file ( ~/Downloads/file.R ) (without a project file) as if I were a user encountering the data for the first time, then the working directory is my root directory.

I tried adding a .here file to the folder that I downloaded, but here() is still returning the root directory. I also tried using set_here("~/Downloads") and it did create a .here file in the Downloads directory but here() still only returns the root directory.

The problem is that the code assumes that the working directory is the folder that the .R file is in to start reading in the data in the subfolders. Again, I thought that the here() package was supposed to address all of this, and I could do it so easily with the .Rproj files, but that may not be available to me.

Steps taken so far -

System Information:

  • RStudio Edition: Desktop
  • RStudio Version: 1.2.5019
  • OS Version: 10.14.6 (Mojave)
  • R Version: 3.6.1

I believe that here is meant to make paths within an RProj easier, but does not work outside of that context.

here uses the top-level directory of a project to easily build paths to files
https://github.com/r-lib/here

The problem it solves for me is dealing with the way .R and .Rmd files interpret the root directory. R files consider the directory that the Rproj sits in to be the root, whereas Rmd files consider the folder that it sits in to be the root (at least when you knit them). Here's a visual if it helps.

It means that given this scenario:

project.RProj
data/data.csv
folder/script.R
folder/report.Rmd

The call read_csv("data/data.csv") would work fine in script.R but would need to be modified to read_csv("../data/data.csv") in report.Rmd. If the script is sourced within the Rmd, then you probably want a path that works for both, which is what here::here("data/data.csv") would help with.

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