From 9c050e1f12a306350c78eb55aba9d2dd180d4c85 Mon Sep 17 00:00:00 2001 From: Joe Zhu Date: Wed, 8 Jan 2025 04:30:22 +0800 Subject: [PATCH] attempt to close #238 (#242) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` > library(rlistings) Loading required package: formatters Attaching package: ‘formatters’ The following object is masked from ‘package:base’: %||% Loading required package: tibble > result <- tibble::tibble(col1 = character(0L), col2 = character(0L), col3 = character(0L)) > > listing <- rlistings::as_listing(result) > > sprintf("Rows: %d // Columns: %d", nrow(listing), ncol(listing)) [1] "Rows: 0 // Columns: 3" > > print(listing) [1] "No observation in the listing object." ``` --------- Signed-off-by: Joe Zhu Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> --- R/rlistings_methods.R | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/R/rlistings_methods.R b/R/rlistings_methods.R index d5ca8d6f..cc5a3e36 100644 --- a/R/rlistings_methods.R +++ b/R/rlistings_methods.R @@ -15,17 +15,25 @@ dflt_courier <- font_spec("Courier", 9, 1) #' @export #' @name listing_methods print.listing_df <- function(x, widths = NULL, tf_wrap = FALSE, max_width = NULL, fontspec = NULL, col_gap = 3L, ...) { - cat( - toString( - matrix_form(x, fontspec = fontspec, col_gap = col_gap), - widths = widths, - tf_wrap = tf_wrap, - max_width = max_width, - fontspec = fontspec, - col_gap = col_gap, - ... + tryCatch({ + cat( + toString( + matrix_form(x, fontspec = fontspec, col_gap = col_gap), + widths = widths, + tf_wrap = tf_wrap, + max_width = max_width, + fontspec = fontspec, + col_gap = col_gap, + ... + ) ) - ) + }, error = function(e) { + if (nrow(x) == 0) { + print("No observation in the listing object.") + } else { + stop(e) + } + }) invisible(x) }