[R] See source code for survplot function in Design package

Marc Schwartz marc_schwartz at comcast.net
Thu Feb 5 19:03:37 CET 2009


on 02/05/2009 10:54 AM Eleni Rapsomaniki wrote:
> Dear R users,
> 
> I know one way to see the code for a hidden function, say function_x, is
> using default.function_x (e.g. summary.default). But how can I see the
> code for imported packages that have no namespace (in this case Design)?
> 
> Many Thanks
> Eleni

The easiest way is probably to use getAnywhere():

library(Design)

> getAnywhere("survplot")
A single object matching ‘survplot’ was found
It was found in the following places
  package:Design
with value

function (fit, ...)
UseMethod("survplot")


This tells you that the function is in package Design and has certain
dispatch methods associated with it. There is also the absence of any
indication of a namespace being present.

The next step would be:

> methods(survplot)
[1] survplot.Design
[2] survplot.residuals.psm.censored.normalized
[3] survplot.survfit

which tells you which methods are present for the function. Note also
that there is no 'default' method.

Note that the methods for survplot() are not hidden within a namespace,
as they would normally be followed by a '*' in the output of methods().
Had this been the case, you could use:

  Design:::survplot.Design


Thus, you can just use:

> survplot.Design
function (fit, ..., xlim, ylim = if (loglog) c(-5, 1.5) else if (what ==
    "survival" & missing(fun)) c(0, 1), xlab, ylab, time.inc,
    what = c("survival", "hazard"), type = c("tsiatis", "kaplan-meier"),
    conf.type = c("log-log", "log", "plain", "none"), conf.int = FALSE,
    conf = c("bars", "bands"), add = FALSE, label.curves = TRUE,
    abbrev.label = FALSE, lty, lwd = par("lwd"), col = 1, adj.subtitle,
    loglog = FALSE, fun, n.risk = FALSE, logt = FALSE, dots = FALSE,
    dotsize = 0.003, grid = FALSE, srt.n.risk = 0, sep.n.risk = 0.056,
    adj.n.risk = 1, y.n.risk, cex.n.risk = 0.6, pr = FALSE)
{
    what <- match.arg(what)
    if (.R.)
        ylim <- ylim
    type <- match.arg(type)
    conf.type <- match.arg(conf.type)
    conf <- match.arg(conf)
    psmfit <- inherits(fit, "psm") || (length(fit$fitFunction) &&
        any(fit$fitFunction == "psm"))
    if (what == "hazard" && !psmfit)
        stop("what=\"hazard\" may only be used for fits from psm")
    if (what == "hazard" & conf.int > 0) {
        warning("conf.int may only be used with what=\"survival\"")
        conf.int <- FALSE
    }
...


and the same for the other methods for the function.

In general, for methods that are within a namespace, you can use:

  namespace:::function.method

or:

  getAnywhere("function.method")


See ?getAnywhere and ?methods

HTH,

Marc Schwartz




More information about the R-help mailing list