knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(flexpivot)
library(magrittr)

Create contingency tables ready to be used in shiny and rmarkdown, and to be exported to Microsoft Office products via package officer.

Usage

Get count and percentage with one variable:

nobel_laureates %>%
  pivot_table("category") %>%
  pivot_format()

Add a second variable as row or col :

nobel_laureates %>%
  pivot_table("category", "gender") %>%
  pivot_format()
nobel_laureates %>%
  pivot_table(c("category", "gender"), total = FALSE) %>%
  pivot_format()

Customize

Change color and labels :

nobel_laureates %>%
  pivot_table("category") %>%
  pivot_format(
    background = "#D8DEE9",
    color = "#3B4252",
    labels = pivot_labels(
      n = "Count",
      p = "Percentage",
      rows = "Nobel category"
    )
  )

Export

Export pivot table to Word, PowerPoint or Excel:

pt <- pivot_table(
  data = nobel_laureates,
  "category", "gender", 
  total = FALSE,
  stats = c("n", "p")
)

# PowerPoint
export_pptx(pt, "my-presentation.pptx")

# Word
export_docx(pt, "my-document.docx")

# Excel
export_xlsx(pt, "my-workbook.xlsx")

Shiny

Use in shiny with pivotOutput() and renderPivot(), a menu is automatically added to export the pivot table (can be disabled or allow specific export to):

library(shiny)
library(flexpivot)
data("nobel_laureates")

ui <- fluidPage(
  tags$h2("Pivot table in Shiny"),
  pivotOutput("pivot")
)

server <- function(input, output, session) {

  output$pivot <- renderPivot({
    pivot_table(nobel_laureates, "category", "birth_continent", stats = c("n", "p"))
  }, background = "#A3BE8C")

}

shinyApp(ui, server)



dreamRs/flexpivot documentation built on Oct. 26, 2023, 9:46 a.m.