Is it possible to scan all the packages on CRAN in order to find all those that are dependent on a specific package?
I have found out how to do that but only on the packages I have installed and that is not what I want.
If you don't care about doing it programmatically, the easiest way is to look at the corresponding CRAN web page (i.e. https://CRAN.R-project.org/package=PKGNAME) and look at the "Reverse depends:", "Reverse imports:", and "Reverse suggests:" sections.
You can do (with a package randomly picked from CRAN):
tools::package_dependencies(package = "ABHgenotypeR")
$ABHgenotypeR
[1] "ggplot2" "reshape2" "utils"
If you need the reverse dependencies, you can do:
tools::package_dependencies(package = "ABHgenotypeR",
reverse = TRUE)
$ABHgenotypeR
character(0)
From documentation:
For given packages which are not found in the db, NULL entries are
returned, as opposed to character(0) entries which indicate no
dependencies.
And you can do it for multiple packages simultaneously:
packages <- c("ABHgenotypeR", "accelerometry", "ACNE")
tools::package_dependencies(package = packages)
$ABHgenotypeR
[1] "ggplot2" "reshape2" "utils"
$accelerometry
[1] "Rcpp" "dvmisc"
$ACNE
[1] "aroma.affymetrix" "MASS" "R.methodsS3" "R.oo" "R.utils"
[6] "matrixStats" "R.filesets" "aroma.core"
Related
My code:
setwd("C:/A549_ALI/4_tert-Butanol (22)/")
list.celfiles()
my.affy=ReadAffy()
dim(exprs(my.affy))
Output:
Show in New Window
[1] "(46) 22-B1-1_(miRNA-4_0).CEL"
[2] "(47) 22-B1-2_(miRNA-4_0).CEL"
[3] "(48) 22-B1-3_(miRNA-4_0).CEL"
[4] "(49) 22-R1-1_(miRNA-4_0).CEL"
[5] "(50) 22-NEC 1-1_(miRNA-4_0).CEL"
[6] "(51) 22-B2-1_(miRNA-4_0).CEL"
[7] "(52) 22-B2-2_(miRNA-4_0).CEL"
[8] "(53) 22-B2-3_(miRNA-4_0).CEL"
[9] "(54) 22-R2-1_(miRNA-4_0).CEL"
[10] "(55) 22-NEC 2-1_(miRNA-4_0).CEL"
[11] "(56) 22-B3-1_(miRNA-4_0).CEL"
[12] "(57) 22-B3-2_(miRNA-4_0).CEL"
[13] "(58) 22-B3-3_(miRNA-4_0).CEL"
[14] "(59) 22-R3-1_(miRNA-4_0).CEL"
[15] "(60) 22-NEC 3-1_(miRNA-4_0).CEL"
[1] 292681 15
Up to here everything works but than I get this error message:
background correction: mas
PM/MM correction : mas
expression values: mas
background correcting...'getOption("repos")' replaces Bioconductor standard repositories, see '?repositories' for details
replacement repositories:
CRAN: https://cran.rstudio.com/
Error in getCdfInfo(object) :
Could not obtain CDF environment, problems encountered:
Specified environment does not contain miRNA-4_0
Library - package mirna40cdf not installed
Bioconductor - mirna40cdf not available
I have already tried to install this package, but I can't find it on the Bioconductor website.
Now I do not know how to proceed. Is there any other way to use the mas5calls function?
I use R 4.2.2.
Thanks for all answers.
This simply means that the package with the cdf-env built upon the chip description (CDF) - file for this type of MicroArray data is not distributed through Bio-conductor. It seems that Affymetrix is not providing those anymore, but you can find them on GEO. (click on the platform and than under "Supplementary file" . Alternatively ask the person you got the data from if they can provide you with the relevant CDFs. Use cfdName() to check which ones you need.
Once you obtained the CDF you can build the R package ( mirna40cdf in your case) that affy needs using the makecdfenv package you can install from Bioconductor. You could also try to use another package called oligo and see if it supports your data.
I wanted to filter a data set based on some conditions. When I looked at the help for filter function the result was:
filter {stats} R Documentation
Linear Filtering on a Time Series
Description
Applies linear filtering to a univariate time series or to each series separately of a multivariate time series.
After searching on web I found the filter function I needed i.e. from dplyr package. How can R have two functions with same name. What am I missing here?
At the moment the R interpreter would dispatch a call to filter to the dplyr environment, at least if the class of the object were among the avaialble methods:
methods(filter)
[1] filter.data.frame* filter.default* filter.sf* filter.tbl_cube* filter.tbl_df* filter.tbl_lazy*
[7] filter.ts*
As you can see there is a ts method, so if the object were of that class, the interpreter would instead deliver the x values to it. However, it appears that the authors of dplyr have blocked that mechanism and instead put in a warning function. You would need to use:
getFromNamespace('filter', 'stats')
function (x, filter, method = c("convolution", "recursive"),
sides = 2L, circular = FALSE, init = NULL)
{ <omitting rest of function body> }
# same result also obtained with:
stats::filter
R functions are contained in namespaces, so a full designation of a function would be: namespace_name::function_name. There is a hierarchy of namespace containers (actually "environments" in R terminology) arranged along a search path (which will vary depending on the order in which packages and their dependencies have been loaded). The ::-infix-operator can be used to specify a namespace or package name that is further up the search path than might be found in the context of the calling function. The function search can display the names of currently loaded packages and their associated namespaces. See ?search Here's mine at the moment (which is a rather bloated one because I answer a lot of questions and don't usually start with a clean systems:
> search()
[1] ".GlobalEnv" "package:kernlab" "package:mice" "package:plotrix"
[5] "package:survey" "package:Matrix" "package:grid" "package:DHARMa"
[9] "package:eha" "train" "package:SPARQL" "package:RCurl"
[13] "package:XML" "package:rnaturalearthdata" "package:rnaturalearth" "package:sf"
[17] "package:plotly" "package:rms" "package:SparseM" "package:Hmisc"
[21] "package:Formula" "package:survival" "package:lattice" "package:remotes"
[25] "package:forcats" "package:stringr" "package:dplyr" "package:purrr"
[29] "package:readr" "package:tidyr" "package:tibble" "package:ggplot2"
[33] "package:tidyverse" "tools:rstudio" "package:stats" "package:graphics"
[37] "package:grDevices" "package:utils" "package:datasets" "package:methods"
[41] "Autoloads"
At the moment I can find instances of 3 versions of filter using the help system:
?filter
# brings this up in the help panel
Help on topic 'filter' was found in the following packages:
Return rows with matching conditions
(in package dplyr in library /home/david/R/x86_64-pc-linux-gnu-library/3.5.1)
Linear Filtering on a Time Series
(in package stats in library /usr/lib/R/library)
Objects exported from other packages
(in package plotly in library /home/david/R/x86_64-pc-linux-gnu-library/3.5.1)
I am trying to understand some code well enough that I can create the necessary files to make it run it for a client. I thought it was odd that it loads a library within a loop:
for (i in 1:length(ids) ){
library(limma)
# About 80 lines of code
}
Is there any likely reason that someone would want to reload the same library multiple times? I thought that libraries didn't do anything besides provide functions.
I searched the limma user guide for the keywords "library" and "load" and didn't find anything obvious.
I would almost think this were an accident if it weren't the very first line in the loop.
It's a mistake. Change the library call to library(limma, verbose=TRUE) and you'll see that only the first call actually does anything (something is returned invisibly, but they're not using it because it's not being assigned).
For example:
> pkgs <- library(base, verbose=TRUE)
Warning message:
In library(base, verbose = TRUE) :
package ‘base’ already present in search()
> pkgs
# [1] "stats" "graphics" "grDevices" "utils" "datasets"
# [6] "setwidth" "colorout" "methods" "base"
An R namespace acts as the immediate environment for all functions in its associated package. In other words, when function bar() from package foo calls another function, the R evaluator first searches for the other function in <environment: namespace:foo>, then in "imports.foo", <environment: namespace:base>, <environment: R_GlobalEnv>, and so on down the search list returned by typing search().
One nice aspect of namespaces is that they can make packages act like better citizens: unexported functions in <environment: namespace:foo> and functions in imports:foo are available only: (a) to functions in foo; (b) to other packages that import from foo; or (c) via fully qualified function calls like foo:::bar().
Or so I thought until recently...
The behavior
This recent SO question highlighted a case in which a function well-hidden in its package's namespace was nonetheless found by a call to a seemingly unrelated function:
group <- c("C","F","D","B","A","E")
num <- c(12,11,7,7,2,1)
data <- data.frame(group,num)
## Evaluated **before** attaching 'gmodels' package
T1 <- transform(data, group = reorder(group,-num))
## Evaluated **after** attaching 'gmodels
library(gmodels)
T2 <- transform(data, group = reorder(group,-num))
identical(T1, T2)
# [1] FALSE
Its immediate cause
#Andrie answered the original question by pointing out that gmodels imports from the the package gdata, which includes a function reorder.factor that gets dispatched to inside the second call to transform(). T1 differs from T2 because the first is calculated by stats:::reorder.default() and the second by gdata:::reorder.factor().
My question
How is it that in the above call to transform(data, group=reorder(...)), the dispatching mechanism for reorder finds and then dispatches to gdata:::reorder.factor()?
(An answer should include an explanation of the scoping rules that lead from a call involving functions in the stats and base packages to a seemingly well-hidden method in gdata.)
Further possibly helpful details
Neither gdata:::reorder.factor, nor the gdata package as a whole are explicitly imported by gmodels. Here are the import* directives in gmodels' NAMESPACE file:
importFrom(MASS, ginv)
importFrom(gdata, frameApply)
importFrom(gdata, nobs)
There are no methods for reorder() or transform() in <environment: namespace:gmodels>, nor in "imports:gmodels":
ls(getNamespace("gmodels"))
ls(parent.env(getNamespace("gmodels")))
Detaching gmodels does not revert reorder()'s behavior: gdata:::reorder.factor() still gets dispatched:
detach("package:gmodels")
T3 <- transform(data, group=reorder(group,-num))
identical(T3, T2)
# [1] TRUE
reorder.factor() is not stored in the list of S3 methods in the base environment:
grep("reorder", ls(.__S3MethodsTable__.))
# integer(0)
R chat threads from the last couple of days include some additional ideas. Thanks to Andrie, Brian Diggs, and Gavin Simpson who (with others) should feel free to edit or add possibly impt. details to this question.
I'm not sure if I correctly understand your question, but the main point is that group is character vector while data$group is factor.
After attaching gmodels, the call for reorder(factor) calls gdata:::reorder.factor.
so, reorder(factor(group)) calls it.
In transform, the function is evaluated within the environment of the first argument, so in T2 <- transform(data, group = reorder(group,-num)), group is factor.
UPDATED
library attaches the import packages into loaded namespace.
> loadedNamespaces()
[1] "RCurl" "base" "datasets" "devtools" "grDevices" "graphics" "methods"
[8] "stats" "tools" "utils"
> library(gmodels) # here, namespace:gdata is loaded
> loadedNamespaces()
[1] "MASS" "RCurl" "base" "datasets" "devtools" "gdata" "gmodels"
[8] "grDevices" "graphics" "gtools" "methods" "stats" "tools" "utils"
Just in case, the reorder generic exists in namespace:stats:
> r <- ls(.__S3MethodsTable__., envir = asNamespace("stats"))
> r[grep("reorder", r)]
[1] "reorder" "reorder.default" "reorder.dendrogram"
And for more details
The call of reorder will search the S3generics in two envs:
see ?UseMethod
first in the environment in which the generic function is called, and then in the registration data base for the environment in which the generic is defined (typically a namespace).
then, loadNamespace registers the S3 functions to the namespace.
So , in your case, library(gmodels) -> loadNamespace(gdata) -> registerS3Methods(gdata).
After this, you can find it by:
> methods(reorder)
[1] reorder.default* reorder.dendrogram* reorder.factor*
Non-visible functions are asterisked
However, as the reorder.factor is not attached on your search path, you cannot access it directly:
> reorder.factor
Error: object 'reorder.factor' not found
Probably this is whole scenario.
Is there an equivalent of dir function (python) in R?
When I load a library in R like -
library(vrtest)
I want to know all the functions that are in that library.
In Python, dir(vrtest) would be a list of all attributes of vrtest.
I guess in general, I am looking for the best way to get help on R while running it in ESS on linux. I see all these man pages for the packages I have installed, but I am not sure how I can access them.
Thanks
help(package = packagename) will list all non-internal functions in a package.
Yes, use ls().
You can use search() to see what's in the search path:
> search()
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
You can search a particular package with the full name:
> ls("package:graphics")
[1] "abline" "arrows" "assocplot" "axis"
....
I also suggest that you look at this related question on stackoverflow which includes some more creative approaching to browsing the environment. If you're using ESS, then you can use Ess-rdired.
To get the help pages on a particular topic, you can either use help(function.name) or ?function.name. You will also find the help.search() function useful if you don't know the exact function name or package. And lastly, have a look at the sos package.
help(topic) #for documentation on a topic
?topic
summary(mydata) #an overview of data objects try
ls() # lists all objects in the local namespace
str(object) # structure of an object
ls.str() # structure of each object returned by ls()
apropos("mytopic") # string search of the documentation
All from the R reference card