elong0527 / r4csr

R for Clinical Study Report and Submission
https://r4csr.org/
Other
69 stars 31 forks source link

Avoid `head()` function to control number of rows #115

Closed elong0527 closed 7 months ago

elong0527 commented 1 year ago

I had an impression there is a way to control default output while printing data frame. That can potentially remove the head function to enhance readability.

nanxstats commented 1 year ago

Modified from customize the printing of objects in chunks in R Markdown Cookbook:

knit_print.data.frame <- function(x, ...) {
  paste(capture.output(base::print.data.frame(head(x, 5))), collapse = "\n")
}

registerS3method(
  "knit_print", "data.frame", knit_print.data.frame,
  envir = asNamespace("knitr")
)

You can add the above to _common.R to customize the printing method. _common.R is sourced at the beginning of most chapters. Remember to remove the existing head() calls.