Search code examples
rtime-seriesstansetwd

How to use the function here::here() with the function source() to set the top level of your project folder in R


Issue

I'm trying to build a path to the top level of my project file by using the functions source(), here:here() into a new (empty) environment using the new.env() function to copy the data into the parent frame.

This code will be used with an interface between 'Stan' and 'R' using the package 'cmdstanr'.

I'm really confused with how to use these functions and I was wondering if anyone knows how to do this.

Dummy Dataframe

tibble(
       Month = sample(month.name, 120, replace = TRUE),
       Year = sample(2012:2024, 120, replace = TRUE),
       Date = Sys.Date(), 120, replace = TRUE,
       Number_Daffodils = sample(1:5, 120, replace = TRUE)
       ) 

R-Code

library(here)

#Open the package 'cmdstanr'
install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

#Knitr will treat each option that you pass to knitr::opts_chunk$set as a global default that can be overwritten in individual chunk headers.
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)

#Open the new environment and to build a path to the top level of my project file by using the functions source(), here:here()  
datenv <- new.env()
source(here::here("Users/coreybrown/Documents/Dat_Frame/Data/dat.csv"), local = datenv)
dat <- datenv$dat

Error Message

Error in source(here::here("Users/coreybrown/Documents/Dat_Frame/Data/dat.csv"),  : 
  /Users/coreybrown/Documents/Dat_Frame/Data/dat.csv:1:5: unexpected ','
1: year,

Dummy Dataframe

tibble(
       Month = sample(month.name, 120, replace = TRUE),
       Year = sample(2012:2024, 120, replace = TRUE),
       Number_Daffodils = sample(1:5, 120, replace = TRUE)
      ) 

Example of my Data Frame

   year     month Number_Daffodils Frequency_New_Bulbs       date n_month
1  2012   January                1                   7 2012-01-01       1
2  2012  February                8                  59 2012-02-01       2
3  2012     April               18                 144 2012-04-01       4
4  2012       May               21                 193 2012-05-01       5

Solution

  • I think your error is related to how you are using source(). source() also parses your input, so the unexpected comma is because it's a .csv and cannot be parsed as R code.

    The {here} package is just a helper to build reliable paths. You can execute here::here() to see what it will append as a prefix to anything in its ... argument. It should set the starting point as the R project when you load/attach the package, however you can also execute here::i_am() to the top of your script to make sure it's set where you want it.

    Maybe something like this, creating your new environment, and then assigning your imported data frame using read.csv(), will work for you to keep that data environment separate. However, that last line of code (dat <- datenv$dat) you have will copy the data into the parent frame you're working in. (I did not fully understand how you wanted to enclose your environments so my apologies if what I wrote below will not work for you.)

    # path relative to your .Rproj file, here() appends the rest as a prefix
    here::i_am("dat.csv") 
    
    # create the data environment in the parent frame
    datenv <- new.env()
    # assign dat.csv to dat in that environment
    datenv$dat <- read.csv(here::here("relative path to your csv file")
    # this will copy dat to your top level env
    dat <- datenv$dat