In R, the same code cannot be knit out in package Vignette file. “list” object cannot be coerced to type integer

前端 未结 1 596
借酒劲吻你
借酒劲吻你 2021-01-18 22:27

This question is about generate_msts() function in package GRATIS.

I add some new stuff (make the function has options to transform its output into a lovely tsibble f

相关标签:
1条回答
  • 2021-01-18 22:53

    I tried to run this for you - my first guess was a NAMESPACE problem. But it seems also related to the generate_msts() function.

    I really don't think this has to do with first saving it to a variable x.

    Here are my findings:

    DOES NOT WORK:

    x <- generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
    
    x
    

    DOES NOT WORK:

    print(generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble"))
    

    DOES NOT WORK:

    x <- generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
    
    print(x)
    

    WORKS:

     head(generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble"))
    

    In the failure cases it is always the same error message as for you:

    Error: processing vignette 'QuickStart.Rmd' failed with diagnostics: 'list' object cannot be coerced to type 'integer'

    So since head(), str(), class() always worked for me and only print() did not work, I am assuming it is a problem with the print function. So your workaround with saving it into variable x only worked fine, because you did not call the print function.

    Also important the problem only occurred for me when using generate_msts() inside Rmarkdown. As I explain later this seems reasonable, since printing in knitr is different from printing on the console.

    When I alter your generate_msts() and rebuild the package:

    output <- if (output_format == "list") {
        res
      } else if (output_format == "tsibble") {
        tsibble(date = as.Date("2017-01-01") + 0:9,value = rnorm(10))
      }
    

    The Rmarkdown suddenly runs without an error.

    My guess would be it is a problem with the print() for your specific data in interaction with knitr.

    Printing in knitr seems to be different from printing on the console (might be why it works without rmarkdown)

    Here is a nice link about custom print methods and knitr: https://cran.r-project.org/web/packages/knitr/vignettes/knit_print.html

    Before knitr v1.6, printing objects in R code chunks basically emulates the R console.

    I could imagine the S3 method for knit_print from the tsibble package (which just uses all the printing methods from tibble?) might just not work properly for your specific dataset (I mean it worked for the tsibble I created with tsibble() ). But just a (wild?) guess...the error and behavior overall is really strange ...

    Edit: Here is also the R Markdown callstack for the error:

     1. ├─base::print(x)
      2. └─tibble:::print.tbl(x)
      3.   ├─cli::cat_line(format(x, ..., n = n, width = width, n_extra = n_extra))
      4.   │ └─base::paste0(..., collapse = "\n")
      5.   ├─base::format(x, ..., n = n, width = width, n_extra = n_extra)
      6.   └─tsibble:::format.tbl_ts(x, ..., n = n, width = width, n_extra = n_extra)
      7.     ├─base::format(trunc_mat(x, n = n, width = width, n_extra = n_extra))
      8.     └─tibble::trunc_mat(x, n = n, width = width, n_extra = n_extra)
      9.       ├─base::as.data.frame(head(x, n))
     10.       ├─utils::head(x, n)
     11.       └─utils:::head.data.frame(x, n)
     12.         └─base::lapply(...)
     13.           └─utils:::FUN(X[[i]], ...)
    

    Should be similar for you, but if you want to get this on your own, you have to the following commands to your rmarkdown document

    options(rlang_trace_top_env = rlang::current_env())
    options(error = function() {
      sink()
      print(rlang::trace_back(bottom = sys.frame(-1)), simplify = "none")
    })
    

    But as you can see in the callstack, the error is caused by base::print(x), which calls the S3 method tibble:::print.tbl(x), this method then internally calls tsibble:::format.tbl_ts, which calls tibble::trunc_mat, ... and somewhere inside the error is caused.

    Ok ... I followed this further down the road and ... what in the end messes inside these function calls, are the knitr options you set in the beginning.

    You write at the beginning of your rmarkdown:

    original <- options("tibble.print_min")
    options(tibble.print_min = 5)
    
    # <---- Do stuff with changed option, e.g. print some tibbles ----> 
    options(tibble.print_min = original)
    

    Change this to just:

    options(tibble.print_min = 5)
    

    Should work then.

    0 讨论(0)
提交回复
热议问题