RMySQL fails running multiple queries - r

The environment:
Platform: x86_64-redhat-linux-gnu (64-bit)
R version 3.3.0 (2016-05-03)
[1] RMySQL_0.10.9 DBI_0.4-1
Basically what I'm doing is getting whit a query a data.frame and then adding a column with sapply() running a function, but most of the time(some times work) I get this error:
Error in .local(dbObj, ...) : INTEGER() can only be applied to a 'integer', not a 'raw' Calls: getData ... dbClearResult -> dbIsValid -> dbIsValid -> .local -> .Call In addition: There were 50 or more warnings (use warnings() to see the first 50) Execution halted
This is my code:
myFunc <- function(row){
res <- dbGetQuery(conn,my_sub_query)
return(res[1,1])
}
df <- dbGetQuery(conn,my_query)
df$newCol <- sapply( df$col1, myFunc)
Fails most of the time, please help and thanks in advance

Related

Cannot use S4 and box in executable R script: name '.cacheOnAssign' not found in 'env'

My set up and minimal example
I have this set up:
SomeProject/
bin/
someMinimalScript.R
utils/
R/
someMinimalModule.R
Contents of someMinimalScript.R:
#!/usr/bin/env Rscript
box::use(./utils/R/someMinimalModule[ someClass ])
This is importing a module who's contents are someMinimalModule.R:
#' #export
someClass <- setClass(
"someClass",
slots = list(
example_slot = "character"
),
prototype = list(
example_slot = character()
)
)
#' #export
setMethod("show", "someClass", \(object) {
cat("\n")
cat("Example slot:", object#example_slot, "\n")
cat("\n")
})
This is a set of tools which is designed to be used from the system command line (CLI tools). This is achieved by exporting the path to the tools in a .bashrc:
export PATH="/Users/someUser/someProject/bin:$PATH"
We need to also do this in the project: chmod 755 ./bin/*.
My errors
Once all of this is set up, I am getting this error when calling this tool from the command line:
someMinimalScript.R
Error in box::use(./utils/R/someMinimalModule[someClass]) :
could not find function "setClass"
(inside “setClass("someClass", slots = list(example_slot = "character"), ”
“ prototype = list(example_slot = character()))”)
Calls: <Anonymous> ... tryCatchList -> tryCatchOne -> <Anonymous> -> rethrow -> throw
Execution halted
First question, why am I getting this above error? It makes no sense, the methods package is part of R or it's included in base. I solved this by appending methods:: to my function calls:
#' #export
someClass <- methods::setClass(
"someClass",
slots = list(
example_slot = "character"
),
prototype = list(
example_slot = character()
)
)
#' #export
methods::setMethod("show", "someClass", \(object) {
cat("\n")
cat("Example slot:", object#example_slot, "\n")
cat("\n")
})
But now I get this error; I would appreciate any insight anyone could extend to get this resolved:
someMinimalScript.R
Error in box::use(./utils/R/someMinimalModule[someClass]) :
name '.cacheOnAssign' not found in 'env'
(inside “env$.cacheOnAssign”)
Calls: <Anonymous> ... tryCatchList -> tryCatchOne -> <Anonymous> -> rethrow -> throw
Execution halted
R version
sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: aarch64-apple-darwin21.3.0 (64-bit)
Running under: macOS Monterey 12.2.1
Matrix products: default
BLAS: /opt/homebrew/Cellar/openblas/0.3.20/lib/libopenblasp-r0.3.20.dylib
LAPACK: /opt/homebrew/Cellar/r/4.2.0/lib/R/lib/libRlapack.dylib
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
loaded via a namespace (and not attached):
[1] compiler_4.2.0 data.table_1.14.2
‘box’ version
‘1.1.2’
Note this error also occurs when trying to run code interactively. It seems that box does not play nice with S4.
Module:
#' #export
someClass <- methods::setClass(
"someClass",
slots = list(
example_slot = "character"
),
prototype = list(
example_slot = character()
)
)
#' #export
methods::setMethod("show", "someClass", \(object) {
cat("\n")
cat("Example slot:", object#example_slot, "\n")
cat("\n")
})
box::use(./utils/R/someModule)
Error in box::use(./utils/R/someModule) :
name '.cacheOnAssign' not found in 'env'
(inside “env$.cacheOnAssign”)

Suppress warnings in testthat functions introduces error

I have a function in my package that uses tryCatch for condition handling.
f <- function(){
tryCatch(expr = warning("?!"), error = function(c) {message(c); 3},
warning = function(c) {message(c); 4})
}
When I try to write a unit test for this with testthat, I always get an error related to restart or muffleWarning. Any idea how to solve this? I want to match output to 4 during warning, in this case.
test_that("test_f", {
suppressWarnings(output <- f())
expect_equal(output, 4)
})
> devtools::test()
error: test_f
no 'restart' 'muffleWarning' found
Backtrace:
1. base::suppressWarnings(output <- f()) tests/testthat/test_file.R:27:2
14. base::invokeRestart("muffleWarning")
version R version 3.6.3 (2020-02-29)
os Ubuntu 16.04.6 LTS
system x86_64, linux-gnu
testthat * 2.3.2

Error while using transformation function in R

I was working with baby names data set and encountered below error while using transform function. Any guidance/suggestion would be highly appreciated. I did reinstalled the packages but of no avail.
Mac OS X (Mountain Lion)
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
library(stringr)
require(stringr)
bnames1 <- transform(bnames1,
first = tolower(str_sub(name,1,1)),
last = tolower(str_sub(name,-1,1)),
vowels = vowels(name),
length= nchar(name),
per1000 = 10000 * prop,
one_par = 1/prop
)
Error in tolower(str_sub(name, 1, 1)) :
lazy-load database '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stringr/R/stringr.rdb' is corrupt
In addition: Warning messages:
1: In tolower(str_sub(name, 1, 1)) :
restarting interrupted promise evaluation
2: In tolower(str_sub(name, 1, 1)) : internal error -3 in R_decompress1
internal error -3 is often a functioning of installing on top of a loaded package. Restart R and restart your application. There may be other issues, but until you do this you won't be going much further.
Try
remove.packages("stringr")
install.packages("stringr")

Inscrutible error in plyr: Error in names(result) <- names(pieces) : 'names' attribute [11644] must be the same length as the vector [11187]

I'm getting an error while running plyr on a cluster, and I'm having a tough time debugging it. I'm sharing pseudo-code below. I run a function called getopt wrapped in Xgetopt, which handles errors. It basically does gridsearch optimization on a matrix of inputs, row-wise. The input matrix is 11644x2.
Can anybody tell me where this error comes from or what it means? I can't do traceback, because I can't replicate the error locally.
> library(plyr)
> library(doMC)
> registerDoMC(32)
>
> Xgetopt = function(input){
+ out = tryCatch(getopt(input), error=function(e) e, finally=NA)
+ if(inherits(out, "error")) {out=NA; print("an error happened but it got handled.")}
+ return(out)
+ }
>
> tocalc = expand.grid(ID = sort(unique(m$model$ID)), price = seq(from=0,to=100,by=2.5))
> tocalc$ID = as.character(tocalc$ID)
> out = dlply(.data=tocalc,c('ID','price'),.fun=Xgetopt,.parallel=TRUE)
[1] "an error happened but it got handled."
[1] "an error happened but it got handled."
[1] "an error happened but it got handled."
snip (only a few errors, mostly successes)
[1] "99 37.5"
Error in names(result) <- names(pieces) :
'names' attribute [11644] must be the same length as the vector [11187]
Calls: dlply -> llply
Execution halted

Error thrown within ddply crashes R

I'm running into an issue where plyr consistently crashes when an error is thrown from the supplied function
> require(plyr)
Loading required package: plyr
Warning message:
package ‘plyr’ was built under R version 3.0.2
> df <- data.frame(group=c("A","A","B","B"), num=c(11,22,33,44))
> ddply(df, .(group), function(x) {x})
group num
1 A 11
2 A 22
3 B 33
4 B 44
> ddply(df, .(group), function(x) {stop("badness")})
called from: (function ()
{
.rs.breakOnError(TRUE)
})()
Error in .fun(piece, ...) : badness
Browse[1]>
# Crashes immediately
Is anyone aware of why this may be occuring and how to prevent it (other than avoiding errors of course)?
(I'm running R 3.0.1 on platform: i386-w64-mingw32/i386 (32-bit) through RStudio 0.98.274 under Windows 7)
EDIT
As a workaround, I am redirecting any errors as warnings which avoids the crashes
ddply(df, .(group), function(x) tryCatch(stop("badness"), error = function(e) warning(e)) )
Will report what happens here if I manage to align the plyr and R versions.
I got the same issue on R 3.1.1 and plyr 1.8.1.
To fix it, I just reinstalled the package from source.
install.packages("plyr", type = "source")

Resources