在物品名称重要的情况下获取物品

时间:2011-07-28 13:24:20

标签: r evaluation

我有一份我想为其制作文档的函数列表。我的问题不是关于如何做到这一点,但它提供了一个我很好奇的方便的例子。

prompt将函数和字符串作为参数,并将该函数的帮助文件写入由字符串路径表示的文件。在循环文件时,使用prompt(f,filename=...)不起作用,因为f是字符类型。我尝试了get(f),它可以很好地提取功能,但是没有给出足够快速的信息(见下文)。那么如何强制一个字符元素返回整个对象而不仅仅是它命名的函数?

files <- c("current.market","current.portfolio.bond","fund","genAccount","genHistory.market","history.market","maRketSim.version","summary.vasicek.discrete","vasicek.discrete")
for(f in files) {
  prompt( get(f), filename=paste("c:/myproject/man/",f,".Rd",sep="") )
}
Error in prompt.default(get(f), filename = paste("F:/Documents/R-projects/maRketSim/man/",  : 
  cannot determine a usable name

1 个答案:

答案 0 :(得分:2)

?prompt告诉我们

Arguments:

  object: an R object, typically a function for the default method.
          Can be ‘missing’ when ‘name’ is specified.

所以我认为prompt()已经做了你想要的事情:

> prompt(name = "print", filename = "print.Rd")
Created file named 'print.Rd'.
Edit the file and move it to the appropriate directory.

哪个产生相关的Rd文件:

> writeLines(readLines("~/print.Rd"))
\name{print}
\alias{print}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
%%  ~~function to do ... ~~
}
\description{
%%  ~~ A concise (1-5 lines) description of what the function does. ~~
}
\usage{
print(x, ...)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
  \item{x}{
%%     ~~Describe \code{x} here~~
....

我应该补充一点,get("foo") 确实会返回实际的函数,这就是prompt()编码的方式,它无法使用匿名函数由get()返回。