Skip to content
Back to formatted view

Raw Message

Message-ID: <CABdHhvEz=vrFuhLwp1nW8dmiw4EmHZNok+MPUsoog+X0u1FPoQ@mail.gmail.com>
Date: 2012-10-30T13:52:03Z
From: Hadley Wickham
Subject: print and execute functions in a package namespace
In-Reply-To: <508FD80F.90408@yorku.ca>

> But I need to work with the names of the figure functions instead, something
> like
>
> figlist <- paste0("fig", 1:3)

Are the functions exported or internal?

# Use for internal functions
pkg <- asNamespace("mypackage")
# Use for exported functions:
pkg <- "package:mypackage"

# Find functions matching a pattern:
figfuns <- grep("fun.*", ls(pkg), value = TRUE)

# Convert names into functions
funs <- lapply(fig, get, envir = pkg)

onefig2 <- function(fig) {
    cat("Figure:", fig, "\n")
    print(body(fig))
    fig()
}
lapply(funs, onefig2)

Hadley

-- 
RStudio / Rice University
http://had.co.nz/