[R] How to see how a function is written
Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Thank you for help! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
On Tue, 2010-06-15 at 14:20 +0200, Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. In what sense does it not work? If I do this, I get: apply function (X, MARGIN, FUN, ...) { FUN - match.fun(FUN) d - dim(X) dl - length(d) if (dl == 0L) stop(dim(X) must have a positive length) ds - 1L:dl if (length(oldClass(X))) X - if (dl == 2) Perhaps you a referring to S3 methods (and just used apply as an easy example)? In which case you could try: fitted function (object, ...) UseMethod(fitted) environment: namespace:stats methods(fitted) [1] fitted.default* fitted.isoreg*fitted.nls* [4] fitted.smooth.spline* Non-visible functions are asterisked fitted.isoreg Error: object 'fitted.isoreg' not found ## hmm, not found, use getAnywhere instead getAnywhere(fitted.isoreg) A single object matching ‘fitted.isoreg’ was found It was found in the following places registered S3 method for fitted from namespace stats namespace:stats with value function (object, ...) { if (object$isOrd) object$yf else object$yf[order(object$ord)] } environment: namespace:stats Alternatively, use: getS3method(fitted, isoreg) You might also consult Uwe Ligges 2006 article in R-News (now The R Journal): Uwe Ligges. R Help Desk: Accessing the sources. R News, 6(4):43-45, October 2006. http://cran.r-project.org/doc/Rnews/Rnews_2006-4.pdf If that doesn't help, can you show exactly what you types, what the error was and what you were looking for, along with your R version etc. My system/R version info (used for the above) is: sessionInfo() R version 2.11.1 Patched (2010-06-14 r52272) x86_64-unknown-linux-gnu locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_GB.UTF-8LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.11.1 HTH G Thank you for help! Sergey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day14 svn rev50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party Genuine Replica Toxic Assets Italian Government Feminine Logic Amicable Divorce Military Intelligence Money Multiplier Fiscal Conservative Abundant Poverty Educated Investor Government Worker Green Shoots Hope and Change Change you can believe in Becky Quick __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day14 svn rev50720 language R version.string R version 2.10.1 (2009-12-14) And what packages have you loaded? That *is* your current definition of apply, I'm guessing some package is changing the base definition of 'apply', since that's not what happens in my R --vanilla session. Regarding mean, you don't show your output, but my guess is that it *is* printing the function definition, which is generic. Try typing out mean.default and see what happens. What happens when you type lm ? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.defaultmean.difftime [5] mean.POSIXctmean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day14 svn rev50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to see how a function is written
Maybe I have to much stuff loaded in the workspace, Gavin, you are right: sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 [4] LC_NUMERIC=CLC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13TTR_0.20-1 Defaults_1.1-1 xts_0.7-0 [6] fPortfolio_2100.78 Rglpk_0.3-5slam_0.1-9 fAssets_2100.78fCopulae_2110.78 [11] sn_0.4-14 mnormt_1.3-3 fBasics_2110.79timeSeries_2110.87 timeDate_2110.87 [16] robustbase_0.5-0-1 quadprog_1.4-12MASS_7.3-5 fEcofin_290.76 foreach_1.3.0 [21] codetools_0.2-2iterators_1.0.3zoo_1.6-3 loaded via a namespace (and not attached): [1] grid_2.10.1lattice_0.18-3 tools_2.10.1 On Tue, Jun 15, 2010 at 14:56, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day 14 svn rev 50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide requests, OS, versions, etc. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% -- Famous Oxymorons: Jobless Recovery Jumbo Shrimp War Game Wedding Party
Re: [R] How to see how a function is written
On Tue, 2010-06-15 at 14:56 +0200, Sergey Goriatchev wrote: Maybe I have to much stuff loaded in the workspace, Gavin, you are right: OK, so now do showMethods(apply) And R should list out the available methods. See which package (re)defines apply. But it is likely going to be simpler to start a clean session and look at the code in there. If you need the S4 method/generic code then you'll have to find out which package is redefining apply and look in the sources for that package. HTH G sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 [4] LC_NUMERIC=CLC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13TTR_0.20-1 Defaults_1.1-1 xts_0.7-0 [6] fPortfolio_2100.78 Rglpk_0.3-5slam_0.1-9 fAssets_2100.78fCopulae_2110.78 [11] sn_0.4-14 mnormt_1.3-3 fBasics_2110.79timeSeries_2110.87 timeDate_2110.87 [16] robustbase_0.5-0-1 quadprog_1.4-12MASS_7.3-5 fEcofin_290.76 foreach_1.3.0 [21] codetools_0.2-2iterators_1.0.3zoo_1.6-3 loaded via a namespace (and not attached): [1] grid_2.10.1lattice_0.18-3 tools_2.10.1 On Tue, Jun 15, 2010 at 14:56, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.defaultmean.difftime [5] mean.POSIXctmean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day14 svn rev50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just typing apply at the prompt does not work. Well, it is supposed to work, and it works for me. So you need to tell us what does not work means, and all the info the posting guide
Re: [R] How to see how a function is written
showMethods(apply) Function: apply (package base) X=ANY X=missing (inherited from: X=ANY) X=timeSeries On Tue, Jun 15, 2010 at 15:10, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:56 +0200, Sergey Goriatchev wrote: Maybe I have to much stuff loaded in the workspace, Gavin, you are right: OK, so now do showMethods(apply) And R should list out the available methods. See which package (re)defines apply. But it is likely going to be simpler to start a clean session and look at the code in there. If you need the S4 method/generic code then you'll have to find out which package is redefining apply and look in the sources for that package. HTH G sessionInfo() R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252 LC_MONETARY=German_Switzerland.1252 [4] LC_NUMERIC=C LC_TIME=German_Switzerland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] PerformanceAnalytics_1.0.0 quantmod_0.3-13 TTR_0.20-1 Defaults_1.1-1 xts_0.7-0 [6] fPortfolio_2100.78 Rglpk_0.3-5 slam_0.1-9 fAssets_2100.78 fCopulae_2110.78 [11] sn_0.4-14 mnormt_1.3-3 fBasics_2110.79 timeSeries_2110.87 timeDate_2110.87 [16] robustbase_0.5-0-1 quadprog_1.4-12 MASS_7.3-5 fEcofin_290.76 foreach_1.3.0 [21] codetools_0.2-2 iterators_1.0.3 zoo_1.6-3 loaded via a namespace (and not attached): [1] grid_2.10.1 lattice_0.18-3 tools_2.10.1 On Tue, Jun 15, 2010 at 14:56, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: On Tue, 2010-06-15 at 14:38 +0200, Sergey Goriatchev wrote: Erik, I see the following when I type apply at the prompt: apply standardGeneric for apply defined from package base Looks like you have something loaded in your workspace (or have created something) that has altered the usual definition of apply(). Most likely is a package has made the base apply() function an S4 method. Send the output of sessionInfo() to the list so we can help if you interest is in the S4 method version of apply() (myself I'm not too familiar with S4 methods just yet). If you start R in a clean session, you should see the normal definition of apply R --vanilla apply On Windows you may need to add that option to the shortcut you use to start R. You could also try base:::apply to see the version in the base R namespace (at least I think that should work). function (X, MARGIN, FUN, ...) standardGeneric(apply) environment: 0x03cad7d0 Methods may be defined for arguments: X, MARGIN, FUN Use showMethods(apply) for currently available ones. Also, whether I type mean at the prompt, or I type edit(mean), I do not see the underlying code for function mean. How would I be able to see it? The info I sent in my previous email should help you with the mean function --- as long as that hasn't been overwritten by anything. methods(mean) [1] mean.data.frame mean.Date mean.default mean.difftime [5] mean.POSIXct mean.POSIXlt getS3method(mean, default) function (x, trim = 0, na.rm = FALSE, ...) { if (!is.numeric(x) !is.complex(x) !is.logical(x)) { warning(argument is not numeric or logical: returning NA) return(NA_real_) } if (na.rm) x - x[!is.na(x)] if (!is.numeric(trim) || length(trim) != 1L) stop('trim' must be numeric of length one) n - length(x) if (trim 0 n) { if (is.complex(x)) stop(trimmed means are not defined for complex data) if (any(is.na(x))) return(NA_real_) if (trim = 0.5) return(stats::median(x, na.rm = FALSE)) lo - floor(n * trim) + 1 hi - n + 1 - lo x - sort.int(x, partial = unique(c(lo, hi)))[lo:hi] } .Internal(mean(x)) } environment: namespace:base Although here, none of the mean methods are hidden so you could just type their names directly. The meaning of the .Internal( ) bit is that this calls internal C code. Uwe Ligges article discusses what to do at this point. HTH G --- My machine: platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 10.1 year 2009 month 12 day 14 svn rev 50720 language R version.string R version 2.10.1 (2009-12-14) On Tue, Jun 15, 2010 at 14:26, Erik Iverson er...@ccbr.umn.edu wrote: Sergey Goriatchev wrote: Hello, If I want to see how, say, apply function is written, how would I be able to do that? Just