Skip to content

do.call tinyplot_add #499

@FlorianSchwendinger

Description

@FlorianSchwendinger

Hey tinyplot maintainers, first, thank you for this excellent package, which makes plotting a delight.

I recognized since version 0.5.0 do.call does not work any more in combination with tinyplot_add (in version tinyplot_0.4.2 it did work as expected).

Below is a minimal example to reproduce the error. Note you have to start a fresh R session where tinyplot was not used jet.

library("tinyplot")

df <- data.frame(x = 1:10, y = (1:10)^2)

# Raises no previous tinyplot call found error.
do.call(tinyplot, list(y ~ x, data = df, type = "p"))
tinyplot_add(type = "l", data = df)
#R> Error in tinyplot_add(type = "l", data = df) :
#R>   No previous tinyplot call found.

# Workes fine.
tinyplot(y ~ x, data = df, type = "p")
tinyplot_add(type = "l", data = df)

The issue seams to be that the following regex does not match when using do.call

    calls = sys.calls()
    tinyplot_calls = "(^tinyplot$)|(^tinyplot::tinyplot$)|(^plt$)|(^tinyplot::plt)|(^tinyplot:::)"
    idx = grep(tinyplot_calls, sapply(calls, function(k) k[[1]]))

since sys.calls() gives in this case

[[1]]
do.call(tinyplot, list(y ~ x, data = df, type = "p"))

[[2]]
(function (x, ...) 
{
    UseMethod("tinyplot")
})(y ~ x, data = list(x = 1:10, y = c(1, 4, 9, 16, 25, 36, 49, 
64, 81, 100)), type = "p")

[[3]]
tinyplot.formula(y ~ x, data = list(x = 1:10, y = c(1, 4, 9, 
16, 25, 36, 49, 64, 81, 100)), type = "p")

[[4]]
tinyplot.default(x = x, y = y, by = by, facet = facet, facet.args = facet.args, 
    data = data, type = type, xmin = mf[["(xmin)"]], xmax = mf[["(xmax)"]], 
    ymin = mf[["(ymin)"]], ymax = mf[["(ymax)"]], xlim = xlim, 
    ylim = ylim, main = main, sub = sub, xlab = xlab, ylab = ylab, 
    ann = ann, axes = axes, frame.plot = frame.plot, asp = asp, 
    grid = grid, legend_args = legend_args, pch = pch, col = col, 
    lty = lty, lwd = lwd, restore.par = restore.par, ...)

You might could use something like that instead of the grep, since comparing objects should
be more reliable.

is_tinyplot_method <- function(x) {
  fun <- try(eval(x[[1]]), silent = TRUE)
  identical(tinyplot::tinyplot, fun)
}
idx <- which(as.logical(lapply(calls, is_tinyplot_method)))

Hope this helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions