Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replay_html has problems when dealing with user defined S3 classes in @examples #677

Closed
s-fleck opened this issue May 23, 2018 · 14 comments
Closed
Labels
rd-translation 🎏 reprex needs a minimal reproducible example

Comments

@s-fleck
Copy link

s-fleck commented May 23, 2018

It is a bit hard for me to provide a reprex, but I will try to describe the problem as accurately as possible.
It seems rather simple anyways.

I am working on a package that deals with year-quarter dates. I have a roxygen block like this, where date_y() is a constructor for a custom s3 class for years:

#' [...]
#' @examples
#' date_y(2013)

If I run pkgdown::build_site (dev version, ‘1.0.0.9000’) I get:

Error in UseMethod("replay_html", x) : 
  no applicable method for 'replay_html' applied to an object of class "c('date_y', 'date_xx', 'integer')"

I do have proper format() and print() methods defined for date_y Objects. The problem described above does not occur if I change my @example to:

#' [...]
#' @examples
#' as.character(date_y(2013))

I havent looked to much in the internals of pkgdown, but maybe it makes sense to define a replay_html.default() like replay_html.character(format(x)) or replay_html.character(capture.output(print(x)))?

@s-fleck
Copy link
Author

s-fleck commented May 23, 2018

If you require a reproducible example to play around with, you can clone https://github.com/s-fleck/dint. The package is small, simple and has no dependencies.

@hadley hadley added bug an unexpected problem or unintended behavior rd-translation 🎏 labels May 23, 2018
@hadley
Copy link
Member

hadley commented May 23, 2018

I think there's a deeper problem because it should be replay_html.value that handles this scenario - replay_html() should never get called directly on your object.

@hadley
Copy link
Member

hadley commented May 24, 2018

Hmmmm, it works just fine for me. Can you please show me exactly what code you're running to build the package, and give me a traceback() from the error?

@hadley
Copy link
Member

hadley commented May 24, 2018

(PS. I think it's bad practice to omit the generated .Rd files from the repository)

@s-fleck
Copy link
Author

s-fleck commented May 25, 2018

When I filed the bug I was on a remote RStudio session at work. I tried it from home now and I am not able to reproduce the bug... I'll be at work again on Monday and try again there. weird...

@hadley hadley added reprex needs a minimal reproducible example and removed bug an unexpected problem or unintended behavior labels May 25, 2018
@s-fleck
Copy link
Author

s-fleck commented May 28, 2018

Apparently I changed something in the repository that fixed the issue between filing this bug and you checking it out. I could not figure out what that was, as I made few changes to the roxygen.

The last commit where the error occured is: 3160e76a
The first commit where it does not occur is: c38f1f4f

Here is the Traceback

Error in UseMethod("replay_html", x) : 
  no applicable method for 'replay_html' applied to an object of class "c('date_y', 'date_xx', 'integer')"
> traceback()
14: replay_html(parts[[i]], ...)
13: replay_html.list(expr, topic = topic, obj_id = obj_id)
12: replay_html(expr, topic = topic, obj_id = obj_id)
11: .f(code = .l[[c(1L, 1L)]], run = .l[[c(2L, 1L)]], show = .l[[c(3L, 
        1L)]], ...)
10: .Call(pmap_impl, environment(), ".l", ".f", "character")
9: purrr::pmap_chr(list(code = code, run = run, show = show), format_example_chunk, 
       env = child_env(env), topic = topic, obj_id = id_generator$id)
8: as_data.tag_examples(tags$tag_examples[[1]], env = new.env(parent = globalenv()), 
       topic = tools::file_path_sans_ext(topic$file_in), examples = examples, 
       run_dont_run = run_dont_run)
7: as_data(tags$tag_examples[[1]], env = new.env(parent = globalenv()), 
       topic = tools::file_path_sans_ext(topic$file_in), examples = examples, 
       run_dont_run = run_dont_run)
6: data_reference_topic(topic, pkg, examples = examples, run_dont_run = run_dont_run, 
       mathjax = mathjax)
5: .f(.x[[i]], ...)
4: .Call(map_impl, environment(), ".x", ".f", "list")
3: purrr::map(topics, build_reference_topic, pkg = pkg, lazy = lazy, 
       examples = examples, run_dont_run = run_dont_run, mathjax = mathjax)
2: build_reference(pkg, lazy = lazy, examples = examples, run_dont_run = run_dont_run, 
       mathjax = mathjax, seed = seed, override = override, preview = FALSE)
1: pkgdown::build_site()

I made a branch where I stripped down the package to just two functions where the bug still occurs:

https://github.com/s-fleck/dint/tree/test

@s-fleck
Copy link
Author

s-fleck commented Jun 4, 2018

Hmm now I can't reproduce the bug for the commits I mentioned, but for some older commits it still exists. I am sorry, but it seems quite hard to pinpoint why and how this happens.

Maybe this helps:

For the cases where build_site() fails, the line expr <- evaluate::evaluate(code, env, new_device = TRUE) in format_example_chunk() returns a list that contains some elements that are of my custom s3 class (date_xx), while for cases where build_site() succeeds, expr is a list that only contains character and source elements.

I am not sure what evaluate() is supposed to return, as I have not worked with that package before.

@jayhesselberth
Copy link
Collaborator

Closing for now. If the issue persists could you please build a minimal package that recreates the issue?

@s-fleck
Copy link
Author

s-fleck commented Oct 6, 2018

I was not able to pinpoint the issue. It persisted in some version of the package but not in others, without any changes in between that had much to do with the roxygen. Maybe it was just a weird combination of several unfortunate circumstances.

@s-fleck
Copy link
Author

s-fleck commented Jun 4, 2019

The problem occurred again to me today, after more than one year of not encountering the issue and for a different package. I am using the most recent github version of pkgdown (1.3.0.9100). I am not reopening the issue because I still can't produce a reprex.

Reading 'man/logger_tree.Rd'
Error in UseMethod("replay_html", x) : 
  no applicable method for 'replay_html' applied to an object of class "c('logger_tree', 'data.frame')"

The traceback is not really helpful:

5: stop(err[[2]])
4: get_result(output = out, options)
3: callr::r(function(..., crayon_enabled, crayon_colors, pkgdown_internet) {
       options(crayon.enabled = crayon_enabled, crayon.colors = crayon_colors, 
           pkgdown.internet = pkgdown_internet)
       pkgdown::build_site(...)
   }, args = args, show = TRUE, timeout = getOption("pkgdown.timeout", 
       Inf))
2: build_site_external(pkg = pkg, examples = examples, run_dont_run = run_dont_run, 
       seed = seed, lazy = lazy, override = override, preview = preview, 
       devel = devel)
1: pkgdown::build_site()

Here is a direct link to the package at the commit that caused the problem:
s-fleck/lgr@54218c6

When I posted this comment originally, other people were not able to reproduce my problem with the package in question. For me at least the problem persists across two different systems (both ubuntu, one with R 3.5.2 and one with R 3.6.0).

@hadley @jayhesselberth

@s-fleck
Copy link
Author

s-fleck commented Jun 10, 2019

@cpsievert

Okay this is super weird: I did the following steps:

  • added the reference to function in question to pkgdown.yaml (I have a custom function index and accidental left out that function)
  • implemented a format() method for the returned S3 object (before there was only a print() method for the S3 class)

The problem disappeared. It does not reappear if I remove the format method again and/or remove the function from pkgdown.yaml, even if I delete /docs in the process.

@cpsievert
Copy link

cpsievert commented Jun 17, 2019

Thanks @s-fleck, adding a format() method in my case doesn't appear to fix the problem.

@ngreifer
Copy link

I was having this same problem with my package MatchingFrontier when trying to build the website from a GitHub action that calls pkgdown. I also have a custom-classed object that seems to be causing the problem. I managed to fix it by ensuring my print() method for the object invisibly returned the object (i.e., using return(invisible(x)) instead of return(x)). Not sure if this helps anyone.

@sorhawell
Copy link

sorhawell commented Sep 26, 2022

I had the same issue as described here on some very customized S3-classed. For me @ngreifer simple suggestion did the trick. I'm very puzzled why that works :) However, to spell it out, a corrected print method could look like this:

#' Print mys3class
#'
#' @param x mys3class
#'
#' @return invis selfie
#' @export
#'
print.mys3class = function(x) {
  cat("mys3class: ")
  #some code to print your s3 class could be added here
  invisible(x)
}

gvegayon added a commit to UofUEpiBio/epiworldR that referenced this issue May 1, 2023
rsh52 added a commit to CHOP-CGTInformatics/ggswim that referenced this issue May 8, 2024
Adding `invisible()` helps resolve errors related to print method. See:

r-lib/pkgdown#677
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rd-translation 🎏 reprex needs a minimal reproducible example
Projects
None yet
Development

No branches or pull requests

6 participants