Issue with TSO function in tsoutliers - r
I am trying to get the tso() function of the tsoutliers package to work but I keep running into the same maddening error when the dplyr package is loaded that I can't seem to figure out. Any ideas?
Here is some code from a previous answer that seemed to work only when dplyr is not loaded in the namespace.
dat.change <- c(12.013995263488, 11.8460207231808, 11.2845153487846, 11.7884417180764,
11.6865425802022, 11.4703118125303, 11.4677576899063, 11.0227199625084,
11.274775836817, 11.03073498338, 10.7771805591742, 10.7383206158923,
10.5847230134625, 10.2479315651441, 10.4196381241735, 10.467607842288,
10.3682422713283, 9.7834431752935, 9.76649842404295, 9.78257968297228,
9.87817694914062, 9.3449034905713, 9.56400153361727, 9.78120084558148,
9.3445162813738, 9.36767436354887, 9.12070987223648, 9.21909859069157,
8.85136359917466, 8.8814423003979, 8.61830163359642, 8.44796977628488,
8.06957847272046, 8.37999165387824, 7.98213210294954, 8.21977468333673,
7.683960439316, 7.73213584532496, 7.98956476021092, 7.83036046746187,
7.64496198988985, 4.49693528397253, 6.3459274845112, 5.86993447552116,
4.58301192892403, 5.63419551523625, 6.67847511602895, 7.2005344054883,
5.54970477623895, 6.00011922569104, 6.882667104467, 4.74057284230894,
6.2140437333397, 6.18511450451019, 5.83973575417525, 6.57271194428385,
5.36261938326723, 5.48948831338016, 4.93968645996861, 4.52598133247377,
4.56372558828803, 5.74515428123725, 5.45931581984165, 5.58701112949141,
6.00585679276365, 5.41639695946931, 4.55361875158434, 6.23720558202826,
6.19433060301002, 5.82989415940829, 5.69321394985076, 5.53585871082265,
5.42684812413063, 5.80887522466946, 5.56660158483312, 5.7284521523444,
5.25425775891636, 5.4227645808924, 5.34778016248718, 5.07084809927736,
5.324066161355, 5.03526881241705, 5.17387528516352, 5.29864121433813,
5.36894461582415, 5.07436929444317, 4.80619983525015, 4.42858947882894,
4.33623051506001, 4.33481791951228, 4.38041031792294, 3.90012900415342,
4.04262777674943, 4.34383842876647, 4.36984816425014, 4.11641092254315,
3.83985887104645, 3.81813419810962, 3.85174630901311, 3.66434598962311,
3.4281724860426, 2.99726515704766, 2.96694634792395, 2.94003031547181,
3.20892607367132, 3.03980832743458, 2.85952185077593, 2.70595278908964,
2.50931109659839, 2.1912274016859)
library("tsoutliers")
dat.ts<- ts(dat.change, frequency=1)
data.ts.outliers <- tso(dat.ts)
ERROR I obtain is:
> data.ts.outliers <- tso(dat.ts)
Error in filter_(.data, .dots = lazyeval::lazy_dots(...)):
argument ".data" is missing, with no default
I am fairly certain that the issue is a conflict with dplyr but I am not sure how to get around it easily. If I do not install dplyr everything is peachy and runs.
Any Ideas?
As you say, there is a conflict between stats::filter and dplyr::filter. I fixed it by adding stats::filter as an import in the namespace of package tsoutliers.
The new version of the package is not yet on CRAN, in the meantime you can get the sources from this temporary link and windows binaries from this other link.
I wrote following function that unloads dplyr when calling tso():
replaceOutliers <- function(series) {
require(tsoutliers)
detach("package:dplyr", unload=TRUE)
o <- tso(ts(series))
print(paste("Replacing", length(o$outliers$ind), "outliers for series"))
series <- o$yadj
library(dplyr)
return(series)
}
Absolutely not an elegant solution but I didn't want to go messing in the R code of tsoutliers.
Related
Why does lintr say "warning: no visible binding for global variable ‘Cloaked’"?
I'm sure that this is a misunderstanding of mine, since I'm not really an R programmer... I have my code here: https://gist.github.com/bnsh/3839c4eb2c6b31e32c39ec312014b2b8 #! /usr/bin/env Rscript library(R6) Cloaked <- R6::R6Class("Cloaked", public = list( msg = function() { return(paste( "this code _works_, but lintr (https://github.com/jimhester/lintr)", "complains that cloak_class.R:19:8: warning: no visible binding for", "global variable ‘Cloaked’ when I try to use Cloaked within a", "function. It's fine tho, if I use it _outside_ a function." )) } ) ) main <- function() { c <- Cloaked$new() c$msg() } main() It works... But, lintr complains: "cloak_class.R:19:8: warning: no visible binding for global variable ‘Cloaked’" Actually, it's not about a class, really, because this also complains: #! /usr/bin/env Rscript cloaked <- function() { return(paste( "this code _works_, but lintr (https://github.com/jimhester/lintr)", "complains that cloak_function.R:13:3: warning: no visible global", "function definition for ‘cloaked’ when I try to use cloaked within", "a function. It's fine tho, if I use it _outside_ a function." )) } main <- function() { cloaked() } main() This code also runs, but lintr says: cloak_function.R:13:3: warning: no visible global function definition for ‘cloaked’ Why? Short of doing something blunt instrument like # nolint start/# nolint end, what can I do to satisfy lintr? Thanks!
I've just started with lintr and had the same problem. It appears to be a bug. https://github.com/REditorSupport/atom-ide-r/issues/7 and the actual issue https://github.com/jimhester/lintr/issues/27 The only workaround for now (short of fixing the bug within lintr) is to disable the object linter (which is not all that ideal as it will not catch genuine errors of this form). e.g. with_defaults(object_usage_linter=NULL) (I don't think the object usage linter is intended for scripts but rather packages - as far as I can tell, to work it will eval the entire script (!) to see what globals are defined. For a package where the R files are all function definitions that's fine, but for a script you don't really want to run the whole script every time you lint the file)
Using the linters argument in the lintr functions: lint("myfile.R", linters = linters_with_defaults( object_length_linter = NULL, object_name_linter = NULL, object_usage_linter = NULL))
NoSuchMethodException when calling sendKeys on object of class org.openqa.selenium.remote.RemoteWebElement via R package rJava
I am trying to use the selenium webdriver API directly from R using rJava. I am subject to a fairly restrictive IT environment, so I can't access a remote driver currently (hence why I'm not currently using the Rselenium package), and I don't have either Chrome or Firefox availaible--just phantomjs. I am able to get this working okay from the Scala REPL. I used sbt to get all the dependenices--build.sbt contains, for example: retrieveManaged := true libraryDependencies ++= Seq ( "org.seleniumhq.selenium" % "selenium-java" % "3.9.1", "com.codeborne" % "phantomjsdriver" % "1.4.4" ) (Note that I have phantomjs installed as /usr/local/bin/phantomjs, and it is version 2.1.1). I then copied all the jar files to a single-level folder via cp jars/*/*/*.jar alljars/ containing the following: animal-sniffer-annotations-1.14.jar httpcore-4.4.6.jar selenium-api-3.9.1.jar byte-buddy-1.7.9.jar j2objc-annotations-1.1.jar selenium-chrome-driver-3.9.1.jar checker-compat-qual-2.0.0.jar jline-2.14.5.jar selenium-edge-driver-3.9.1.jar commons-codec-1.10.jar jsr305-1.3.9.jar selenium-firefox-driver-3.9.1.jar commons-exec-1.3.jar okhttp-3.9.1.jar selenium-ie-driver-3.9.1.jar commons-logging-1.2.jar okio-1.13.0.jar selenium-java-3.9.1.jar error_prone_annotations-2.1.3.jar phantomjsdriver-1.4.4.jar selenium-opera-driver-3.9.1.jar gson-2.8.2.jar scala-compiler-2.12.4.jar selenium-remote-driver-3.9.1.jar guava-23.6-jre.jar scala-library-2.12.4.jar selenium-safari-driver-3.9.1.jar httpclient-4.5.3.jar scala-reflect-2.12.4.jar selenium-support-3.9.1.jar I start Scala via scala -cp "alljars/*" and can the do following: val drv = new org.openqa.selenium.phantomjs.PhantomJSDriver drv.get("https://www.google.com") val q = drv.findElementByName("q") q.sendKeys("rJava selenium") q.submit drv.getTitle I think the following is roughly the same thing in R using rJava: library(rJava) .jinit() jars <- dir("alljars", pattern = "*.jar", full.names = TRUE) .jaddClassPath(jars) drv <- .jnew('org/openqa/selenium/phantomjs/PhantomJSDriver') drv$get("https://www.google.com") q <- drv$findElementByName("q") q$sendKeys("rJava selenium") q$submit() drv$getTitle() This fails at the point q$sendKeys("rJava selenium") with the following error: Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.lang.NoSuchMethodException: No suitable method for the given parameters In RStudio, if I type q$ and press TAB, sendKeys is definitely in the list of available methods. I tried to be explicit about this, and tried: keys <- .jnew("java/lang/String", "rJava selenium") keys <- .jcast(keys, "java/lang/CharSequence", check = TRUE) q <- .jcast(q, "org/openqa/selenium/WebElement", check = TRUE) .jcall(q, "V", "sendKeys", keys) which resulted in the following error: Error in .jcall(q, "V", "sendKeys", keys) : method sendKeys with signature (Ljava/lang/CharSequence;)V not found q has class org/openqa/selenium/remote/RemoteWebElement in R, and org/openqa/selenium/WebElement in Scala; but in both cases the return is void and the required argument is CharSequence according to the javadocs. I tried a few variations of this--java.lang.String instead of CharSequence, RemoteWebElement instead of WebElement, etc., but no joy. I doubt this is a problem with rJava, but I'm stumped nonetheless and need help!
Oh good grief. I didn't know about .jmethods. Running this: > .jmethods(q, "sendKeys") [1] "public void org.openqa.selenium.remote.RemoteWebElement.sendKeys(java.lang.CharSequence[])" So, basically, my problem was that I was passing String instead of String[]. That is, instead of: q$sendKeys("rJava selenium") I can use: q$sendKeys(.jarray("rJava selenium")) The more you know...
tryCatch() Vs if else
I have recently started exploring R programming and have a very trivial doubt. I want to write a function which would try to load the required packages and install the same in case its not installed already. I have written it using if else logic. My question is will it be more efficient if I use tryCatch()? Is there any other advantage than the ability to handle errors as it is not important to handle errors in this context. Below is the code I am currently using: Inst.Pkgs=function(){ if (!require(tm)) {install.packages("tm") require(tm)} if (!require(SnowballC)) {install.packages("SnowballC") require(SnowballC)} if (!require(wordcloud)) {install.packages("wordcloud") require(wordcloud)} if (!require(RWeka)) {install.packages("RWeka") require(RWeka)} if (!require(qdap)) {install.packages("qdap") require(qdap)} if (!require(timeDate)) {install.packages("timeDate") require(timeDate)} if (!require(googleVis)) {install.packages("googleVis") require(googleVis)} if (!require(rCharts)) { if (!require(downloader)) {install.packages("downloader") require(downloader)} download("https://github.com/ramnathv/rCharts/archive/master.tar.gz", "rCharts.tar.gz") install.packages("rCharts.tar.gz", repos = NULL, type = "source") require(rCharts) } }
You can check at once and install missing packages. # Definition of function out, the opposite of in "%out%" <- function(x, table) match(x, table, nomatch = 0) == 0 # Storing target packages pkgs <- c("tm", "SnowballC", "wordcloud", "RWeka", "qdap", "timeDate", "googleVis") # Finding which target packages are already installed using # function installed.packages(). Names of packages are stored # in the first column idx <- pkgs %out% as.vector(installed.packages()[,1]) # Installing missing target packages install.packages(pkgs[idx])
We can in fact use tryCatch for this. If the program tries to load a library that is not installed, it will throw an error - an error that can be caught and resolved with a function called by tryCatch(). Here's how I would do it: needed_libs <- c("tm", "SnowballC", "wordcloud", "RWeka", "qdap", "timeDate", "googleVis") install_missing <- function(lib){install.packages(lib,repos="https://cran.r-project.org/", dependencies = TRUE); library(lib, character.only = TRUE)} for (lib in needed_libs) tryCatch(library(lib, character.only=TRUE), error = function(e) install_missing(lib)) This will install the missing packages and load the required libraries, as requested in the OP.
How to check platform in .onLoad in the R package
I am trying to check if the package is run on Windows during the loading of the package and load some additional files. For some reason this doesn't work (added to my zzz.R): .onLoad <- function(libname, pkgname){ if(.Platform$OS.type == "windows") { # do specific task here } } How to make it work, is there a better way to do it?
EDIT to correct a wrong previous answer After loading, loadNamespace looks for a hook function named .onLoad and calls it. The functions loadNamespace is usually called implicitly when library is used to load a package. However you can be useful at times to call these functions directly. So the following(essentially your code) works for me under windows platform. .onLoad <- function(libname, pkgname ) { if(.Platform$OS.type == "windows") print("windows") else print("others") } Then you test it using: loadNamespace("loadpkg") [1] "windows" <environment: namespace:loadpkg> Note also you can have the same thing using library, but you should unload the namespace before (I guess it checks if the package is already loaded and don't call all hooks): unloadNamespace("loadpkg") library("loadpkg") [1] "windows" don't use this , it is a wrong but accepted solution You should initialize function parameters: .onLoad <- function(libname = find.package(pkg_name), pkgname = pkg_name){ if(.Platform$OS.type == "windows") { # do specific task here } }
Building R package with Reference objects fails
I recently rewrote a package to use the new(er) R reference class objects. I've exported the three classes using export() in the NAMESPACE file so as far as I'm aware that should work. However when I test build the package I get an error at lazy loading stage: ** preparing package for lazy loading Error in file(con, "rb") : invalid 'description' argument ERROR: lazy loading failed for package ‘PACKAGE_NAME_HERE’ * removing ‘/Library/Frameworks/R.framework/Versions/3.0/Resources/library/PACKAGE_NAME_HERE’ I'm not sure what the problem is here. I don't know if it's relevant but the reference classes do store data on files in the tmp directory by having some fields set as accessor functions - I don't know if that's what s being complained about here when it says (con, "rb") which I guess is some connection thing. Does anybody have any ideas or advice for making sure reference classes get exported properly? My namespace is currently simple - export(Main) export(Mainseq) export(Maintriplet) Which are the three reference classes I exported by using #export tags in roxygen2. What is it I'm doing (or not doing) that is throwing the lazy load error? (ASIDE - I have no compiled code - all R, although the reference class methods do call some internal functions that are not exported, but these are supposed to be internal so I don't think I need to export them. Thanks, Ben. EDIT: My description file is as follows: Package: HybRIDS Type: Package Title: Quick detection and dating of Recombinant Regions in DNA sequence data. Version: 1.0 Date: 2013-03-13 Author: Ben J. Ward Maintainer: Ben J. Ward <b.ward#uea.ac.uk> Description: A simple R package for the quick detection and dating of Recombinant Regions in DNA sequence data. License: GPL-2 Depends: ggplot2,grid,gridExtra,png,ape I can't see what is wrong with this - the Depends are correct. EDIT: I've eliminated the first error with the description but I'm still getting the con error. I think it's because the Mainseq class (which is nested in class Main) has some fields: FullSequenceFile = "character", FullSequence = function( value ) { if( missing( value ) ){ as.character( read.dna( file = FullSequenceFile, format = "fasta", as.matrix = TRUE ) ) } else { write.dna( value, file = FullSequenceFile, format = "fasta" ) } }, InformativeSequenceFile = "character", InformativeSequence = function( value ) { if( missing( value ) ){ as.character( read.dna( file = InformativeSequenceFile, format = "fasta", as.matrix = TRUE ) ) } else { write.dna( value, file = InformativeSequenceFile, format = "fasta" ) } } The idea being upon initialisation, the two character fields are filled with a path to a temp file in tmpdir, and when the variables are called or edited the files containing the variable data are read or written to. However it seems the variables are being accessed before this path is available because up package build the following happens: ** preparing package for lazy loading Warning in file(con, "rb") : cannot open file '/var/folders/kp/clkqvqn9739ffw2755zjwy74_skf_z/T//RtmpLB8ESC/FullSequenceaba52ac591f3': No such file or directory Error in file(con, "rb") : cannot open the connection