2012-01-17 41 views
1

我遇到了makefile和R程序相结合的问题,它接受命令行参数 我在编写makefile时经验不足。R命令行参数和makefile

只是为了做一个例子 我写了一个R文件,它接受命令行参数并生成一个图。 这里是我的文件test.R

args <- commandArgs(trailingOnly=TRUE) 
    if (length(args) != 1) { 
    cat("You must supply only one number\n") 
    quit() 
    } 
    inputnumber <- args[1] 
    pdf("Rplot.pdf") 
    plot(1:inputnumber,type="l") 
    dev.off() 

现在,这里是我的Makefile。

all : 
     make Rplot.pdf 
Rplot.pdf : test.R 
     cat test.R | R --slave --args 10 

现在的问题是如何(在这种情况下10)供应--args这样我就可以说 东西可能是这样的化妆Rplot.pdf -10

我理解它更多makefile文件问题而不是R问题。

任何帮助是极大的赞赏

问候,

萨彦

回答

5

这里有两个问题。

第一个问题是关于命令行参数解析,我们已经在网站上有几个问题。请搜索“[r] optparse getopt”以查找例如

多。

第二个问题涉及基本的Makefile语法和用法,是的,网络上也有大量的教程。而且你基本上提供了类似shell的参数。这里是例如生成文件的矿(从RInside实施例),其中我们查询R键命令行标志等的一部分:

## comment this out if you need a different version of R, 
## and set set R_HOME accordingly as an environment variable 
R_HOME :=  $(shell R RHOME) 

sources :=  $(wildcard *.cpp) 
programs :=  $(sources:.cpp=) 


## include headers and libraries for R 
RCPPFLAGS := $(shell $(R_HOME)/bin/R CMD config --cppflags) 
RLDFLAGS :=  $(shell $(R_HOME)/bin/R CMD config --ldflags) 
RBLAS :=  $(shell $(R_HOME)/bin/R CMD config BLAS_LIBS) 
RLAPACK :=  $(shell $(R_HOME)/bin/R CMD config LAPACK_LIBS) 

## include headers and libraries for Rcpp interface classes 
RCPPINCL :=  $(shell echo 'Rcpp:::CxxFlags()' | \ 
          $(R_HOME)/bin/R --vanilla --slave) 
RCPPLIBS :=  $(shell echo 'Rcpp:::LdFlags()' | \ 
          $(R_HOME)/bin/R --vanilla --slave) 


## include headers and libraries for RInside embedding classes 
RINSIDEINCL := $(shell echo 'RInside:::CxxFlags()' | \ 
          $(R_HOME)/bin/R --vanilla --slave) 
RINSIDELIBS := $(shell echo 'RInside:::LdFlags()' | \ 
          $(R_HOME)/bin/R --vanilla --slave) 

[...] 
+0

非常感谢答案 – 2012-01-27 08:40:57

2

您可以定义命名参数如下:

$ cat Makefile 
all: 
    echo $(ARG) 

$ make ARG=1 all 
echo 1 
1 

您还可以使用Rscript test.R 10代替cat test.R | R --slave --args 10

+0

嘿感谢您的回答。但问题是如何通过Makefile提供参数?我在我的test.R文件中使用了commandArgs() – 2012-01-17 14:39:35

+0

我误解了这个问题......我编辑了我的答案。 – 2012-01-17 14:43:52

+0

嘿谢谢一吨 – 2012-01-27 08:41:06