nalimilan / R2HTML

R2HTML: HTML exportation for R objects
http://cran.r-project.org/web/packages/R2HTML
3 stars 5 forks source link

Subscript out of bounds error from tbl data frame above 20 rows #4

Open samgilbert1983 opened 7 years ago

samgilbert1983 commented 7 years ago

I'm getting a subscript out of bounds error when I try to insert a data frame tbl with more than 20 rows into an html document.

require(R2HTML)
require(dplyr)

df <-
  tbl_df(data.frame(col1 = 1:21))

working_dir <- paste0("/home/", system('whoami', intern = TRUE))

HTML_file <- HTMLInitFile(outdir = working_dir,
                          filename = "file")

HTML(df)

The above example errors with:

Error in x.formatted[i, ] : subscript out of bounds

Removing the tbl_df() works as expected.

Session info:

R version 3.3.3 RC (2017-02-27 r72279)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

dplyr_0.5.0  R2HTML_2.3.2

tibble_1.3.3
nalimilan commented 7 years ago

Sorry, but nobody actively develops the package nowadays. If you want this to be fixed I'm afraid you'll have to debug it yourself. There's probably something that tibble does differently from data frames, despite pretending to be a data frame.

tfry-git commented 2 years ago

For what it's worth, the cause of the error is:

dim(df)  # [1] 21  1
as.matrix(format(df)) # [1] 14  1

HTML.data.frame - reasonably, IMO - assumes that both are equal, but tibble:::format.tbl_df() shortens it beyond 20 rows (and formats it is a way that does not immediately make any sense to me).

HTML(data.frame(df)) works.

tfry-git commented 2 years ago

Explicitly coercing x <- data.frame(x) inside HTML.data.frame() would do the trick in this case, but I'm not entirely sure, whether there could be unintended side-effects for other data.frame derived classes.

aitap commented 2 years ago

Thanks for the diagnosis!

One way of solving this which sounds relatively safe would be to define a separate method for tibbles:

HTML.tbl_df <- function(x, ...) HTML(data.frame(x), ...)