R命令行参数和makefile

say*_*pta 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()
Run Code Online (Sandbox Code Playgroud)

现在这是我的Makefile.

all :
        make Rplot.pdf
Rplot.pdf : test.R
        cat test.R | R --slave --args 10
Run Code Online (Sandbox Code Playgroud)

现在的问题是如何提供--args(在这种情况下为10)这样我可以说一些可能是这样的事情使得Rplot.pdf -10

我理解它更像是一个makefile问题,而不是一个R问题.

任何帮助是极大的赞赏

问候,

萨扬

Dir*_*tel 5

你有两个问题.

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

和更多.

第二个问题涉及基本的Makefile语法和用法,是的,网上还有很多教程.你基本上提供它们类似于shell参数.这是例如我的Makefile的一部分(来自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)

[...]
Run Code Online (Sandbox Code Playgroud)