让对象处于其名称很重要的情况下

发布于 2024-11-26 13:45:47 字数 698 浏览 2 评论 0原文

我有一个函数列表,我想为其制作文档。我的问题不是如何做到这一点,而是它提供了一个我好奇的事情的方便示例。

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

I have a list of functions that I'd like to make documentation for. My question is not about how to do this, but it provides a convenient example of something I'm curious about.

prompt takes a function and a character string as arguments, and writes a help file on that function to the file represented by the character string path. In looping over the files, using prompt(f,filename=...) doesn't work since f is of type character. I tried get(f), which pulls the function out just fine, but doesn't give prompt enough information to work with (see below). So how do I force a character element to return the whole object not just the function that it names?

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

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

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

发布评论

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

评论(1

红颜悴 2024-12-03 13:45:47

?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()

?prompt tells us that

Arguments:

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

So I think prompt() already does what you want:

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

Which does produce the relevant Rd file:

> 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~~
....

I should add, that get("foo") does return the actual function, it is just the way that prompt() is coded that it can't work with an anonymous function as returned by get().

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