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
Related
Is there any way to run an applescript within R?
I found this reference in an R FAQ on CRAN
From release 1.3.1 R has partial support for AppleScripts. This means two things: you can run applescripts from inside R using the command applescript() (see the corresponding help)
But in my current version of R
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
neither applescript() nor ?applescript() returns anything.
Thanks, Simon
Those features aren't in modern R versions (IIRC they harken back to pre-macOS/Mac OS X days).
However, the applescript() function performed no magic:
applescript <- function(script_source, extra_args = c()) {
script_source <- paste0(script_source, collapse = "\n")
tf <- tempfile(fileext = ".applescript")
on.exit(unlink(tf), add=TRUE)
cat(script_source, file = tf)
osascript <- Sys.which("osascript")
args <- c(extra_args, tf)
system2(
command = osascript,
args = args,
stdout = TRUE
) -> res
invisible(res)
}
So you can do anything with it, like open a folder:
applescript(
sprintf(
'tell app "Finder" to open POSIX file "%s"',
Sys.getenv("R_DOC_DIR")
)
)
or, query an app and return data:
res <- applescript('
tell application "iTunes"
set r_name to name of current track
set r_artist to artist of current track
end
return "artist=" & r_artist & "\ntrack=" & r_name
')
print(res)
## [1] "artist=NICO Touches the Walls" "track=Hologram"
For (mebbe) easier usage (I say "mebbe" as the pkg relies on reticulate for some things) I added this to the macthekinfe macOS-centric R package.
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
Does anybody have an idea for what I need to reconfigure?
I'm on Mac OS X Version 9.0 (10601.1.56.2) and R version 3.2.2
The edit() function seems to work for simple objects and functions
R segfaults if I try editing a data frame however
R version 3.2.2 (2015-08-14) -- "Fire Safety"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
[R.app GUI 1.66 (6996) x86_64-apple-darwin13.4.0]
> x <- rnorm(10)
> edit(x)
[1] 0.96793711 0.46449359 0.05136297 0.38438847 0.73942230 -1.12691486 -0.84414357 -3.67522840 -1.03864446
[10] -0.37159107
> x <- data.frame(x)
> edit(x)
*** caught segfault ***
address 0x10db0, cause 'memory not mapped'
Traceback:
1: as.matrix.data.frame(format.data.frame(x, digits = digits, na.encode = FALSE))
2: as.matrix(format.data.frame(x, digits = digits, na.encode = FALSE))
3: print.data.frame(x)
4: function (x, ...) UseMethod("print")(x)
Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection:
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")
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")