0

I have different versions of tidyverse installed in a project renv and on my system. When I execute my project using a Makefile, it doesn't use the renv versions for some reason, instead using the system-wide versions. When I execute the code in the R file directly it uses the renv versions.

Could this have something to do with the initialization locations? renv was initialized in the project root folder but the Makefile is in the code folder, one level down.

Would appreciate any advice!

Thanks, Doron

EDIT: apologies, was unclear to me what information would be useful due to the nature of the issue.

My Makefile looks like this:

imp := import/
fn := functions/
mw := merge_wealthSupplement/
cm := clean_merged/
ccl := clean_clans/
mc := merge_clans/
cv := clean_variables/
dsc := descriptives/
an := analysis/
o := output/
i := input/
s := src/


targets := $(dsc)output/%.pdf
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
R = R --vanilla --args "" < "$<" "$(mkfile_path)"   # $< marks the first prerequisite

all: $(targets)

# Import data from PSID needs username and password so can't run automatically
# $(imp)$(o)data.Rds: $(imp)$(s)import.R
#   R --vanilla --args "" < "import/src/import.R"

$(mw)$(o)merged.Rds: $(mw)$(s)merge_wealthSupplement.R $(imp)$(o)data.Rds
    $(R)

$(cm)$(o)clean_merged.Rds: $(cm)$(s)clean_merged.R $(mw)$(o)merged.Rds $(fn)$(s)functions.R
    $(R)

$(ccl)$(o)sample.Rds: $(ccl)$(s)clean_clans.R $(cm)$(o)clean_merged.Rds $(fn)$(s)functions.R \
$(ccl)$(s)functions.R
    $(R)    

$(mc)$(o)merged_clans.Rds: $(mc)$(s)merge_clans.R $(ccl)$(o)sample.Rds \
$(cm)$(o)clean_merged.Rds $(fn)$(s)functions.R
    $(R)

$(cv)$(o)clean_variables.Rds: $(cv)$(s)clean_variables.R $(mc)$(o)merged_clans.Rds
    $(R)

$(targets): $(dsc)$(s)descriptives.R $(cv)$(o)clean_variables.Rds $(an)$(s)analysis.R
    $(R)    


.PHONY: clean

clean: 
    rm $(objects)

It is saved in .../project_name/code/Makefile

The R script where I notice the problem is the fourth makefile execution command: clean_clans.R

clean_clans.R starts like this:

if (!require("pacman")) install.packages(
  "pacman", repos = "http://cran.us.r-project.org")
pacman::p_load(tidyverse)

It is saved in .../project_name/code/clean_clans/src/clean_clans.R

The renv lockfile is in .../project_name/renv.lock

When I execute clean_clans.R from the file itself there is no problem. When I execute it via Terminal it errors upon getting to pick(), which was introduced in a newer dplyr version.

When I print out sessionInfo I see that all the package versions are consistent with the renv lockfile when I execute the code directly from the file, but are consistent with the system (not the lockfile) when I execute from the Makefile.

Thanks so much for all the help and I would be happy to supply any further info! Doron

  • 1
    Perhaps you could share the contents of your `Makefile`? We have no idea what you're trying to do, how you're calling R, or anything else other than "something is not working correctly". – r2evans Aug 08 '23 at 16:31
  • 2
    Questions on SO (especially in R) do much better if they are reproducible and self-contained. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Aug 08 '23 at 16:32

1 Answers1

1

renv projects are normally activated via a .Rprofile contained in the project folder, which relies on R's working directory matching the directory containing that .Rprofile. To wit, you most likely need to ensure that you set the working directory to the project root directory before launching R.

EDIT: Looking at your Makefile, I think the culprit is this:

R = R --vanilla --args "" < "$<" "$(mkfile_path)" # $< marks the first prerequisite

In this case, you do want the project's .Rprofile to be sourced (since that's how renv activates itself on startup), so you should remove the --vanilla usage.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • Thanks so much r2evans and Kevin for your responses! I edited my project .Rprofile and it now has `setwd(".../clan_wealth/code")` but unfortunately when I run the Makefile it still uses the system packages rather than those from the lockfile... – Doron Shiffer-Sebba Aug 08 '23 at 19:12
  • Dear Kevin, thank you so much for all your help here. Reading the documentation I think you should be totally right and that should have taken care of it. However, unfortunately when I replace `--vanilla` with `--no-save` (or `--save` for that matter) a `sessionInfo()` command in the .R file is still not printing out the lockfile packages... Any other ideas or information that might be helpful? I really appreciate the help!! – Doron Shiffer-Sebba Aug 09 '23 at 01:37