Watts-College / cpp-528-fall-2022

https://watts-college.github.io/cpp-528-fall-2022/
1 stars 2 forks source link

Open R Project file from .Rmd file #8

Closed jacobtnyoung closed 2 years ago

jacobtnyoung commented 2 years ago

In Lab 2, it asks us to use the here() function to open the "data/rodeo/ltdb_data_dictionary.csv" file:


# store data dictionary file path
DD_FILEPATH <- here::here( "data/rodeo/ltdb_data_dictionary.csv" )

# import data dictionary
dd <- read.csv( DD_FILEPATH, stringsAsFactors=F )

This works when I have my R Project open. But, I don't see how it works when running the utilties.R script or when running the .Rmd file for the lab.

How do you open an R Project from the .R script and the .Rmd file?

lecy commented 2 years ago

RMD files already have local awareness - when you knit they will use the local folder as the default directory.

(you might need to open R Studio by clicking on a specific RMD file - I have heard reports that behavior is not uniform across operating systems)

It depends on how your project folder is organized. If the lab and data files are in the same folder:

#  project
#  +-- lab.rmd
#  +-- ltdb-data.csv

d <- read.csv( "ltdb-data.csv" )

Data in a data subdirectory:

#  project
#  +-- lab.rmd
#  |   data
#  |   +-- ltdb-data.csv

d <- read.csv( "data/ltdb-data.csv" )

In sibling directories:

#  project
#  |   labs
#  |   +-- lab.rmd
#  |   data
#  |   +-- ltdb-data.csv

d <- read.csv( "../data/ltdb-data.csv" )

In a parent directory:

#  project
#  +-- ltdb-data.csv
#  |   labs
#  |   +-- lab.rmd

d <- read.csv( "../ltdb-data.csv" )

In R scripts you have to set the directory manually, which will be problematic because different team members would likely have different folder paths for the project.

setwd( ... )

Thus the creation of the here package and project settings.

jacobtnyoung commented 2 years ago

Got it. Thanks.