Custom R package loads, shows help but does not provide functions - r

My custom package created with devtools installs, loads, displays help files for the functions, but does not provide the functions. This solution does not seem to be relevant, as my package is installed in .libPaths().
# from the parent directory of the created package
install.packages("mypkg", repos = NULL, type = "source")
# ...
# * DONE (mypkg)
require(mypkg)
# Loading required package: mypkg
?my.fun # displays the function help documentation correctly
my.fun()
Error: could not find function "my.fun"
What causes this behaviour and how to fix it?
sessionInfo()
#R version 3.3.0 (2016-05-03)
#Platform: x86_64-apple-darwin13.4.0 (64-bit)
#Running under: OS X 10.13.1 (unknown)
#locale:
#[1] cs_CZ.UTF-8/cs_CZ.UTF-8/cs_CZ.UTF-8/C/cs_CZ.UTF-8/cs_CZ.UTF-8
#attached base packages:
#[1] stats graphics grDevices utils datasets methods base
#other attached packages:
#[1] mypkg_0.1 devtools_1.13.4
#loaded via a namespace (and not attached):
#[1] tools_3.3.0 withr_2.1.0 memoise_1.0.0 git2r_0.19.0 digest_0.6.9

Did you export the function? To check if it's in the package run:
mypkg:::my.fun()

Related

auto_test gives reporter error in testthat

I'm trying to force myself to do more unit testing on some data analysis projects where I don't really want to develop a package. So, I've been playing with the testthat R package. I have a code folder and inside it is a src and a test folder. In the src folder I have the file add.R that has this function:
add <- function(x,y){
x+y
}
In the test folder I have the file test-add.R that contains this:
library(testthat)
test_that("adding numbers", {
expect_equal(add(2,3), 5)
expect_equal(add(5,5), 10)
})
The following works fine...
> source('code/src/add.R')
> test_file('code/test/test-add.R')
But I'd like to be able to use the auto_test function since as the project grows source/test_file will get tedious. But when I try auto_test I get this...
> auto_test('code/src', 'code/test')
Error in find_reporter(reporter) : attempt to apply non-function
I'm sure I'm missing something simple, but what?
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.1 (El Capitan)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] testthat_0.11.0.9000 knitr_1.12.3
loaded via a namespace (and not attached):
[1] lazyeval_0.1.10 R6_2.1.2 tools_3.2.3
[4] withr_1.0.1 memoise_1.0.0 crayon_1.3.1
[7] digest_0.6.9 devtools_1.10.0.9000
It seems that this is a problem of testthat version 0.11.0.9000. If I install this version from github, I get the same error message as you do:
auto_test('code/src', 'code/test')
## Error in find_reporter(reporter) : attempt to apply non-function
But with version 0.11.0 that can be installed from CRAN, your example runs fine:
auto_test('code/src', 'code/test')
## ..
## DONE

pander $add function not working

I was able to use pander successfully this morning on one computer, but now I am trying to do it on my own computer, and I am having some weird issues.
Here is my very elementary R Pander code:
library("pander")
library("knitr")
test<-Pandoc$new("My Name","Test")
test$add.paragraph('# Welcome to Test')
test$add.paragraph('*****')
#works so far but here it won't work
test$add(3+3)
#here I get error in dev.control(displaylist = "enable"):
# dev.control() called without an open grpahics device
# In addition: Warning message:
#In png("plots/....png", width=480,height=480,res=72, : failed to load cairo DLL
I literally can't use $add to add R code, and I don't understand why. I would greatly appreciate if someone could help.
Regards
edit:
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.10.5 rmarkdown_0.7 rapport_0.51 yaml_2.1.13 plyr_1.8.3 lattice_0.20-33 pander_0.5.2 brew_1.0-6
loaded via a namespace (and not attached):
[1] htmltools_0.2.6 tools_3.2.1 Rcpp_0.12.0 reshape_0.8.5 grid_3.2.1 digest_0.6.8
It appears I did not have XQuartz installed. I installed it and everything works so thank you and köszönöm for the help.

diff() fails on zoo object

While working on a new project in R, I wrote the following code:
sp500 <- get.hist.quote("^GSPC",start=(today <- Sys.Date())-735,quote="Cl")
lsp500 <- log(sp500)
rlsp500 <- diff(lsp500)
The problem is the diff() function, it produces the following error:
Error in MATCH(x, x) : could not find function "MATCH"
All other code executes without problems. I'm using RStudio and R version 2.15.2 (2012-10-26) -- "Trick or Treat" on Mac OSX 10.8.2.
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tseries_0.10-30
loaded via a namespace (and not attached):
[1] grid_2.15.2 lattice_0.20-10 quadprog_1.5-4 tools_2.15.2 zoo_1.7-9
What am I missing?
tseries::get.hist.quote returns a zoo object by default, but the tseries package doesn't attach zoo, so zoo::MATCH isn't found. I assume zoo::MATCH is used in diff.zoo or one of the functions called by it.
Attaching zoo (via library(zoo)) will fix the problem.

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

I am in a process of figuring out how to use my university cluster. It has 2 versions of R installed. System wide R 2.11 (Debian 6.0) and R 2.14.2 in non-standard location.
I am trying to use MPI together with snow. The code I am trying to run is the following
library(snow)
library(Rmpi)
cl <- makeMPIcluster(mpi.universe.size()-1)
stopCluster(cl)
mpi.quit()
It works without the problems on R 2.11. (I launch the script with mpirun -H localhost,n1,n2,n3,n4 -n 1 R --slave -f code.R). Now when I try to do it with R 2.14.2, I get the following message:
Error: This is R 2.11.1, package 'snow' needs >= 2.12.1
In addition: Warning message:
So it seems that R loads the package snow version compiled for R 2.11. I've installed snow under R 2.14 into my home folder and I added the following lines to my code:
.libPaths("/soft/R/lib/R/library")
.libPaths("~/R/x86_64-pc-linux-gnu-library/2.11")
print(.libPaths())
print(sessionInfo())
print(version)
And the output before the error confirms that I am indeed running R 2.14.2 and my R packages folder is first in search path. But I still get the error.
So my question is how do I determine which version of package is loaded in R? I can see with installed.packages all the packages which are installed, so maybe there is some function which lists similar information for loaded packages?
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()
You can use utils::packageVersion to see what version of a package is installed:
> packageVersion("snow")
[1] ‘0.3.9’
Note that
A package will not be ‘found’ unless it has a DESCRIPTION file which contains a valid Version field. Different warnings are given when no package directory is found and when there is a suitable directory but no valid DESCRIPTION file.
Although it sounds like you want to see what version of R you are running, in which case #Justin's sessionInfo suggestion is the way to go.
Technically speaking, all of the answers at this time are wrong. packageVersion does not return the version of the loaded package. It goes to the disk, and fetches the package version from there.
This will not make a difference in most cases, but sometimes it does. As far as I can tell, the only way to get the version of a loaded package is the rather hackish:
asNamespace(pkg)$`.__NAMESPACE__.`$spec[["version"]]
where pkg is the package name.
EDIT: I am not sure when this function was added, but you can also use getNamespaceVersion, this is cleaner:
getNamespaceVersion(pkg)
To check the version of R execute : R --version
Or after you are in the R shell print the contents of version$version.string
EDIT
To check the version of installed packages do the following.
After loading the library, you can execute sessionInfo ()
But to know the list of all installed packages:
packinfo <- installed.packages(fields = c("Package", "Version"))
packinfo[,c("Package", "Version")]
OR to extract a specific library version, once you have extracted the information using the installed.package function as above just use the name of the package in the first dimension of the matrix.
packinfo["RANN",c("Package", "Version")]
packinfo["graphics",c("Package", "Version")]
The above will print the versions of the RANN library and the graphics library.
You can try something like this:
package_version(R.version)
getRversion()
GUI solution:
If you are using RStudio then you can check the package version in the Packages pane.
Use the R method packageDescription to get the installed package description and for version just use $Version as:
packageDescription("AppliedPredictiveModeling")$Version
[1] "1.1-6"
Based on the previous answers, here is a simple alternative way of printing the R-version, followed by the name and version of each package loaded in the namespace. It works in the Jupyter notebook, where I had troubles running sessionInfo() and R --version.
print(paste("R", getRversion()))
print("-------------")
for (package_name in sort(loadedNamespaces())) {
print(paste(package_name, packageVersion(package_name)))
}
Out:
[1] "R 3.2.2"
[1] "-------------"
[1] "AnnotationDbi 1.32.2"
[1] "Biobase 2.30.0"
[1] "BiocGenerics 0.16.1"
[1] "BiocParallel 1.4.3"
[1] "DBI 0.3.1"
[1] "DESeq2 1.10.0"
[1] "Formula 1.2.1"
[1] "GenomeInfoDb 1.6.1"
[1] "GenomicRanges 1.22.3"
[1] "Hmisc 3.17.0"
[1] "IRanges 2.4.6"
[1] "IRdisplay 0.3"
[1] "IRkernel 0.5"
Old question, but not among the answers is my favorite command to get a quick and short overview of all loaded packages:
(.packages())
To see which version is installed of all loaded packages, just use the above command to subset installed.packages().
installed.packages()[(.packages()),3]
By changing the column number (3 for package version) you can get any other information stored in installed.packages() in an easy-to-read matrix. Below is an example for version number and dependency:
installed.packages()[(.packages()),c(3,5)]
To add on #GSee's answer, note that the returned value of utils::packageVersion() is not a character and that you can perfectly use it to write conditions:
packageVersion("dplyr")
#> [1] '1.0.7'
packageVersion("dplyr")>1
#> [1] TRUE
packageVersion("dplyr")>'1.0'
#> [1] TRUE
packageVersion("dplyr")>'1.1'
#> [1] FALSE
Created on 2021-08-23 by the reprex package (v2.0.0)
Use the following code to obtain the version of R packages installed in the system:
installed.packages(fields = c ("Package", "Version"))
Search() can give a more simplified list of the attached packages in a session (i.e., without the detailed info given by sessionInfo())
search {base}- R Documentation
Description: Gives a list of attached packages. Search()
search()
#[1] ".GlobalEnv" "package:Rfacebook" "package:httpuv"
#"package:rjson"
#[5] "package:httr" "package:bindrcpp" "package:forcats" #
#"package:stringr"
#[9] "package:dplyr" "package:purrr" "package:readr"
#"package:tidyr"
#[13] "package:tibble" "package:ggplot2" "package:tidyverse"
#"tools:rstudio"
#[17] "package:stats" "package:graphics" "package:grDevices"
#"package:utils"
#[21] "package:datasets" "package:methods" "Autoloads"
#"package:base"
Simply use help(package="my_package") and look at the version shown.
This assumes there are no other package versions in the same .libPaths.

R Rmetrics packages

I ´ve tried to install some packages from Rmetrics, but it looks I am doing something wrong.
For
https://r-forge.r-project.org/scm/viewvc.php/pkg/fPortfolioBacktest/?logsort=cvs&root=rmetrics&pathrev=4948
I simply used
install.packages("fPortfolioBacktest", repos="http://R-Forge.R-project.org")
but
> install.packages("fPortfolioBacktest", repos="http://R-Forge.R-project.org")
Warning: unable to access index for repository http://R-Forge.R-project.org/bin/windows/contrib/2.12
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘fPortfolioBacktest’ is not available
Exactly the same problem is with
https://r-forge.r-project.org/scm/viewvc.php/pkg/Rsocp/?root=rmetrics&pathrev=3690
> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=Slovak_Slovakia.1250 LC_CTYPE=Slovak_Slovakia.1250 LC_MONETARY=Slovak_Slovakia.1250 LC_NUMERIC=C
[5] LC_TIME=Slovak_Slovakia.1250
attached base packages:
[1] splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] corpcor_1.5.7 fPortfolio_2130.80 fAssets_2100.78 fCopulae_2110.78 sn_0.4-16
[6] mnormt_1.4-3 robustbase_0.7-0 xlsx_0.3.0 xlsxjars_0.3.0 rJava_0.8-8
[11] ttrTests_1.5 PerformanceAnalytics_1.0.3.2 fTrading_2110.77 fBasics_2110.80 timeSeries_2130.90
[16] timeDate_2130.91 dynlm_0.3-0 car_2.0-8 survival_2.36-3 nnet_7.3-1
[21] MASS_7.3-8 lmtest_0.9-27 tseries_0.10-24 quadprog_1.5-3 quantmod_0.3-15
[26] TTR_0.20-2 xts_0.8-0 zoo_1.6-4 Defaults_1.1-1
loaded via a namespace (and not attached):
[1] grid_2.12.0 lattice_0.19-13 Rglpk_0.3-5 slam_0.1-22 strucchange_1.4-2 tools_2.12.0
Is there a fast way how to install them with all the dependencies? I am using Windows XP
Thanks,
Alex
Thanks for adding the error and your sessionInfo.
As I said in my comment, you're running an old version of R (2.12.0). R-forge only builds binaries for the most recent major revision (2.13.X). You need to either upgrade R or build from source. To build from source on Windows, you will need the Windows toolset.

Resources