how to add new R packages in azure machine learning for time series anomaly detection - r

I am trying to find out time series anomaly detection in which i need to install new R packages. In this i m following https://github.com/business-science/anomalize site. In this i needed to install 2 packages: tidyverse and anomalize.
can anyone help me on installing package mentioned above as I am getting
error "package or namespace load failed for tidyverse"
Also while adding zip of tidyverse and anomalize do I need to add any other packages and dependencies in that as I am adding only those 2 packages thinking there r no other dependencies I needed for those 2?
you can see in code that I created R_Package.zip and put tidyverse.zip and anomalize.zip in that that
dataset1 <- maml.mapInputPort(1)
data.set <- data.frame(installed.packages())
#install.packages(“src/R_Package/tidyverse_1.2.1.zip”, lib = “.”,
repos = NULL, verbose = TRUE);
#library(tidyverse, lib.loc=”.”, verbose=TRUE);
install.packages("src/tidyverse.zip",lib=".",repos=NULL,verbose=TRUE)
library(R_package, lib.loc = ".", verbose=TRUE);
install.packages("src/anomalize.zip",lib=".",repos=NULL,verbose=TRUE)
library(R_package, lib.loc = ".", verbose=TRUE);
#success <- library("tidyverse", lib.loc = ".",
logical.return = TRUE, verbose = TRUE)
#library(tidyverse)
maml.mapOutputPort("dataset1");

Regarding the error message, notice that it may take some time for the installed packages to become actually available; quoting from Adding R Packages In Azure ML blog post:
Note: In one instance, we ran into an issue where the package was not
loaded into the workspace immediately. We had to wait about half an
hour before we could use the package. You may be running into this
issue as well if you see a message that looks something like this
and you’ve used the above method to check which packages are in your
workspace and the package in question appears on that list. If this is
the case, we suggest waiting a bit before running your experiment
again.
AFAIK yes, you also need to add the package dependencies; the SO thread Install R Packages in Azure ML contains some useful hints.

Related

How to import sf to package to run a function that depends on lwgeom?

I'm building a package that imports {sf}, and more specifically I use st_length() in one of my functions.
I initially added only {sf} to my package "Imports", but when I checked it I got a few {lwgeom} related errors:
Running examples in 'gtfstools-Ex.R' failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: get_trip_speed
> ### Title: Get trip speed
> ### Aliases: get_trip_speed
>
> ### ** Examples
>
> data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
>
> gtfs <- read_gtfs(data_path)
>
> trip_speed <- get_trip_speed(gtfs)
Error in sf::st_length(trips_geometries) :
package lwgeom required, please install it first
This error happens when the examples are running, but some similar errors happen with the tests.
Then I added {lwgeom} to Imports. The check runs fine, but in the end I get a note: NOTE: Namespaces in Imports field not imported from: 'lwgeom'
What's the best practice when dealing with cases like this? Should I just keep track of this note and send it as a comment to CRAN during the package submission process?
You can consider adding the {lwgeom} package in Suggests field of your package DESCRIPTION file. It should do the trick.
The Suggests != Depends article by Dirk Eddelbuettel refers to a relevant bit of Writing R Extensions (WRE) that might be useful to this case.
Section 1.1.3.1 (suggested packages) reads (as of 2021-03-12):
Note that someone wanting to run the examples/tests/vignettes may not have a suggested package available (and it may not even be possible to install it for that platform). The recommendation used to be to make their use conditional via if(require("pkgname")): this is OK if that conditioning is done in examples/tests/vignettes, although using if(requireNamespace("pkgname")) is preferred, if possible.
However, using require for conditioning in package code is not good practice as it alters the search path for the rest of the session and relies on functions in that package not being masked by other require or library calls. It is better practice to use code like
if (requireNamespace("rgl", quietly = TRUE)) {
rgl::plot3d(...)
} else {
## do something else not involving rgl.
}
So while just adding {lwgeom} to Suggests works, we may stumble upon the issue where someone that runs a "lean installation" (i.e. without suggested packages) of my package won't be able to use the functions that rely on {lwgeom}.
More importantly, if an author of a package that I am importing decides to run a reverse dependency check on my package while not installing suggested packages, the check would fail because I'd have a few examples, tests and vignettes bits failing due to not having {lwgeom} available.
Thus, in addition to listing it in Suggests, I added some checks on examples and vignettes like suggested by WRE:
*examples/vignette context*
# the examples below require the 'lwgeom' package to be installed
if (requireNamespace("lwgeom", quietly = TRUE)) {
... do something ...
}
In the functions that require {lwgeom} I added:
if (!requireNamespace("lwgeom", quietly = TRUE))
stop(
"The 'lwgeom' package is required to run this function. ",
"Please install it first."
)
And added this bit to the tests of such functions (using {testthat}):
if (!requireNamespace("lwgeom", quietly = TRUE)) {
expect_error(
set_trip_speed(gtfs, "CPTM L07-0", 50),
regexp = paste0(
"The \\'lwgeom\\' package is required to run this function\\. ",
"Please install it first\\."
)
)
skip("'lwgeom' package required to run set_trip_speed() tests.")
}

How do I deal with an error message while installing a package?

I am brand new to this so please forgive my inexperience...I'm trying to learn.
I'm attempting to install an R package called "Doublet Finder" using the specified code given on the Github site.
When I do this, I get this error immediately:
Error in rbind(info, getNamespaceInfo(env, "S3methods")) :
number of columns of matrices must match (see arg 2)
Being new to R, I'm not sure what this error means and when I google this something similar comes up and the individual removed and re-installed ALL of their libraries...that seems crazy. Does anyone have advice on what this could be, how to fix it, or why the package won't install?
Your problem seems to be fairly similar to this one. It might be the case that the dependencies (packages that Doublet Finder relies on) are outdated. What you can try is to follow these steps to uninstall and reinstall all packages with the hope that by updating packages there isn't a version mismatch.
This code is copied from the website above:
ip <- as.data.frame(installed.packages(lib.loc = .libPaths()[1]),
stringsAsFactors = FALSE)
head(ip)
str(ip)
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
str(pkgs.to.remove)
sapply(pkgs.to.remove, remove.packages, lib = path.lib)
sapply(pkgs.to.remove, install.packages, lib = path.lib)

Error when trying to deploy to shinyapps.io: Application depends on package "package" but it is not

My server.R contains the following code for dynamically installing packages when needed:
package <- input$chip
if (!require(package, character.only=T, quietly=T)) {
source("https://bioconductor.org/biocLite.R")
biocLite(package, ask = F, suppressUpdates = T, suppressAutoUpdate = T)
library(package, character.only=T)
}
ui.R has a select input element where the user can select one of the following bioconductor packages:
selectInput(inputId = 'chip', label='Chip', choices=c('Mouse Gene 1.0'='mogene10sttranscriptcluster.db',
'Mouse Gene 2.0'='mogene20sttranscriptcluster.db',
'Human Gene 1.0'='hugene10sttranscriptcluster.db',
'Human Genome U133A 2.0'='hgu133a2.db'))
So, based on what chip the user selects, the corresponding annotation package should get loaded, and if it is not already installed, it should install it.
This works on my local machine. But when I try to deploy my app on shinyapps.io. I get the following error:
Error:
* Application depends on package "package" but it is not installed. Please resolve before continuing.
I know that it is unable to recognize the package in biocLite(package, ask = F, suppressUpdates = T, suppressAutoUpdate = T). The deployment process thinks that package is a library name and not a variable and is unable to evaluate its value.
Is there any way to resolve this? Or do I have to explicitly load all required packages? The problem with explicitly loading the annotation packages is that these packages are so big they take up a lot of memory, which is why I wanted to load these packages only when required.
An alternative is to make an if-else loop or switch statement to install packages based on the condition:
package <- function(input$chip) {
switch(input$chip,
'mogene10sttranscriptcluster.db' = 'mogene10sttranscriptcluster.db',
'mogene20sttranscriptcluster.db' = 'mogene20sttranscriptcluster.db',
'hugene10sttranscriptcluster.db' = 'hugene10sttranscriptcluster.db',
'hgu133a2.db' = 'hgu133a2.db')
}
library(package)
But even in this case, the deployment process won't be able to evaluate the package value.
Thanks!
UPDATE:
Taking Yihui's suggestion, I modified my code to:
package <- input$genome
if(!do.call(require, list(package = package, character.only = T, quietly = T))){
do.call(biocLite, list(pkgs = package, ask = F, suppressUpdates = T, suppressAutoUpdate = T))
do.call(library, list(package = package, character.only = TRUE))
}
The application is able to deploy now, but it throws me this error:
Error: unable to install packages
Unfortunately, you have to fool the shinyapps (or rsconnect) package a bit so that it does not detect package as a literal package name. For example, you may use do.call():
do.call(library, list(package = package, character.only = TRUE))
The ShinyApps.io server does not allow you to install packages on the fly (strictly speaking, this is not true, but I don't want to show you how). You have to declare all packages you need in the app as dependencies beforehand. Again, it is a hack:
if (FALSE) {
library(mogene10sttranscriptcluster.db)
library(mogene20sttranscriptcluster.db)
library(hugene10sttranscriptcluster.db)
library(hgu133a2.db)
}
Then ShinyApps.io will detect these packages as dependencies and pre-install them for you. What you need to do in your app is simply load them, and you don't need to install them by yourself.

Sentiment analysis in R (not using tm.plugin.tags)

I'm using R version 3.0.2 and have installed the package tm. Previously, I also loaded a package called tm.plugin.tags. To get a measure of whether a text corpus was positive or negative I used the following approach:
library('tm')
library('tm.plugin.tags')
pos <- tm_tag_score(TermDocumentMatrix(corpus, control = list(removePunctuation = TRUE)), tm_get_tags("Positiv"))
tm.plugin.tags seems to be no longer available for R. This was based on the following classification system http://www.wjh.harvard.edu/~inquirer/homecat.htm and I'm wondering if there is any other package or approach that I can use to achieve a similar result.
I have emailed the package maintainer of tm so I will post an update here once/if I receive a response.
You can install tm.plugin.tags using the following command
install.packages("tm.plugin.tags", repos = "http://datacube.wu.ac.at", type = "source")
This installs without any problem
Thanks
Cheers

dependent packages not loading in R on Debian

I'm having trouble loading the sde package on a clean Debian install running R 2.11.1. I've seen this behavior with some other packages, however, so I don't think it's specific to only this one package. Here's an example of the conundrum:
>install.packages("sde", lib.loc=libPath)
... installs sde, and the packages it's dependent on: zoo, fda
> library(sde, lib=libPath)
Loading required package: fda
Error: package 'fda' could not be loaded
In addition: Warning message:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc) :
there is no package called 'fda'
ok, that's odd. I saw fda being installed. So I manually load the dependencies:
> library(zoo, lib=libPath)
> library(fda, lib=libPath)
Loading required package: splines
ok, that worked. Now let's try sde:
> library(sde, lib=libPath)
To check the errata corrige of the book, type vignette("sde.errata")
WTF? it loaded fine?!?
So why can I manually load the packages but R is not picking them up automagically?
Adding to my confusion, I discovered during debugging that if I don't use the lib=libPath then everything works just fine. So it looks like the use of a custom path for packages is screwing this all up... but why?
You confirmed my suspicions in the comments. You need to do one of two things:
.libPaths(libPath)
or
library(sde, lib=c(libPath,.libPaths()))
I prefer the first method because the second requires you do that for all calls to library.

Resources