R/render.R

Defines functions render_funr

Documented in render_funr

## --- showing the output of the returned..

# if error, ultimately, die with a error
# show a clear, clean error message

#' Render output of functions in a print friendly format
#'
#' @description
#' If the function returns with invisible, output is suppressed
#'
#' @param x a output from \code{funr}
#' @param max_rows In case output of a function is a data.frame, the number
#' of rows to display.
#'
#' @export
#'
render_funr <- function(x, max_rows = 100){

  #print(class(out))
  if(class(x)[1] == "try-error")
    cat("")

  out = try(x$value, silent = TRUE)
  vis = ifelse(length(x$visible) == 0, FALSE, x$visible)

  #message("visible status: ", vis)

  if(!vis){
    return(cat(""))

    # if list and size is greater than 200KB, save the results as an RDS file
    # saved in the cwd as funr_results.rds
  }else if(is.list(out) & (object.size(out)/1024) > 200){
    message("This seems like a large list object. Saving results as rds file funr_results.rds")
    saveRDS(out, "funr_results.rds")
    # try(head(out, max_rows), silent = TRUE)

  }else if(is.data.frame(out)){
    message("Showing the first ", max_rows, " rows of the data.frame")
    try(head(out, max_rows), silent = TRUE)

  }else if(class(out) == "help_files_with_topic"){
    ## print help files
    print(out)

  }else if(is.null(out)){
    ## skip NULL
    cat("")

  }else if(is.list(out)){
    ## print list
    print(out)

  }else if(is.atomic(out)){
    cat(out)

  }else if(is.function(out)){
    print(out)

  }else{
    cat("")
  }
}
sahilseth/funr documentation built on March 10, 2021, 4:16 p.m.