## * DONE save table
#' Save a table in various formats
#'
#' This function saves a given table in CSV, Excel, and LaTeX formats to a specified directory with a given filename.
#'
#' @param tab A data frame or matrix to be saved.
#' @param tabl A character string with the LaTeX code for the table.
#' @param path A character string specifying the directory path where the files will be saved.
#' @param fn A character string specifying the base filename (with or without extension) for the output files.
#'
#' @return This function does not return any value. It saves files to the specified path.
#'
#' @examples
#' # Sample data frame
#' tab <- data.frame(Name = c("John", "Jane"), Age = c(30, 25))
#'
#' # Sample LaTeX table representation
#' tabl <- kable(df)
#'
#' # Save the table
#' save.table(tab, tabl, path = "output", fn = "my_table.tex")
#'
#' @export
save.table <- function(tab, tabl, path, fn)
{
fn = file.path(path, glue::glue("{fn}"))
fn = tools:::file_path_sans_ext(fn)
##
cat(glue::glue("\n\nSaving {fn}.csv...") )
write.table(x=tab, file=glue::glue("{fn}.csv") , row.names=F, sep=';')
cat("done!")
##
cat(glue::glue("\n\nSaving {fn}.xlsx...") )
writexl::write_xlsx(x=tab, path=glue::glue("{fn}.xlsx"))
cat("done!")
##
cat(glue::glue("\n\nSaving {fn}.tex...") )
writeLines(text=tabl, glue::glue("{fn}.tex"))
cat("done!\n")
##
## cat(glue::glue("#+CAPTION: {table.caption(tabl)}\n\n"))
}
## table.caption <- function(table)
## {
## ##
## caption = gsub(".*\\\\caption\\{\\label(.*?)\\}.*", "\\1", table)
## return(caption)
## }
## * DONE save figure
#' Save Figure and Associated Table
#'
#' This function saves a figure generated by ggplot2 and its associated data table in multiple formats.
#'
#' @param g A ggplot object to save.
#' @param tab A data frame or table that contains the data used to create the figure.
#' @param caption A text caption for the figure.
#' @param path A directory path where the files will be saved.
#' @param fn The base filename (without extension) for the saved files.
#' @param width Numeric value for the width of the figure. Default is NA.
#' @param height Numeric value for the height of the figure. Default is NA.
#' @param add.to.caption Optional additional text to append to the figure's caption.
#'
#' @return A message indicating the completion of the saving process.
#' @export
save.figure <- function(g, tab, caption, path, fn, width=NA, height=NA,
add.to.caption=NULL)
{
fig = fn
if (!is.null(add.to.caption)) {
caption = glue::glue("{caption} {add.to.caption}")
}
## table
cat("\n ----------------------------------")
cat(glue::glue("\n\nSaving table used to create figure {fig}...") )
fn = file.path(path, glue::glue("{fig}"))
write.table(x=tab, file=glue::glue("{fn}.csv") , row.names=F, sep=';')
writexl::write_xlsx(x=tab, path=glue::glue("{fn}.xlsx"))
cat("done!\n")
##
cat(glue::glue("Saving figure {fig}...") )
fn = c(glue::glue("{fig}.pdf"),
glue::glue("{fig}.png"),
glue::glue("{fig}.jpg"))
fns = file.path(path, fn)
for (fn in fns){ggsave(g, filename=fn, width=width, height=height)}
cat("done!\n")
cat(glue::glue("#+CAPTION: {caption}\n\n"))
cat(glue::glue("#+Name: {fig}\n\n"))
cat(" ----------------------------------\n")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.