diff() fails on zoo object - r

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.

Related

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

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()

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

New bug in glmmADMB? VarCorr can't locate rdig

I think this is a newly introduced bug. At the very least, it bugged previously working code.
library(glmmADMB)
epil2$subject <- factor(epil2$subject)
fm <- glmmadmb(y~Base*trt+Age+Visit+(Visit|subject),
data=epil2, family="nbinom")
VarCorr(fm)
Produces the following error:
Error in glmmADMB::VarCorr(fm) :
could not find symbol "rdig" in environment of the generic function
VarCorr from nlme still works fine:
library(nlme)
nlme::VarCorr(fm) ##WORKS
glmmADMB::VarCorr(fm) ##Still doesn't work.
Here is my systemInfo if that helps:
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] nlme_3.1-125 glmmADMB_0.8.1 MASS_7.3-45
loaded via a namespace (and not attached):
[1] Matrix_1.2-3 plyr_1.8.3 magrittr_1.5 tools_3.2.3
[5] coda_0.18-1 Rcpp_0.12.3 stringi_1.0-1 grid_3.2.3
[9] stringr_1.0.0 R2admb_0.7.13 lattice_0.20-33
I found a solution to my problem. I used the installation code provided here:
http://glmmadmb.r-forge.r-project.org/ . I am not sure if it was necessary but before running the below code I navigated to my ~R/R-3.2.3/library folder and deleted the glmmADMB folder.
Then I ran the code below.
install.packages("R2admb")
install.packages("glmmADMB",
repos=c("http://glmmadmb.r-forge.r-project.org/repos",
getOption("repos")),
type="source")
This updated my glmmADMB to version 0.8.3.3 which corrected the error (by turning it into a warning).

RStudio crashes with call to getURL in RCurl

I am using the latest stable release of R (3.2.0) with RStudio (0.98.1103) on Mac OS 10.10.3.
When calling getURL from the RCurl package RStudio terminates with a fatal error.
This does not happen when using R from the command line, only in RStudio.
library("RCurl")
getURL("http://www.omegahat.org/RCurl/")
Checking the output of sessionInfo(), system.file(), and .libPaths() after library("RCurl") does indeed lead me to believe I am using the same version of R and RCurl in both scenarios.
Executing R from the command line:
> library("RCurl")
Loading required package: bitops
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RCurl_1.95-4.6 bitops_1.0-6
> system.file()
[1] "/Library/Frameworks/R.framework/Resources/library/base"
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
and then from inside RStudio:
> library("RCurl")
Loading required package: bitops
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RCurl_1.95-4.6 bitops_1.0-6
loaded via a namespace (and not attached):
[1] tools_3.2.0
> system.file()
[1] "/Library/Frameworks/R.framework/Resources/library/base"
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
Any help greatly appreciated.
This seems to have been fixed with upgrading to Mac OS 10.10.5, RStudio Version 0.99.489 and the latest R 3.2.3

Error: "could not find function.." when using .First()

I have a script in .First(). I saved it to .RData and put it in a folder to which I point in a Windows shortcut of Rterm.exe 64-bit (like described here). When I open the shortcut I get the error "could not find function..", even for base functions..
like so:
.First <- function() {
plot(rnorm(100))
}
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252
[3] LC_MONETARY=German_Austria.1252 LC_NUMERIC=C
[5] LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_2.15.0
Files with .RData extenstions are often assigned as data rather than source files. The problem however is that the .First scripts can only use functions in the base package unlees the other packages are first loaded.
.First <- function() { require(graphics)
plot(rnorm(100))
}
?Startup
Personally, I would name this with a .R extension.

Resources