R6Class bug __Deferred_Default_Marker__ - r

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.

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:

Redefine arguments within an R core package function

My fingers are starting to tire of typing update.packages(checkBuilt = T, ask = F). I was wondering whether it's possible to redefine the default parameters within the update.packages() function. So far, I've tried adding the following to my .Rprofile file:
utils::assignInNamespace(
"update.packages",
function(checkBuilt = TRUE, ask = FALSE, ...) {
update.packages(checkBuilt = checkBuilt, ask = ask, ...)
},
"utils"
)
But when I try to use the function in R I get the following error:
update.packages()
Error: C stack usage 7976404 is too close to the limit
I've also just tried using formals() with the following in the .Rprofile:
local({
args_new <- alist(lib.loc = .libPaths(), ask = FALSE, checkBuilt = TRUE)
ind <- which(methods::formalArgs(update.packages) %in% names(args_new))
formals(update.packages)[ind] <- args_new
})
But that results in the following error upon launching R:
Error in formals(update.packages) : object 'update.packages' not found
As #Roland said in the comments, your definition is recursive. You shouldn't bother with the assignInNamespace: keeping the new function in your workspace is good enough. Then you can use utils::update.packages in its definition, e.g.
update.packages <- function(checkBuilt = TRUE, ask = FALSE, ...)
utils::update.packages(checkBuilt = checkBuilt, ask = ask, ...)
You should avoid using assignInNamespace for the reasons listed in its help page.

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)
}

dplyr clashes with testthat package when matches is used

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())
}

R / devtools / roxygen2 : difficulty creating package

I'm trying to turn this function found here into an R package. I'm following the directions found here.
Here are the steps I take:
1) Load required library
library(devtools)
2) Go to a new location
setwd('C:\\myRpkgs\\')
3) Create skeleton
create('conveniencePkg')
4) Copy function to file and save in 'C:\\myRpkgs\\conveniencePkg\\R\\lsos.R'
5) Run document function
setwd("./conveniencePkg")
document()
6) Install package
setwd("..")
install("conveniencePkg")
7) Load library
library(conveniencePkg)
8) try to use lsos function
>lsos()
Error in is.na(obj.dim)[, 1] : subscript out of bounds
Result is the following error:
> traceback()
2: .ls.objects(..., order.by = "Size", decreasing = TRUE, head = TRUE,
n = n)
1: conveniencePkg::lsos()
The function runs fine if I put it into an R file and just use the source() function. Anything seem incorrect in the above steps?

Resources