dplyr clashes with testthat package when matches is used - r

I am getting an error because testthat::matches clashes with dplyr::matches, and I want to know how to use testthat::test_file to check functions which contain calls to matches(), without having to specify dplyr::matches in the function body.
E.g.:
> testthat::test_file('tmp_fn_testthat_test.R')
Attaching package: ‘testthat’
The following object is masked from ‘package:dplyr’:
matches
The following object is masked from ‘package:purrr’:
is_null
Show Traceback
Rerun with Debug
Error in -matches("tmp") : invalid argument to unary operator In addition: Warning message:
package ‘testthat’ was built under R version 3.2.5
DONE =========================================================================================================================================
This error can be reproduced by saving the following code in a file called tmp_fn_testthat_test.R in your working directory, and running the command testthat::test_file('tmp_fn_testthat_test_20161115.R'). Note that sourcing or running the expect_equal command while testthat is not loaded makes the test pass.
tmp_fn <- function() {
tmp_df <- data.frame(tmp_a = 1, tmp_b = 2)
tmp_df %>%
select(-matches('tmp')) %>%
ncol
}
testthat::expect_equal(tmp_fn(), 0)

This is a known issue with dplyr 0.5. The recommended solution is to use an explicit namespace prefix: dplyr::matches.

A work around appears to be commenting out the library(testthat) in the definition of testthat::test_file, and making function calls explicit (not sure whether this will have bad side effects):
my_test_that_file <- function (path, reporter = "summary", env = testthat::test_env(), start_end_reporter = TRUE,
load_helpers = TRUE)
{
# library(testthat)
reporter <- testthat:::find_reporter(reporter)
if (load_helpers) {
testthat:::source_test_helpers(dirname(path), env = env)
}
lister <- testthat:::ListReporter$new()
if (!is.null(reporter)) {
reporter <- testthat:::MultiReporter$new(reporters = list(reporter,
lister))
}
else {
reporter <- lister
}
testthat::with_reporter(reporter = reporter, start_end_reporter = start_end_reporter,
{
lister$start_file(basename(path))
testthat::source_file(path, new.env(parent = env), chdir = TRUE)
testthat:::end_context()
})
invisible(lister$get_results())
}

Related

I get Error: invalid version specification ‘0,2’ when I use the function dm_draw() in r

I want to use the function dm_draw() to visualize a dm object but when I ran the command I get the error message " Error: invalid version specification ‘0,2’". I've tried the code included in the vignette "Visualizing dm objects" (https://cran.r-project.org/web/packages/dm/vignettes/tech-dm-draw.html) and I get the same error message when I run the dm_draw() function.
library(dm)
library(dplyr)
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
dm_draw(flights_dm_w_many_keys)
I'm using dm version 0.2.7 and DiagrammeR 1.0.8. R version 4.1.2
I'm looking for a solution to visualize a dm object, it can be also different from dm_draw().
I hope someone can help me to get that done. Sorry for my broken English and thanks for your time, any type of help is appreciated.
You can use this code:
library(dm)
library(dplyr)
library(DiagrammeR)
library(DiagrammeRsvg)
# Use this function
dm_draw_svg = function(dm,...) {
if (!requireNamespace("DiagrammeRsvg", quietly = TRUE)) {
stop(
"Package \"DiagrammeRsvg\" must be installed to use this function.",
call. = FALSE
)
}
dm::dm_draw(dm = dm, ...) %>%
DiagrammeRsvg::export_svg() %>%
htmltools::HTML() %>%
htmltools::html_print()
}
flights_dm_w_many_keys <- dm_nycflights13(color = FALSE)
# plot
dm_draw_svg(flights_dm_w_many_keys)
Output:

R package "feather" give me error "Error in check_dots_empty(action = signal) : unused argument (action = signal)"

I start getting a very strange error from the "feather" package in R:
Let say I write and read file
write_feather(mtcars, 'm')
read_feather('m')
The last one gives me
Error in check_dots_empty(action = signal) :
unused argument (action = signal)
I reinstalled the package, restarted sessions, and still have no idea how to fix it. R version 3.6.1 (2019-07-05)
Please, help me
Here is a solution I found at the end:
devtools::install_github("tidyverse/tibble", force = TRUE)
The problem lies in updated code for 'read_feather' function.
It now using 'as_tibble' function. By updating 'tibble' package this starts working fine.
Here is the code of 'read_feather' in new version:
tibble: 3.1.0 - feather: 0.3.5 - read_feather
function (path, columns = NULL)
{
data <- feather(path)
on.exit(close(data), add = TRUE)
if (is.null(columns))
as_tibble(data)
else as_tibble(data[columns])
}
Here is the code of 'read_feather' in previous version:
tibble: 3.0.1 - feather: 0.3.5 - read_feather
function (file, col_select = NULL, as_data_frame = TRUE, ...)
{
reader <- FeatherTableReader$create(file, ...)
all_columns <- ipc___feather___TableReader__column_names(reader)
col_select <- enquo(col_select)
columns <- if (!quo_is_null(col_select)) {
vars_select(all_columns, !!col_select)
}
out <- reader$Read(columns)
if (isTRUE(as_data_frame)) {
out <- as.data.frame(out)
}
out
}

R reticulate package and python sub-modules

When trying to mimic some python code using R's reticulate package:
import_from_path("jwt_auth", "C:/Program Files/Python 3.5/Lib/site-packages/boxsdk/auth")
I get this error:
Error in py_module_import(module, convert = convert) :
SystemError: Parent module '' not loaded, cannot perform relative import
Detailed traceback:
File "C:\Program Files\Python 3.5\Lib\site-packages\boxsdk\auth\jwt_auth.py", line 13, in <module>
from .oauth2 import OAuth2
Is there a better way to load sub-modules in R like such:
from boxsdk import JWTAuth
I haven't seen any solid solution. In the package I do something like this. Interestingly the drill down of reportlab can be done with dot notation.
# this is needed in case we use python in the R code rather than source_python
fitz <- NULL
pdfr <- NULL
pdfw <- NULL
repl <- NULL
.onLoad <- function(libname, pkgname) {
# this will fail in package check
reticulate::use_condaenv(condaenv = "myenv", required = TRUE)
fitz <<- reticulate::import("fitz", delay_load = TRUE)
pdfr_0 <- reticulate::import("PyPDF2", delay_load = TRUE)
pdfr <<- pdfr_0$PdfFileReader
pdfw <<- pdfr_0$PdfFileWriter
repl <- reticulate::import("reportlab.pdfgen.canvas", delay_load = TRUE)
}

R6Class bug __Deferred_Default_Marker__

I'm implementing a new R6Class and trying to add new members dynamically (https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html#adding-members-to-an-existing-class) but I get this error "__Deferred_Default_Marker__" (whether it be dynamic or not) when I implement the getx2 function.
Simple <- R6Class("Simple",
public = list(
x = 1,
getx = function() self$x,
getx2 = function() return(self$x * 2)
)
)
# To replace an existing member, use overwrite=TRUE
Simple$set("public", "x", 10, overwrite = TRUE)
s <- Simple$new()
s$getx2() # this returns "__Deferred_Default_Marker__"
Any ideas on this? It's exactly like in the documentation
The solution was to update the package. The problem with the following instruction:
devtools::install_github('r-lib/R6', build_vignettes = FALSE)
was it threw me the following error: namespace 'R6' is imported by 'CompatibilityAPI', 'mrsdeploy' so cannot be unloaded"
so i closed RStudio, and opened R.exe (C:\Program Files\R\R-3.3.3\bin) and ran the same command. Now, I have this package:
Package: R6
Version: 2.2.2.9000
URL: https://github.com/r-lib/R6/
and it works as in the specification.

How to initialize libraries by their string names in cluster?

I want to initialize libraries in cluster by their names represented as strings.
This code works fine:
library(snowfall, rlecuyer, rsprng)
sfInit(parallel = TRUE, cpus = 4, type = "SOCK")
sfClusterEval(library(e1071))
And this code produces en error: 4 nodes produced errors; first error: object 'expr' not found
library(snowfall, rlecuyer, rsprng)
sfInit(parallel = TRUE, cpus = 4, type = "SOCK")
lib <- "e1071"
expr <- parse(text=paste("library(", lib, ")", sep=""))
sfClusterEval(expr)
So sfClusterEval try to evaluate expr and not an expression which expr contains. I cannot undestand which type of expression should be passed to sfClusterEval function, which uses substitute in its body
> sfClusterEval
function (expr, stopOnError = TRUE)
{
sfCheck()
if (sfParallel()) {
return(sfClusterCall(eval, substitute(expr), env = globalenv(),
stopOnError = stopOnError))
}
else {
return(eval(expr, envir = globalenv(), enclos = parent.frame()))
}
}
This question seems simple, but I could not solve it and need someone's advice.
UPDATE:
Further investigation details on simplier examples. I feel that the truth is near.
This code works fine
sfClusterEval(library("e1071"))
But this call produces en error: 4 nodes produced errors; first error: object 'lib' not found
lib <- "e1071"
sfClusterEval(library(lib, character.only=TRUE))
ANSWER:
The variable lib should be exported to the cluster previously. And after that it can be removed.
lib <- "e1071"
sfExport("lib")
sfClusterEval(library(lib, character.only=TRUE))
sfRemove("lib")
Thanks for Richie, for giving the starting idea!
You can use sfLibrary to load extra packages on workers. See ?snowfall and click snowfall-tools.
Whether in a cluster or not, you simply use the character.only argument to library.
library("e1071", character.only = TRUE)
If your nodes report an error stating that they can't find the package, double check that the package is installed on that machine, in a location that is one of .libPaths(). If all else fails, explicitly provide the location of the package in the lib.loc argument to library.

Resources