我正在編寫一個生成Makefile
的R程序包,我需要編寫一個Makefile
,在制定目標之前調用Rscript
。以下是MWE的問題。由於.INIT
的右側未執行,因此錯誤退出。編寫file.rds
的食譜不適合我的需求。稱爲Rscript的便攜式Makefiles
a=1
.INIT=`Rscript -e 'saveRDS('$(a)', "file.rds")'`
all: file2.rds
file2.rds: file.rds
cp file.rds file2.rds
clean:
rm file.rds file2.rds
我能做些什麼來解決這個Makefile
並保持便攜?從R擴展手冊中,我不能使用$(shell
來實現我想要完成的任務。
編輯
從@ Spacedman的第一個答案,我學會了當且僅當它的地方作爲變量.INIT
被「擴大」 /執行。太棒了! @Spacedman,我邀請您將以下Makefile
複製到您自己的答案中,以便我可以給您信用。
a=1
.INIT=`Rscript -e 'saveRDS('$(a)', "file.rds")'`
all: file2.rds
file2.rds:
echo "file.rds should not have been built."
file3.rds:
echo -n $(.INIT)
cp file.rds file3.rds
clean:
rm file.rds file2.rds
以下演示了我所希望的結果。
$ make file2.rds
echo "file.rds should not have been built."
file.rds should not have been built.
$ ls file.rds
ls: cannot access file.rds: No such file or directory
$ make file3.rds
echo -n `Rscript -e 'saveRDS('1', "file.rds")'`
cp file.rds file3.rds
$ ls file.rds
file.rds
什麼錯誤?您是否希望Makefile通過.INIT創建'file.rds',然後將其視爲file2.rds的依賴項? – Spacedman
是的,確切地說。可笑,我知道,但實際使用情況不同,足以說明問題。 – landau
如果你不能使用'$(shell)並且你不能使用配方,我懷疑你是塞滿的,我沒有看到運行外部命令的另外一種方式 – Spacedman