glmnet error when running a documentation example - r

I am currently running an example in the glmnet documentation (https://cran.r-project.org/web/packages/glmnet/glmnet.pdf page 3):
> x=matrix(rnorm(100*20),100,20)
> y=rnorm(100)
> fit1=glmnet(x,y)
> plot(fit1,xvar="lambda")
However, I get this error message
Error in as.vector((beta %*% ones) > 0) :
no method for coercing this S4 class to a vector
and I need help to understand why the error occurs.

I run exactly the code you put and it works.
Maybe you should remove your workspace and run again the code.

Related

Error in validityMethod(as(object, superClass)) : object 'Matrix_validate' not found

I just try to use :
scRNA <- FindNeighbors(scRNA, dims = pc.num)
and
scRNA.counts <- Read10X(data.dir = "filtered_feature_bc_matrix")
and both of them gives error like :
Error in validityMethod(as(object, superClass)) : object 'Matrix_validate' not found
I guess these code totally run well in other's computer
so I wonder what's wrong with my code and how to fix it ?
Indeed, to solve the problem for you, it should be sufficient to do what #Mikael Jagan says:
update.packages("Matrix")
2nd thought: The above may not solve the problem entirely:
As there are other packages involved, some of these may have to be re-installed (after the updating of Matrix).
Can you post the output (or good summary of that if it's too long) of
traceback()
immediately after producing the error you are seeing?

base::assign(".ptime", proc.time(), pos = "CheckExEnv") ERROR when using devtools::check

I am doing the R CMD check for my package using devtools::check and I encountered the same ERROR(see bellow) discussed here.
I tried to do what was suggested there: I added a tag of #'#export before the #'#example in my prep.R code, and I also added export(prep) in NAMESPACE. However I still get the same error.
Does anyone knows how can I solve this?
Any help will be greatly appreciated
Ayala
* checking R/sysdata.rda ... OK
* checking examples ... ERROR
Running examples in 'prepdat-Ex.R' failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: prep
> ### Title: Aggregate Long Format Data According to Grouping Variables and
> ### Generate a Number of Measures for Each Cell in the Aggregated Data
> ### for Further Analysis
> ### Aliases: prep
>
> ### ** Examples
>
> data(stroopdata)
> x1 <- prep(
+ dataset = stroopdata
+ , file_name = NULL
+ , id = "subject"
I found this question when googling this exact error. I think I understand now why it occurred: Roxygen actually executes the code in the #examples section, and my code included undeclared objects and gave this exact error when running check.
Quick solution: remove the offending lines of code from the #examples section. Or, a more considerate solution is to enclose the example code within \dontrun{...}.
See ?examples for more details on this as well as other options.

KFAS package; error with documentation code

This is an example from the KFAS manual page 13. I just tried a copy/paste and this was the result. Any idea what the problem is?
There were additional errors with the rest of the example.
> require(KFAS)
Loading required package: KFAS
> data(GlobalTemp)
> model<-SSModel(GlobalTemp~SSMtrend(1,Q=NA,type= common ),H=matrix(NA,2,2))
Error in ts(x) : object is not a matrix
Did you copy the example by hand? The line in the manual (at least this one: http://cran.r-project.org/web/packages/KFAS/KFAS.pdf) uses this command:
require(KFAS)
data(GlobalTemp)
model<-SSModel(GlobalTemp~SSMtrend(1,Q=NA,type='common'),H=matrix(NA,2,2))
With the ' around common (and it works fs)
However you should have got the "common not found" error message, not the one you quote (except you already have an object named common in your environment), so I don't really know if this answer will help you.

Error in running the gWidgets2Qt demo

I've got this error while running the demo of the gWidgets2Qt package:
> demo(gWidgets2Qt)
demo(gWidgets2Qt)
---- ~~~~~~~~~~~
Type <Return> to start :
> ## run examples
> require(gWidgets2)
> options(guiToolkit="Qt")
> ## run examples
> source(system.file("examples", "run_examples.R", package="gWidgets2"))
Error in envRefSetField(x, what, refObjectClass(x), selfEnv, value) :
‘.visible’ is not a field in class “GWindow”
please check also this question I've just asked for session info and a similar error I've got with the cranvas package which I think might be related to the above. Thanks a lot.
EDIT:
following the tips from #jverzani I tried a simple code which worked. Then I did some tests:
I get this when detaching the package
detach("package:gWidgets2Qt", unload=TRUE)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In FUN(X[[2L]], ...) :
Created a package name, ‘2013-05-23 07:40:37’, when none found
Tried to re-load and run the demo but still didn't work
I restarted ubuntu and tried again
library(gWidgets2Qt)
demo(gWidgets2Qt)
it run correctly, I only get error with the ex-graphics.R example
which fails to run with this error at the first attempt:
Error in qsceneDevice(width, height, pointsize, family, the_scene) :
unused argument (the_scene)
In addition: Warning message:
In .removePreviousCoerce(class1, class2, where, prevIs) :
methods currently exist for coercing from “AlternativeSingleEnum” to “character”; they will be replaced.
Error in qinvoke(<environment>, "initScene", ...) :
Implementation failed for method 'R::gWidgets2Qt::QtDevice::initScene'
and this one at the next attmpts:
Error in qsceneDevice(width, height, pointsize, family, the_scene) :
unused argument (the_scene)
Error in qinvoke(<environment>, "initScene", ...) :
Implementation failed for method 'R::gWidgets2Qt::QtDevice::initScene'
But all the other examples work. However, as soon as I load cranvas, with
> library(cranvas)
Attaching package: ‘cranvas’
The following object is masked from ‘package:gWidgets2’:
visible, visible<-
demo(gWidgets2Qt) fails again and detaching cranvas
> detach("package:cranvas", unload=TRUE)
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In FUN(X[[2L]], ...) :
Created a package name, ‘2013-05-23 08:37:43’, when none found
demo(gWidgets2Qt) runs again. Has been this incompatibility already seen? Is this depending on invisible being masked from gWidgets2?
I'm not sure why this isn't working. I just installed the whole thing (qtbase, qtutils, gWidgets2, gWidget2Qt) on a linux setup and the demo starts. The ones involving graphs don't really work, but the basic demo does. To see if everything is working, try with something simple:
w <- gwindow("something simple")
b <- gbutton("click me", container=w)
addHandlerChanged(b, handler=function(h,...) {
gmessage("Hello world", parent=w)
})
If that doesn't work then there are installation issues

tryCatch does not catch an error if called though RScript

I'm facing a strange issue in R.
Consider the following code (a really simplified version of the real code but still having the problem) :
library(timeSeries)
tryCatch(
{
specificWeekDay <- 2
currTs <- timeSeries(c(1,2),c('2012-01-01','2012-01-02'),
format='%Y-%m-%d',units='A')
# just 2 dates out of range
start <- time(currTs)[2]+100*24*3600
end <- time(currTs)[2]+110*24*3600
# this line returns an empty timeSeries
currTs <- window(currTs,start=start,end=end)
message("Up to now, everything is OK")
# this is the line with the uncatchable error
currTs[!(as.POSIXlt(time(currTs))$wday %in% specificWeekDay),] <- NA
message("I'm after the bugged line !")
},error=function(e){message(e)})
message("End")
When I run that code in RGui, I correctly get the following output:
Up to now, everything is OK
error in evaluating the argument 'i' in
selecting a method for function '[<-': Error in
as.POSIXlt.numeric(time(currTs)) : 'origin' must be supplied
End
Instead, when I run it through RScript (in windows) using the following line:
RScript.exe --vanilla "myscript.R"
I get this output:
Up to now, everything is OK
Execution interrupted
It seems like RScript crashes...
Any idea about the reason?
Is this a timeSeries package bug, or I'm doing something wrong ?
If the latter, what's the right way to be sure to catch all the errors ?
Thanks in advance.
EDIT :
Here's a smaller example reproducing the issue that doesn't use timeSeries package. To test it, just run it as described above:
library(methods)
# define a generic function
setGeneric("foo",
function(x, ...){standardGeneric("foo")})
# set a method for the generic function
setMethod("foo", signature("character"),
function(x) {x})
tryCatch(
{
foo("abc")
foo(notExisting)
},error=function(e)print(e))
It seems something related to generic method dispatching; when an argument of a method causes an error, the dispatcher cannot find the signature of the method and conseguently raises an exception that tryCatch function seems unable to handle when run through RScript.
Strangely, it doesn't happen for example with print(notExisting); in that case the exception is correctly handled.
Any idea about the reason and how to catch this kind of errors ?
Note:
I'm using R-2.14.2 on Windows 7
The issue is in the way the internal C code implementing S4 method dispatch tries to catch and handle some errors and how the non-interactive case is treated in this approach. A work-around should be in place in R-devel and R-patched soon.
Work-around now committed to R-devel and R-patched.
Information about tryCatch() [that the OP already knew and used but I didn't notice]
I think you are missing that your tryCatch() is not doing anything special with the error, hence you are raising an error in the normal fashion. In interactive use the error is thrown and handled in the usual fashion, but an error inside a script run in a non-interactive session (a la Rscript) will abort the running script.
tryCatch() is a complex function that allows the potential to trap and handle all sorts of events in R, not just errors. However by default it is set up to mimic the standard R error handling procedure; basically allow the error to be thrown and reported by R. If you want R to do anything other than the basic behaviour then you need to add a specific handler for the error:
> e <- simpleError("test error")
> tryCatch(foo, error = function(e) e,
+ finally = writeLines("There was a problem!"))
There was a problem!
<simpleError in doTryCatch(return(expr), name, parentenv, handler): object 'foo'
not found>
I suggest you read ?tryCatch in more detail to understand better what it does.
An alternative is to use try(). To modify your script I would just do:
# this is the line with the uncatchable error
tried <- try(currTs[!(as.POSIXlt(time(currTs))$wday %in% specificWeekDay),] <- NA,
silent = TRUE)
if(inherits(tried, "try-error")) {
writeLines("There was an error!")
} else {
writeLines("Everything worked fine!")
}
The key bit is to save the object returned from try() so you can test the class, and to have try() operate silently. Consider the difference:
> bar <- try(foo)
Error in try(foo) : object 'foo' not found
> bar <- try(foo, silent = TRUE)
> class(bar)
[1] "try-error"
Note that in the first call above, the error is caught and reported as a message. In the second, it is not reported. In both cases an object of class "try-error" is returned.
Internally, try() is written as a single call to tryCatch() which sets up a custom function for the error handler which reports the error as a message and sets up the returned object. You might wish to study the R code for try() as another example of using tryCatch().

Resources