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

Zero-row nested tibbles generate spurious warnings when knitted into html_document or html_notebook documents #8898

Open
4 tasks done
davidski opened this issue Feb 5, 2021 · 4 comments

Comments

@davidski
Copy link

@davidski davidski commented Feb 5, 2021

Follow up on RStudio Community thread - https://community.rstudio.com/t/warnings-when-knitting-zero-row-nested-tibbles/95164

System details

RStudio Edition : Desktop
RStudio Version : 1.4.1103
OS Version      : MacOS Big Sur
R Version       : 4.0.3

Steps to reproduce the problem

Embed the following reprex code in a fresh RMarkdown notebook and knit to html_document or html_notebook

library(dplyr, warn.conflicts = FALSE)
mt_df <- mtcars %>% tidyr::nest(data = c(cyl))
mt_df %>% filter(disp == 160)
#> # A tibble: 2 x 11
#>     mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb data            
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list>          
#> 1    21   160   110   3.9  2.62  16.5     0     1     4     4 <tibble [1 × 1]>
#> 2    21   160   110   3.9  2.88  17.0     0     1     4     4 <tibble [1 × 1]>
mt_df %>% filter(disp == 999)
#> # A tibble: 0 x 11
#> # … with 11 variables: mpg <dbl>, disp <dbl>, hp <dbl>, drat <dbl>, wt <dbl>,
#> #   qsec <dbl>, vs <dbl>, am <dbl>, gear <dbl>, carb <dbl>, data <list>

Created on 2021-02-04 by the reprex package (v1.0.0)

Session info
sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur 10.16
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.0.2
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.31       magrittr_2.0.1   tidyselect_1.1.0 R6_2.5.0        
#>  [5] rlang_0.4.10     fansi_0.4.1      stringr_1.4.0    styler_1.3.2    
#>  [9] highr_0.8        tools_4.0.3      xfun_0.19        utf8_1.1.4      
#> [13] cli_2.3.0        htmltools_0.5.0  ellipsis_0.3.1   assertthat_0.2.1
#> [17] yaml_2.2.1       digest_0.6.27    tibble_3.0.6     lifecycle_0.2.0 
#> [21] crayon_1.4.0     tidyr_1.1.2      purrr_0.3.4      vctrs_0.3.6     
#> [25] fs_1.5.0         glue_1.4.2       evaluate_0.14    rmarkdown_2.6   
#> [29] reprex_1.0.0     stringi_1.5.3    compiler_4.0.3   pillar_1.4.7    
#> [33] generics_0.1.0   backports_1.2.1  pkgconfig_2.0.3

Describe the problem in detail

If a nested tibble is filtered to zero rows and then printed in a knitted html (notebook or doument) context, a spurious warning message is generated reading:

Warning messages: 1: In [<-.data.frame(tmp, is_list, value = list(11 = "<>")) : replacement element 1 has 1 row to replace 0 rows

This may be related to #7546 and #7562.

Describe the behavior you expected

A zero-length tibble is printed without warnings (or other errors).

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • If I am reporting a RStudio crash, I have included a diagnostics report.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
@ronblum
Copy link
Contributor

@ronblum ronblum commented Feb 8, 2021

@davidski Thank you for raising this! I can reproduce this in

  • RStudio Desktop 1.4.1540 on MacOS 11.3
  • RStudio Server 1.4.1533 on Red Hat 8.3

We'll review this issue as we continue to work on improving RStudio.

Note: I can only reproduce this with html_notebook.

@kevinushey
Copy link
Contributor

@kevinushey kevinushey commented Feb 8, 2021

Simpler reprex:

```{r}
data <- list(x = list())
class(data) <- "data.frame"
rownames(data) <- character()
data
```

Most likely coming from here somewhere:

.rs.addFunction("readDataCapture", function(path)
{
type_sum <- function(x) {
format_sum <- switch (class(x)[[1]],
ordered = "ord",
factor = "fctr",
POSIXt = "dttm",
difftime = "time",
Date = "date",
data.frame = class(x)[[1]],
tbl_df = "tibble",
NULL
)
if (!is.null(format_sum)) {
format_sum
} else if (!is.object(x)) {
switch(typeof(x),
logical = "lgl",
integer = "int",
double = "dbl",
character = "chr",
complex = "cplx",
closure = "fun",
environment = "env",
typeof(x)
)
} else if (!isS4(x)) {
paste0("S3: ", class(x)[[1]])
} else {
paste0("S4: ", methods::is(x)[[1]])
}
}
"%||%" <- function(x, y) {
if(is.null(x)) y else x
}
big_mark <- function(x, ...) {
mark <- if (identical(getOption("OutDec"), ",")) "." else ","
formatC(x, big.mark = mark, ...)
}
dim_desc <- function(x) {
dim <- dim(x) %||% length(x)
format_dim <- vapply(dim, big_mark, character(1))
format_dim[is.na(dim)] <- "??"
paste0(format_dim, collapse = " \u00d7 ")
}
is_atomic <- function(x) {
is.atomic(x) && !is.null(x)
}
is_vector <- function(x) {
is_atomic(x) || is.list(x)
}
paged_table_is_vector_s3 <- function(x) {
switch(class(x)[[1]],
ordered = TRUE,
factor = TRUE,
Date = TRUE,
POSIXct = TRUE,
difftime = TRUE,
data.frame = TRUE,
!is.object(x) && is_vector(x))
}
size_sum <- function(x) {
if (!paged_table_is_vector_s3(x)) return("")
paste0(" [", dim_desc(x), "]" )
}
obj_sum.default <- function(x) {
paste0(type_sum(x), size_sum(x))
}
obj_sum <- function(x) {
switch(class(x)[[1]],
POSIXlt = rep("POSIXlt", length(x)),
list = vapply(x, obj_sum.default, character(1L)),
paste0(type_sum(x), size_sum(x))
)
}
e <- new.env(parent = emptyenv())
load(file = path, envir = e)
# this works around a strange bug in R 3.5.1 on Windows
# where output can be mis-encoded after a save / load
cat(NULL, sep = "")
data <- head(e$x, getOption("max.print", 1000))
data <- if (is.null(data)) as.data.frame(list()) else data
options <- e$options
columnNames <- names(data)
columnSequence <- seq_len(ncol(data))
columns <- lapply(
columnSequence,
function(columnIdx) {
column <- data[[columnIdx]]
baseType <- class(column)[[1]]
tibbleType <- type_sum(column)
list(
label = if (!is.null(columnNames)) columnNames[[columnIdx]] else "",
name = columnIdx,
type = tibbleType,
align = if (baseType == "character" || baseType == "factor") "left" else "right"
)
}
)
names(data) <- as.character(columnSequence)
addRowNames = isTRUE(options[["rownames.print"]])
if (addRowNames) {
columns <- c(
list(
list(
label = "",
name = "_rn_",
type = "",
align = "left"
)
),
columns
)
data$`_rn_` <- rownames(data)
}
columns <- unname(columns)
is_list <- vapply(data, is.list, logical(1))
data[is_list] <- lapply(data[is_list], function(x) {
summary <- obj_sum(x)
paste0("<", summary, ">")
})
# R 3.6.0 on Windows has an issue where RGui escapes can 'leak'
# into encoded strings; detect and remove those post-hoc.
# (should be fixed in R 3.6.1)
#
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17567
needsEncodeFix <-
.Platform$OS.type == "windows" &&
getRversion() == "3.6.0"
data <- as.data.frame(
lapply(
data,
function (y) {
# escape NAs from character columns
if (typeof(y) == "character") {
y[y == "NA"] <- "__NA__"
}
# encode string (ensure control characters are escaped)
y <- encodeString(format(y))
if (needsEncodeFix) {
y <- gsub("^\002\377\376", "", y)
y <- gsub("\003\377\376$", "", y)
}
# trim spaces
gsub("^\\s+|\\s+$", "", y)
}
),
stringsAsFactors = FALSE,
optional = TRUE)
pagedTableOptions <- list(
columns = list(
min = options[["cols.min.print"]],
max = if (is.null(options[["cols.print"]])) 10 else options[["cols.print"]],
total = options[["cols.total"]]
),
rows = list(
min = if (is.null(options[["rows.print"]])) 10 else options[["rows.print"]],
max = if (is.null(options[["rows.print"]])) 10 else options[["rows.print"]],
total = options[["rows.total"]]
),
pages = options[["pages.print"]]
)
list(
columns = columns,
data = if (length(data) == 0) list() else data,
options = pagedTableOptions
)
})

@github-actions
Copy link

@github-actions github-actions bot commented Aug 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.

@github-actions github-actions bot added the stale Issues which have been closed automatically due to inactivitiy. label Aug 8, 2021
@davidski
Copy link
Author

@davidski davidski commented Aug 8, 2021

This issue still exists under RStudio 1.4.1725 and is accepted by the team as reproducible. Please don't close this issue out as stale as it is still relevant and needs attention.

@stale stale bot removed the stale Issues which have been closed automatically due to inactivitiy. label Aug 8, 2021
@mikebessuille mikebessuille added this to the Later milestone Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants