Kable won't render to pdf within a function calling multiple CSV files, but pie chart does

19 Views Asked by At

I'm trying to write a function to take all the CSV files in one local folder, manipulate the data to simplify/summarize for a lay audience, and print a table and a pie chart in one PDF for each CSV file. The pie chart prints fine, but the table doesn't even render. I'm new at this, so I'm sure my code is a hot mess, but I can't for the life of me figure out why it's not working. No errors get thrown either

This is what i tried, expecting a table and a chart, but all I get is the chart.

---
title: "Table and Plot Using CSV"
author: "ME"
date: "2024-02-26"
output: pdf_document
toc: true   
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(knitr.table.format = 'latex')

```

```{r, echo = FALSE, include=FALSE} 
#tinytex::install_tinytex()
```

```{r, echo = FALSE, include=FALSE}  

library(tidyverse)
library(ggplot2)
library(ggrepel)
library(data.table)
library(knitr)
library(kableExtra)
library(gridExtra)

```


```{r, echo = FALSE, results = 'asis'}   
csv_directory <- "C:/Users/ME/OneDrive - University/Documents/Budget Statements/February24"
csv_files <- list.files(path = csv_directory, pattern = "\\.csv$", full.names = TRUE)


setwd(csv_directory)

generate_pdf <- function(csv_file) { 
  BA <- fread(csv_file)
  BA <- BA %>%
    dplyr::rename('Balance' = '(Un)Favorable Balance')

  #Create Remaining Balance and filter out non-zero accounts
  BApie <- BA %>%
    select(Account, Balance)
  remainingBalance <- sum(BApie$Balance)
  newRow <- data.frame(Account = 'Remaining Balance', Balance = remainingBalance)
  colnames(newRow) <- colnames(BApie)
  BApie2 <- rbind(BApie, newRow)
  BA3 <- BApie2 %>%
    filter(Balance != 0) %>%
    filter(Account != '79999 Budget Carry Forward*')
  
  pdf_file <- sub("\\.csv$", ".pdf", basename(csv_file))
  pdf(paste0(pdf_file), width = 8.5, height = 11)
  
  
  # Create and print table
 
table <- BA3 %>%
  kable(format = 'latex', caption = 'Account Balances') %>% 
row_spec(nrow(BA3), background = 'yellow')

print(table)



  # Calculate label positions
  BA4 <- BA3 %>%
    filter(Balance < 0)
  BA4 <- rbind(BA4, newRow)
  BA4$label_pos <- ifelse(BA4$Balance >= 0, 1, -1)
  
  # Create pie chart
 
  pie_chart <- ggplot(BA4, aes(x = ' ', y = abs(Balance), fill = Account)) +
    geom_bar(stat = 'identity', width = 1, color = 'white') +
    coord_polar('y', start = 0) +
    geom_text_repel(aes(label = sprintf('%0.2f', abs(Balance)),
                       hjust = ifelse(Balance == min(Balance), 1.5, BA4$label_pos)),
                    position = position_stack(vjust = 0.5),
                    box.padding = 0.75,
                    segment.size = 0.4,
                    direction = "x",
                    show.legend = FALSE) + 
    labs(title = 'Spending Summary Visualization',
         fill = 'Account',
         x = ' ',
         y = 'Dollar Amount') +
    theme_minimal()
  


# Print the pie chart
  print(pie_chart)

  
  dev.off()
}



invisible(lapply(csv_files, generate_pdf))

```
0

There are 0 best solutions below