Figure Out What Version of R a Function Was Introduced In

Figure out what version of R a function was introduced in

Even easier than Dirk's solution is to use R's news function:

> newsDB <- news()
> news(grepl("regmatches",Text), db=newsDB)
Changes in version 2.14.0:

NEW FEATURES

o New function regmatches() for extracting or replacing matched or
non-matched substrings from match data obtained by regexpr(),
gregexpr() and regexec().

As of R-3.3.0, news will launch via the HTML help system if it is available. You can suppress it via the print.news_db method:

> print(news(grepl("news",Text), db=newsDB), doBrowse=FALSE)
Changes in version 3.3.0:

NEW FEATURES

o news() now displays R and package news files within the HTML help
system if it is available. If no news file is found, a visible
NULL is returned to the console.

How to find out which package version is loaded in R?

You can use sessionInfo() to accomplish that.

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] graphics grDevices utils datasets stats grid methods base

other attached packages:
[1] ggplot2_0.9.0 reshape2_1.2.1 plyr_1.7.1

loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 MASS_7.3-18 memoise_0.1 munsell_0.3
[7] proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.0 stringr_0.6
>

However, as per comments and the answer below, there are better options

> packageVersion("snow")

[1] ‘0.3.9’

Or:

"Rmpi" %in% loadedNamespaces()

How to find the R version under which an R package was built?

The information you want is in the last column ("Built") of the output of installed.packages(), per https://stat.ethz.ch/R-manual/R-devel/library/utils/html/installed.packages.html.

.libPaths() # get the library location
installed.packages(lib.loc = "C://Revolution//R-Enterprise-7.3//R-3.1.1//library")

How to get the package name of a function in R?

Perhaps even most convenient, if you are just after the package name:

environmentName(environment(select))

The advantage is that this produces a string rather than an environment object.

Check for installed packages before running install.packages()

try: require("xtable") or "xtable" %in% rownames(installed.packages())

How to find out if (the source code of) a function contains a call to a method from a specific module?

You can replace the random module with a mock object, providing custom attribute access and hence intercepting function calls. Whenever one of the functions tries to import (from) random it will actually access the mock object. The mock object can also be designed as a context manager, handing back the original random module after the test.

import sys

class Mock:
import random
random = random

def __enter__(self):
sys.modules['random'] = self
self.method_called = False
return self

def __exit__(self, *args):
sys.modules['random'] = self.random

def __getattr__(self, name):
def mock(*args, **kwargs):
self.method_called = True
return getattr(self.random, name)
return mock

def uses_module(func):
with Mock() as m:
func()
return m.method_called

Variable module name

A more flexible way, specifying the module's name, is achieved by:

import importlib
import sys

class Mock:
def __init__(self, name):
self.name = name
self.module = importlib.import_module(name)

def __enter__(self):
sys.modules[self.name] = self
self.method_called = False
return self

def __exit__(self, *args):
sys.modules[self.name] = self.module

def __getattr__(self, name):
def mock(*args, **kwargs):
self.method_called = True
return getattr(self.module, name)
return mock

def uses_module(func):
with Mock('random') as m:
func()
return m.method_called

What to do when a package is not available for our R version?

Try something like:

packageurl <- "http://cran.r-project.org/src/contrib/Archive/XXXX/XXXX_A.B.C.tar.gz"
install.packages(packageurl, contriburl=NULL, type="source")

where XXXX is your package name and A.B.C is the version of the package (not R).



Related Topics



Leave a reply



Submit