#' display tibble conditional on output format
#'
#' utility function that displays a dataframe as
#' pandoc.table or datatable
#'
#' @param df tibble to be printed
#' @param caption caption for table
#' @param output char: pdf/default
#' @param page_length nr. of rows to display
#' @param col_select_enable logical: enable col-select-button
#' @param colnames colnames to be displayed
#' @param longtable logical defaults to TRUE
#' @param align alignment of columns
#' @param latex_options char-vector: passed to kableStyling
#' @param full_width table spans full width if true
#' @param modify_these_columns char-vector of col-names to be modified
#' @param column_widths width of the columns [cm] in pdf output
#'
#' @return markup for pandoc
#'
#' @export print_df
#'
#' @examples
#' library(tidyverse)
#' datasets::mtcars %>% print_df(caption = "mtcars", output = "pdf")
print_df <- function(
df, #dataframe to be printed
output = "default", # output to pdf or not
caption = NULL,
colnames,
# options for datatable (html and notebook)
page_length = 10,
col_select_enable = FALSE,
# options for kable (pdf)
longtable = TRUE,
align = NULL,
# options for kableExtra::kable_styling
latex_options = c("striped", "hold_position", "repeat_header"),
full_width = TRUE,
# options for kableExtra::column_spec
modify_these_columns,
column_widths
){
Check <- ArgumentCheck::newArgCheck()
if(missing(df)){
ArgumentCheck::addMessage(
msg = "df must not be missing",
argcheck = Check
)
}
if(any(missing(colnames))){
colnames <- df %>% names
}else{
if(length(colnames) != length(names(df))){
ArgumentCheck::addError(
msg = "Nr. of columns in df an nr. of supplied columnnames must be equal",
argcheck = Check
)
}
}
ArgumentCheck::finishArgCheck(Check)
if(output == "pdf"){
print_df <-
df %>%
knitr::kable(
col.names = colnames,
format = "latex",
booktabs = TRUE,
longtable = longtable,
caption = caption,
align = align,
escape = FALSE
) %>%
kableExtra::kable_styling(
full_width = full_width,
latex_options = latex_options
)
if(any(!missing(modify_these_columns))){
print_df <-
print_df %>%
kableExtra::column_spec(
column = which(colnames %in% c(modify_these_columns)),
width = column_widths
)
}
print_df
}
else{# output to notebook or html: make data-table interactive
if(col_select_enable){
df %>%
DT::datatable(
filter = "top", rownames = FALSE,
extensions = c('ColReorder', 'Buttons'),
options = list(pageLength = page_length, colReorder = TRUE, dom = 'Bfrtip', buttons = I('colvis')),
caption = caption,
colnames = colnames
)
}else{
df %>%
DT::datatable(
filter = "top", rownames = FALSE,
extensions = c('ColReorder'),
options = list(pageLength = page_length, colReorder = TRUE),
caption = caption,
colnames = colnames
)
}
}
}
#' returns string conditional on output format
#'
#' @param str string to be returned if output == "pdf"
#' @param output string specifing wheter output is pdf or default (html, nootbook...)
#'
#' @return string or Null
#' @export pdf_str
#'
pdf_str <- function(str, output = "pdf"){
if(output == "pdf"){
str
}else{
NULL
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.