Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting export to gt in tablespan #26

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions R/tablespan.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' tablespan
#'
#' Create table tops (headers) using a formula approach.
#' Create complex table spanners with a simple formula.
#'
#' \code{tablespan} provides a formula based approach to adding headers and spanners
#' to an existing data.frame. The objective is to provide an easy to use, but good
Expand Down Expand Up @@ -39,7 +39,7 @@
#' You can also nest spanners (e.g., \code{Species ~ (Sepal = (Length = Sepal.Length) + (Width = Sepal.Width))}.
#'
#' When exporting tables, you may want to rename some of you columns. For example,
#' you may want to rename Sepal.Length and Petal.Length to Lenght and Sepal.Width and
#' you may want to rename Sepal.Length and Petal.Length to Length and Sepal.Width and
#' Petal.Width to Width. With \code{tablespan}, you can rename the item in the header
#' using \code{new_name:old_name}.
#' For example, \code{Species ~ (Sepal = Length:Sepal.Length + Width:Sepal.Width) + (Petal = Length:Sepal.Length + Width:Sepal.Width)}
Expand All @@ -59,6 +59,9 @@
#' |-------:|------:|-------:|------:|
#' | 5.1| 3.5| 1.4| 0.2|}
#'
#' Tables created with tablespan can be exported to Excel (using openxlsx),
#' HTML (using gt), LaTeX (using gt), and RTF (unsing gt).
#'
#' @param data data set
#' @param formula formula to create table
#' @param title string specifying the title of the table
Expand All @@ -67,15 +70,40 @@
#' @returns Object of class Tabletop with title, subtitle, header info, data, and footnote.
#' @export
#' @examples
#' data("iris")
#' tbl <- tablespan(data = iris[iris$Species == "setosa", ],
#' formula = Species ~ (Sepal = Sepal.Length + Sepal.Width) +
#' (Petal = Petal.Length + Petal.Width))
#'
#' # Create Excel table:
#' library(tablespan)
#' library(dplyr)
#' data("mtcars")
#'
#' # We want to report the following table:
#' summarized_table <- mtcars |>
#' group_by(cyl, vs) |>
#' summarise(N = n(),
#' mean_hp = mean(hp),
#' sd_hp = sd(hp),
#' mean_wt = mean(wt),
#' sd_wt = sd(wt))
#'
#' # Create a tablespan:
#' tbl <- tablespan(data = summarized_table,
#' formula = Cylinder:cyl + Engine:vs ~
#' N +
#' (`Horse Power` = Mean:mean_hp + SD:sd_hp) +
#' (`Weight` = Mean:mean_wt + SD:sd_wt),
#' title = "Motor Trend Car Road Tests",
#' subtitle = "A table created with tablespan",
#' footnote = "Data from the infamous mtcars data set.")
#'
#' tbl
#'
#' # Export as Excel table:
#' wb <- to_excel(tbl = tbl)
#'
#' # saveWorkbook(wb, "iris.xlsx")
#' # Save using openxlsx
#' # openxlsx::saveWorkbook(wb, "iris.xlsx")
#'
#' # Export as gt:
#' gt_tbl <- to_gt(tbl = tbl)
#' gt_tbl
tablespan <- function(data,
formula,
title = NULL,
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ wb <- to_excel(tbl = tbl,
#### Formatting Data Types

`tablespan` also allows formatting specific data types. Let's assume that we want
to round all doubles to 3 instead of the default four digits.
to round all doubles to 3 instead of the default 2 digits.
To this end, we use the `create_data_styles` function, where we specify (1) a
function that checks for the data type we want to style (here `is.double`) and
(2) a style for all columns that match that style:
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,10 @@ wb <- to_excel(tbl = tbl,
#### Formatting Data Types

`tablespan` also allows formatting specific data types. Let’s assume
that we want to round all doubles to 3 instead of the default four
digits. To this end, we use the `create_data_styles` function, where we
specify (1) a function that checks for the data type we want to style
(here `is.double`) and (2) a style for all columns that match that
style:
that we want to round all doubles to 3 instead of the default 2 digits.
To this end, we use the `create_data_styles` function, where we specify
(1) a function that checks for the data type we want to style (here
`is.double`) and (2) a style for all columns that match that style:

``` r
double_style <- create_data_styles(double = list(test = is.double,
Expand Down
46 changes: 37 additions & 9 deletions man/tablespan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading