简体   繁体   中英

Makefile for building an R package linked to an analysis

Suppose I have a project for which I have developed an R package. The hierarchy might look something like this.

/project
---Makefile
---workflow.R
---test.R
---/mypackage
    ---DESCRIPTION
    ---NAMESPACE
    ---/R
        ---func1.R
        ---func2.R

workflow.R depends on the latest version of mypackage being installed. However, I only want to re-build the package if any file inside of it has been modified.

Currently, in my Makefile, I have:

PACKAGE=$(wildcard mypackage/**/*)

all: install test workflow

install: $(PACKAGE)
    R CMD INSTALL mypackage

workflow: install
    Rscript workflow.R

test: install
    Rscript test.R

However, this will re-install the package every time I run make test , even if nothing inside the package has changed. Is there a clean way to avoid this?

The install rule does not create a file named install in the current directory, so make tries to remake it each time. This looks like it should be a .PHONY target, but that itself won't fix the issue as it will still execute the recipes.

One solution is to have another rule that creates a stub file:

.PHONY: all install test workflow

all: install test workflow

install: install.done
install.done: $(PACKAGE)
    R CMD INSTALL mypackage
    touch $@

Or you could just make install the stub file itself and make it a non- .PHONY rule.

It sounds like you want to treat the installation as an intermediate step. You can do this by adding

.INTERMEDIATE: install

to your makefile.

The make manual explains ( link ):

If an ordinary file b does not exist, and make considers a target that depends on b, it invariably creates b and then updates the target from b. But if b is an intermediate file, then make can leave well enough alone. It won't bother updating b, or the ultimate target, unless some prerequisite of b is newer than that target or there is some other reason to update that target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM