4

I am writing a document with knitr and latex on linux. I made a small makefile to automate the build. It works perfectly fine on linux, but it is problematic on windows. Issuing make I get

C:\Users\samde_000\Documents\GaussianProcess-Course>make
mkdir -p build && \
    cp src/GPcourse.Rnw build/GPcourse.Rnw && \
    cd build && \
    Rscript -e "library(knitr); knit(\"GPcourse.Rnw\")"
Error in library(knitr) : there is no package called 'knitr'
Execution halted

So I checked this to pin down the problem:

C:\Users\samde_000\Documents\GaussianProcess-Course>Rscript -e ".libPaths()"
[1] "C:/Users/samde_000/Documents/R/win-library/3.2"
[2] "C:/Program Files/R/R-3.2.2/library"

C:\Users\samde_000\Documents\GaussianProcess-Course>Rscript -e "library(knitr)"

So when using make, the package cannot be found. But manually issuing the same commands, the package can be found.

To absolutely make sure that difference is due to using make, I altered the Makefile a little to output the R search path:

C:\Users\samde_000\Documents\GaussianProcess-Course>make
mkdir -p build && \
    cp src/GPcourse.Rnw build/GPcourse.Rnw && \
    cd build && \
    Rscript -e ".libPaths()" && \
    Rscript -e "library(knitr); knit(\"GPcourse.Rnw\")"
[1] "C:/Program Files/R/R-3.2.2/library"
Error in library(knitr) : there is no package called 'knitr'
Execution halted

This shows that when make issues the commands. The search path is somehow altered to only contain "C:/Program Files/R/R-3.2.2/library".

If I am correct, make issues all recipes in a subshell. So maybe this is causing trouble somehow? Anyway, I don't have much experience with windows, and I don't know how I can keep the same R search path while using make.

The reason why I am asking this is because a co-author works on windows. So the makefile should be compatible with both linux and windows.

bschneidr
  • 6,014
  • 1
  • 37
  • 52
Sam De Meyer
  • 2,031
  • 1
  • 25
  • 32
  • Are you adding `C:/Users/samde_000/Documents/R/win-library/3.2` to the R libpaths in some shell startup file or similar? – Etan Reisner Nov 06 '15 at 14:52
  • Not that I know of at least. But maybe some installer did it for me. Is that possible? – Sam De Meyer Nov 06 '15 at 15:07
  • Admittedly, I know basically nothing about makefiles, but I suppose `Rscript` might be launched under a different user account when using the makefile. You could verify this by executing `echo %username%` both within and outside the makefile. – CL. Nov 06 '15 at 15:20
  • Certainly possible. Are you running this all from `cmd.exe`, a cygwin shell or something else? – Etan Reisner Nov 06 '15 at 17:05
  • `echo %username%` prints my user name when I type the command in the `cmd.exe` shell, but printing it from the makefile it simply returns `%username%` again. In other words, the `username` variable is not there. – Sam De Meyer Nov 06 '15 at 18:36
  • Why don't you replace `library(knitr)` by something like `if (length(find.package(package = "knitr", quiet = TRUE, verbose = FALSE)) == 0) { install.packages("knitr") }`? This doesn't answer the question *why* `knitr` cannot be found but it should solve the problem. – CL. Nov 07 '15 at 08:10
  • Do you have multiple versions of R installed on the windows machine? – Thomas Nov 08 '15 at 20:35
  • Can you show us what `$(info $(SHELL))` prints when you put it at the beginning of your makefile? Or is there an assignment to `SHELL := ...` somewhere in the makefile? Which `make` do you use on Windows? – Vroomfondel Mar 19 '21 at 16:16
  • At least for me, there's no assignment to `SHELL := ...` in the makefile. When I put `$(info $(SHELL))` at the top of the make file, the result is `sh`. And when I add `where sh`, I get: `C:\rtools40\usr\bin\sh.exe`. Here's what I get when put `where make` at the top of the make file: `C:\rtools40\usr\bin\make.exe C:\ProgramData\chocolatey\bin\make.exe` – bschneidr Mar 19 '21 at 17:44
  • @bschneidr Can you invoke a test call *also* from the `build` directory, i.e. the one where the makefile changes to, before calling `Rscript`? I don't think this problem has to do with `make`, maybe `Rscript` tries to be too clever with working directories and paths? – Vroomfondel Mar 22 '21 at 15:51
  • @bschneidr The last thing I ask you to check is if there is a difference between `set` (ie the set of all environment variables) from your command line vs. a `$(info $(shell set))` from inside the makefile. Maybe the shell which is invoked by `make` is not invoked under your user, thereby missing out on any user-reserved paths. – Vroomfondel Mar 23 '21 at 08:44

0 Answers0