Skip to content

do.call tinyplot_add #499

Closed
Closed

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.

Activity

  1. grantmcdermott commented on Sep 25, 2025

    @grantmcdermott
    Owner

    Thanks very much for the detailed report @FlorianSchwendinger.

    @zeileis would you be able to look into this when you get a sec?

  2. zeileis commented on Sep 25, 2025

    @zeileis
    Collaborator

    Florian, sorry for the inconvenience and thanks for digging into this. I didn't think about do.call(), good catch. I'll try to look at this later today.

  3. zeileis commented on Oct 6, 2025

    @zeileis
    Collaborator

    Thanks again for the report @FlorianSchwendinger, very much appreciated. Apologies for the delay...I was hit by the new semester (with 850 instead of the expected 700 first-year students in my course).

    Finally, I had a closer look, slightly tweaked your suggested code, added a NEWS item and test case, and opened a PR.

    Grant @grantmcdermott does this look ok to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions