R 命令行参数和 makefile

发布于 2024-12-27 14:27:32 字数 659 浏览 0 评论 0原文

我在组合 makefile 和接受命令行参数的 R 程序时遇到问题。

一个例子: 我编写了一个 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

现在的问题是如何提供 --args (本例中为 10),以便我可以这样说: make Rplot.pdf -10

我理解它更多是一个 Makefile 问题,而不是 R 问题。

I am having a problem combining makefile and R program which accepts command line arguments.

An example:
I have written an R file that accepts a command line argument and generates a plot.

Code

    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

Now the question is how to supply --args (10 in this case), so that I can say something like this:
make Rplot.pdf -10

I understand its more a Makefile question rather an R question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2025-01-03 14:27:32

你在这里有两个问题。

第一个问题是关于命令行参数解析的,我们已经在网站上提出了几个相关问题。请搜索“[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)

[...]

You have two questions here.

The first question is about command-line argument parsing, and we already had several questions on that on the site. Please do a search for "[r] optparse getopt" to find e.g.

and more.

The second question concerns basic Makefile syntax and usage, and yes, there are also plenty of tutorials around the net. And you basically supply them similar to shell arguments. Here is e.g. a part of Makefile of mine (from the examples of RInside) where we query R to command-line flags etc:

## 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)

[...]
你的呼吸 2025-01-03 14:27:32

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

$ cat Makefile 
all:
    echo $(ARG)

$ make ARG=1 all
echo 1
1

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

You can define named arguments as follows:

$ cat Makefile 
all:
    echo $(ARG)

$ make ARG=1 all
echo 1
1

You can also use Rscript test.R 10 instead of cat test.R | R --slave --args 10
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文