您可以尝试以下方法。
诀窍是您需要从输入(.R 文件)中推断出输出
# Makefile
# Beware of indentation when copying use TABS
PLOTDIR = ../figs
RDIR = ../R
# list R files
RFILES = $(wildcard $(RDIR)/*.R)
# compute output file names
PDF_FIGS = $(RFILES:.R=.pdf)
PNG_FIGS = $(RFILES:.R=.png)
# relocate files in output folder
OUT_FILES = $(subst $(RDIR), $(PLOTDIR), $(PDF_FIGS) $(PNG_FIGS))
# first target is the default: simply do 'make'
all: $(OUT_FILES)
clean:
rm $(OUT_FILES)
.PHONY: all clean
# need to split PNG from PDF rules
$(PLOTDIR)/%.png: $(RDIR)/%.R
R --no-save < $<
$(PLOTDIR)/%.pdf $(PLOTDIR)/%.png: $(RDIR)/%.R
R --no-save < $<
编辑以反映我的评论:每个 R 脚本使用 1 个依赖项输出文件
PLOTDIR= ../figs
RDIR= ../R
# list R files
RFILES := $(wildcard $(RDIR)/*.R)
# relocate files in output folder
OUT_FILES=$(subst $(RDIR), $(PLOTDIR), $(RFILES:.R=.out))
#$(warning $(OUT_FILES))
# first target is the default: simply do 'make'
all: $(OUT_FILES)
clean:
rm $(OUT_FILES)
.PHONY: all clean
$(PLOTDIR)/%.out: $(RDIR)/%.R
R --no-save < $< && touch $@