11

This is likely a newbie's question but I think I did my homework and yet have not found the answer (I hope to find) so I am posting it here to seek some assistance.

Similar questions were asked before but from what I found, no answer could help me with the current issue except an "expensive" solution, which requires an editor for R.

I learned that ls and objects allow us to view the objects inside a package. But even with ls(all.names=TRUE), I still couldn't see all the content. Someone suggested ls(getNAMEspace) but still this isn't "good" enough for me.

e.g.

>search()
[1]".GlobalEvn"      "package:TCGAGBM"
>ls("package:TCGAGBM")
character(0)
>ls(getNamespace("TCGAGBM"),all.names=TRUE)
[1]"._NAMESPACE_."   "._S3MethodsTable_."  ".packageName"

However, under C (cmd), I see the following

C:\Users\XYZ\Documents\R\R-2.15.1\library\TCGAGBM . .. data extdata ...... (total 3 File(s), 7 Dir(s))

I came across this "discrepancy" when I saw the following line of script -

>clinical=read.delim(system.file(
+"extdata/Clinical/clinical_patient_public_GBM.txt.gz",
+package="TCGAGBM"), header=TRUE)

Thus I was wondering if there is a way under R to see ALL the content within a package so that we could "know" how better to utilize the package. Vignette would probably help, but in my limited experience with R so far, I have found that some packages did not come with Vignette.

Any comment will be appreciated to help me learn more about R.

5
  • 2
    Do you want to see all the source code files etc? If you want to see contents of the package directory, try something like list.files(system.file(package = 'TCGAGBM'), recursive = T, full.names = T)) - but this will be OS dependent as to how much you actually see due to the way R packages are packaged for windows / linux etc
    – mnel
    Sep 25, 2012 at 1:42
  • maybe data(package='TCGAGBM')
    – GSee
    Sep 25, 2012 at 1:42
  • According to googling TCGAGBM r package, there seems to be a tutorial for this package at watson.nci.nih.gov/~sdavis/tutorials/TCGA_data_integration . Perhaps better to start by following that? Digging into the internals of a package is a last resort ...
    – Ben Bolker
    Sep 25, 2012 at 1:43
  • @mnel, post comment as answer?
    – Ben Bolker
    Sep 25, 2012 at 2:07
  • great answer ls(getNamespace("TCGAGBM"),all.names=TRUE) 👍👍
    – mdsumner
    Mar 23, 2020 at 4:35

3 Answers 3

5

My preferred approach by far is to simply look at the source code of a package in question.

In fact, I actually do that pretty often as running CRANberries creates a local CRAN mirror as a side effect. But even if you don't, CRAN packages really are only a quick download away and will come with comments in the source which the parsed code excludes.

Edit: I just found what Ben found too: Sean Davis' page at http://watson.nci.nih.gov/~sdavis/tutorials/TCGA_data_integration/ -- looks like it uses some BioC packages too. I would still study the source which often has more comments, annotations, extras, ... than the installed package. But maybe that's just my preference. YMMV as they say.

2
  • Thanks a lot, guys. To mnel, I think for now list.files will do the job. To Ben, I read the tutorial but was thinking (while reading it) what if I didn't have the tutorial, how would I know there is an "extdata" to extract more info? Thanks A LOT for all helpful info as I am just a 3 month R baby still finding my way.
    – B Chen
    Sep 25, 2012 at 1:56
  • @BChen: My pleasure, and welcome to StackOverflow. It is customary to upvote a good answer (which you may or may not have done) and to accept one if it is seen as helpful. Sep 25, 2012 at 2:51
5

Here is another way to explore the functionality of any package. Although it is not as comprehensive a solution as Dirk's, it is still useful. When I want to know all the functionality of a package, I quickly list all of its functions. Then if I'm curious about any function, I an quickly pull up the help file ?function_name and see what it does. For that reason, I keep this function in my .rprofile so it automatically loads every time I run R.

lsp <- function (package, all.names = FALSE, pattern) {
    package <- deparse(substitute(package))
    ls(pos = paste("package", package, sep = ":"), all.names = all.names, 
        pattern = pattern)
}

This is especially helpful when I know the partial name of a function and what package it belongs to but quickly need to find it.

e.g.

> lsp(ggplot2, pattern = "geom")
 [1] "geom_abline"          "geom_area"           
 [3] "geom_bar"             "geom_bin2d"          
 [5] "geom_blank"           "geom_boxplot"        
 [7] "geom_contour"         "geom_crossbar"       
 [9] "geom_density"         "geom_density2d"      
[11] "geom_dotplot"         "geom_errorbar"       
[13] "geom_errorbarh"       "geom_freqpoly"       
[15] "geom_hex"             "geom_histogram"      
[17] "geom_hline"           "geom_jitter"         
[19] "geom_line"            "geom_linerange"      
[21] "geom_map"             "geom_path"           
[23] "geom_point"           "geom_pointrange"     
[25] "geom_polygon"         "geom_quantile"       
[27] "geom_raster"          "geom_rect"           
[29] "geom_ribbon"          "geom_rug"            
[31] "geom_segment"         "geom_smooth"         
[33] "geom_step"            "geom_text"           
[35] "geom_tile"            "geom_violin"         
[37] "geom_vline"           "update_geom_defaults"
3
  • That's nice. I had been using TAB in ESS which gives about the same, but this filters the internal names away. Obnoxious note: You do have the opening brace in the wrong spot though :) R Core style all the way. Sep 25, 2012 at 13:33
  • Is it just too hard to remember this syntax ls('package:ggplot2', pattern='geom')? If you're going to look at help pages for functions next anyway, maybe help(package='ggplot2', help_type='html') is more appropriate.
    – GSee
    Sep 25, 2012 at 16:16
  • It's a great idea. The only minor quirk is that the package to be listed needs to have been previously imported using library(). Oct 27, 2020 at 21:50
4

If you want to see all the system files for a particular package, then try something like

list.files(system.file(package = 'TCGAGBM'), recursive = T, full.names = T)

How useful this will be will depend on your OS, as the way packages are installed is OS dependent

see the appropriate section in R Installation and Administration manual for more details.

NOTE

@DirkEddelbuettel's suggestion of inspecting the source is a far better approach.

1
  • will follow up on this, but as of now, the baby cannot know how to use a "dictionary" yet
    – B Chen
    Sep 25, 2012 at 3:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.