1

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
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235

1 Answers1

2

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

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Thanks Gavin. I just realized a second ago that prompt takes the ,name argument. But I was hoping for something a little more generic: how to fetch an object with its name information intact. I'm not even sure where name information about functions gets stored. Looking through `getS3method("prompt","default")` , it looks like `substitute` is used to return the parse tree, and then that is checked to see if it is a name with `is.name`. But I suspect that `g <- get(f); name(g) <- f` is not only non-working but conceptually wrong. – Ari B. Friedman Jul 28 '11 at 13:43
  • A `name` is just the way to refer to R objects by their name rather than the contents of the object with that name. How to proceed really depends on what you want to do. You could wrap `prompt()` and do various manipulations of `f` to get what you want. But it isn't clear to me what you want to do generically. – Gavin Simpson Jul 28 '11 at 13:54
  • 1
    This question may be relevant: [Getting the object name for S3 print method failing](http://stackoverflow.com/q/4959463/210673) – Aaron left Stack Overflow Jul 28 '11 at 14:06
  • Ok maybe I'll try to find another example of where generically it would be helpful. Basically I'm trying to learn more about programming-on-the-language so every time I run across a problem where `get` `eval` `assign` `bquote` etc. would be helpful I try to think more about them even if they're not the easiest solution. – Ari B. Friedman Jul 28 '11 at 14:08
  • @Aaron Your and Joris' Answers in the linked Q hit the nail on the head nicely. – Gavin Simpson Jul 28 '11 at 14:20
  • @Aaron Thanks. I'll take a look and see if I can't wrap my brain around this. – Ari B. Friedman Jul 28 '11 at 14:21