R语言选项参数2021.3.7

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言选项参数2021.3.7相关的知识,希望对你有一定的参考价值。

参考技术A file:接一个文件
data:一般指要输入一个数据框;
x:表示单独的一个对象,一般都是向量,也可以是矩阵或者列表;
x和y:函数需要两个输入变量;
x,y,z:函数需要三个输入变量;
formula:公式;
...:,在help文档中的三个点表示参数可传递,或者表示参数没有数量限制
na.rm:删除缺失值;

color选项和明显用来控制颜色
select 与选择有关
font与字体有关
font.axis 就是坐标轴的字体
lty 是line type,线条类型
lwd是line width,线条宽度
method 软件算法

main:字符串,不能是向量
na.rm : TRUE或者FALSE
axis : side参数只能是1到4,调节坐标轴方向。1,2,3,4分别代表左下右上
fig:包含四个元素的向量
row:排,行
col:列

对从命令行(optparse)R传递参数感到困惑,即使使用显式“默认”选项也没有存储参数

【中文标题】对从命令行(optparse)R传递参数感到困惑,即使使用显式“默认”选项也没有存储参数【英文标题】:Confused about passing arguments from command line (optparse) R, arguments not getting stored even with explicit 'default' option 【发布时间】:2019-05-04 18:45:22 【问题描述】:

学习如何从命令行使用 R。我遇到了“optparse”包并开始使用它。以为一切都很好,直到我注意到它的行为不像我最初预期的那样,并且无法让它在我想要的目录中写入文件。

为了简单起见,我决定用一个简短的脚本来解释发生了什么:

require(optparse)

#Parse arguments from command line
options <- list(
  make_option(c("-d", "--directory"), action = "store", default = getwd(), type = "character", help="Working directory path."),
  make_option(c("-e", "--extension"), action = "store", default = ".tsv", type = "character", help="File(s) extension."),
  make_option(c("-p", "--outputPath"), action = "store", default = getwd(), type = "character", help="Output file(s) directory to be saved at."),
  make_option(c("-o", "--outputName"), action = "store", default = "output", type = "character", help="Output file(s) base name."),
  make_option(c("-s", "--separator"), action = "store", default = NA, help="Separator to use explicitely")
)
arguments <- parse_args(OptionParser(option_list = options))

setwd(arguments$d) 

cat(arguments$d, arguments$e, arguments$s)

这很好用,它显示:

C:/Users/path/to/where/I/work .tsv NA

但是,要求 'outputPath' 和 outputName' 参数

cat(arguments$p, arguments$o)

绝对不会打印任何内容...即使我明确地为这些参数提供了默认值、类型和操作。 outputPath 选项实际上与 directory 选项相同!

使用 getwd()、as.character(getwd()) 或什至 as.character(file.path(getwd())) 对选项 'outputPath' 给出相同的结果。

从命令行传递参数(而不是使用默认值)返回完全相同的东西(你对 arguments$d、arguments$e、arguments$s 的期望,而对于 arguments$p、arguments 什么都没有$o)

我对此感到非常困惑;当然,当尝试在我的真实脚本中使用这些变量时......这是不可能的,因为它说它们的长度为零......

有趣的是,如果我这样做:

cat(unlist(arguments))

我确实得到了我期望的输出......但我还在我的参数列表的末尾得到了一个值为 FALSE 的附加逻辑变量。不知道从哪里来的……

C:/Users/path/to/where/I/work .tsv C:/Users/path/to/where/I/work output NA FALSE

我在 Windows powershell 上运行它:

Rscript.exe .\script.R

当我输入长标志(例如 --outputName 某些东西)时,标志“点亮”并在终端上变成白色,而使用短标志(例如 -o 东西)不会(它在终端上看起来是灰色的)命令行,好像它是未使用的或什么的)。不知道这是否意味着什么,因为两者都做完全相同的事情。只是想指出来。

最后一件事!当然 'C:/Users/path/to/where/I/work' 不是真正的目录路径,而我实际使用的路径上有一个空格……你认为这重要吗?

编辑:好的,现在我知道 FALSE 来自哪里,只需要打印我的“参数”列表......我觉得很愚蠢。但是,最初的问题仍然存在......现在让我更加困惑,因为这些值存储在我的“参数”列表中。

在做:

arguments

演出:

$directory
[1] "C:/Users/alang/Desktop/LabiVicenteLau D="

$extension
[1] ".tsv"

$outputPath
[1] "C:/Users/alang/Desktop/LabiVicenteLau D="

$output
[1] "output"

$separator
[1] NA

$help
[1] FALSE

【问题讨论】:

【参考方案1】:

参数的部分匹配是你的(我猜不是)朋友。

#!/usr/bin/env Rscript
require(optparse)

#Parse arguments from command line
options <- list(
  make_option(c("-d", "--directory"), action = "store", default = getwd(), type = "character", help="Working directory path."),
  make_option(c("-e", "--extension"), action = "store", default = ".tsv", type = "character", help="File(s) extension."),
  make_option(c("-p", "--outputPath"), action = "store", default = getwd(), type = "character", help="Output file(s) directory to be saved at."),
  make_option(c("-o", "--outputName"), action = "store", default = "output", type = "character", help="Output file(s) base name."),
  make_option(c("-s", "--separator"), action = "store", default = NA, help="Separator to use explicitely")
)
arguments <- parse_args(OptionParser(option_list = options))

setwd(arguments$d)

print("R tries to match anything starting with an o.")
print(arguments$o)
print("No success")
print("R tries not to partially matchi anything. Explicitly call argument.")
print(arguments$outputPath)
print("Great success!")

上面的脚本给了我:

romunov@kista  ~/Documents
$ ./test.R
Loading required package: optparse
Warning message:
package 'optparse' was built under R version 3.5.3
[1] "R tries to match anything starting with an o."
NULL
[1] "No success"
[1] "R tries not to partially matchi anything. Explicitly call argument."
[1] "C:/Users/romunov/Documents"
[1] "Great success!"

【讨论】:

是的,刚刚想通了 :D 谢谢!我认为可能有办法让它按照我最初尝试匹配的方式工作,但现在我会这样做。 您应该按参数的全名匹配参数。你的代码应该是可读的,而不是简短的。

以上是关于R语言选项参数2021.3.7的主要内容,如果未能解决你的问题,请参考以下文章

R语言scale()函数实现数据标准化

怎么利用r语言做em算法估计混合双参数指数分布的数值模拟

R语言ggplot2画横向直方图以及去掉图例

r语言中acf函数的xaxp参数是啥意思

新手:R语言的heatmap的制作问题

R语言笔记 R中设置图形参数--函数par()详解