【问题标题】:Determining argument descriptions within R在 R 中确定参数描述
【发布时间】:2014-03-06 08:32:49
【问题描述】:

我需要一种方法来确定 R 中参数的描述。

例如,如果我使用 ggplot2 包中的函数 qplot(),我需要能够提取 qplot() 中每个参数的描述。从ggplot2's reference manual,很容易看出有几个参数需要考虑。其中之一称为“数据”,其描述为:“要使用的数据框(可选)。如果未指定,将创建一个,从当前环境中提取向量。”

有没有办法从 R 会话中获取这些信息,而不是通过阅读参考手册?也许是一个类似于 packageDescription() 的 R 函数,但用于函数的参数?

谢谢!


编辑:我在这里找到了我的问题的一个变体:

How to access the help/documentation .rd source files in R?

阅读 .Rd 文件似乎是获取所需信息的最安全方法。对于任何感兴趣的人,以下代码会返回参数列表及其描述,其中“package_name”可以是您想要的任何包:

db <- Rd_db("package_name")
lapply(db, tools:::.Rd_get_metadata, "arguments")

谢谢大家的帮助。

【问题讨论】:

  • args() 函数将列出所有函数参数(例如 args(qplot)。但我不确定这是否是您想要的。您也可以从命令访问函数的帮助文件与 ?function 一致(例如 ?qplot)。最后,formals() 将提取函数参数作为列表,如果您希望以编程方式使用它们(例如 formals(qplot))。
  • @matt_k 我知道存在用于确定参数名称的函数,但我正在寻找返回它们描述的东西。现在,我最好的解决方案是将包的参考手册从 pdf 格式转换为 txt 并扫描“参数”部分。但这种方法有点容易出错。
  • R 的帮助页面以.Rd 格式编写。然后由所选浏览器显示。您需要阅读该格式,而不是尝试反编译 pdf 版本。检查`?` 的输出。
  • 回到问题:你究竟为什么需要这个?我不禁想到如果你使用 会发生什么,例如,par()。我强烈建议不要做你要求的事情。

标签: r arguments


【解决方案1】:

从 Mac GUI R.app 中的 R 控制台...当我查看来自 help'seq', help_type="text") 的文本输出(转到临时文件)时,我看到您想要的 hte 描述的开头由以下内容划分:

_A_r_g_u_m_e_n_t_s:   # Those underscores were ^H's before I pasted

然后出现的参数是name:description对:

     ...: arguments passed to or from methods.

from, to: the starting and (maximal) end values of the sequence.  Of
          length ‘1’ unless just ‘from’ is supplied as an unnamed
          argument.

      by: number: increment of the sequence.

length.out: desired length of the sequence.  A non-negative number,
          which for ‘seq’ and ‘seq.int’ will be rounded up if
          fractional.

along.with: take the length from the length of this argument.

当我使用终端会话获得相同的输出时,它会出现在同一个窗口中,但显示为 Unix 帮助页面,例如:

Arguments:

     ...: arguments passed to or from methods.

from, to: the starting and (maximal) end values of the sequence.  Of
          length ‘1’ unless just ‘from’ is supplied as an unnamed
          argument.

      by: number: increment of the sequence.

length.out: desired length of the sequence.  A non-negative number,
          which for ‘seq’ and ‘seq.int’ will be rounded up if
          fractional.

along.with: take the length from the length of this argument.

我相信这些是由options("pager") 的值调用的任何系统程序显示的。就我而言,这就是程序“less”。

【讨论】:

  • 有没有办法以编程方式访问这些帮助文件?例如,是否可以将描述保存为字符对象?
猜你喜欢
  • 2021-05-06
  • 2015-12-10
  • 2014-11-17
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多