Creating a Dashboard with Shiny: Newbie

19 views Asked by At

Goal: The Invoice Trends Dashboard is designed to provide insights into monthly sales trends for various debtors. The dashboard utilizes data from multiple CSV files, including ADTP example data, aging as of data, and debtor concentration data. By visualizing monthly sales trends, users can identify patterns and fluctuations in sales over time for different debtors.

Problems: I have run code variations for 10 hours on Sunday. I have run it through ChatGPT; it says it should work. I downloaded all Shiny packages and tried the code line by line and in different environments. I also ensured all of the CSV columns were consistent without spaces. One error states that a column can't be found, but I double-checked the name and ensured that there were no spaces or additional characters. I have attached my only semi-successful page as a screenshot Screenshot and an example of what I want the graph Example to look like.

My Current Error:

Listening on http://127.0.0.1:7751
Rows: 239 Columns: 10                                                                                                           
── Column specification ─────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (3): Debtor_Name, AR_Balance, Terms
dbl (7): Oldest_Age, Mar_24_ Avg_DTP, Feb-24_Avg_DTP, Jan_24_Avg_DTP, Dec_23_Avg_DTP, Nov...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
New names:                                                                                                                      
• `` -> `...5`
Rows: 151 Columns: 20
── Column specification ─────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (9): Client, Rep, Debtor, Date, Due, Industry, Client_State, Purchase, ADTP
dbl (8): Invoice, Age, 0_30, 31_45, 46_60, 61_90, 90, Balance
lgl (3): ...5, Other_Ref, S_O

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 16 Columns: 8                                                                                                             
── Column specification ─────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Debtor_Name, %
dbl (1): 90
num (5): 0_30, 31_45, 46_60, 61_90, Total

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Warning: Error in geom_text: Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error:
! object 'Feb_24_Avg_DTP' not found
  195: <Anonymous>
  194: signalCondition
  193: signal_abort
  192: rlang::abort
  191: cli::cli_abort
  190: handlers[[1L]]
  189: h
  188: .handleSimpleError
  187: paste0
  186: FUN
  185: lapply
  184: compute_aesthetics
  183: l$compute_aesthetics
  182: f
  175: by_layer
  174: ggplot_build.ggplot
  172: print.ggplot
  167: func
  165: f
  164: Reduce
  155: do
  154: hybrid_chain
  126: drawPlot
  112: <reactive:plotObj>
   96: drawReactive
   83: renderFunc
   82: output$invoicePlot
    1: runApp
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

My Code:

# Load necessary libraries
library(shiny)
library(ggplot2)
library(dplyr)
library(readr)



loadAndProcessData <- function() {
  # Load the data files, adjusting paths as needed
  adtp_example <- readr::read_csv("C:/Dashboard_Examples/ADTP_Example.csv")
  aging_as_of <- readr::read_csv("C:/Dashboard_Examples/aging_as_of_3_7.csv")
  debtor_concentration <- readr::read_csv("C:/Dashboard_Examples/Debtor_concentration_through_2_29.csv")
  
  
  # Standardize column names where necessary
  colnames(aging_as_of)[colnames(aging_as_of) == "Debtor"] <- "Debtor_Name"
  
  # Process aging_as_of to include a Date column in the correct format and a Month column
  aging_as_of <- aging_as_of %>%
    mutate(Date = as.Date(Date, format = "%m/%d/%Y"),
           Month = format(Date, "%b %y")) %>%
    group_by(Debtor_Name, Month) %>%
    summarize(Monthly_Sales = sum(Invoice), .groups = 'drop') %>%
    ungroup()
  
  # Merge data frames
  final_data <- merge(debtor_concentration, adtp_example, by = "Debtor_Name", all.x = TRUE)
  final_data <- merge(final_data, aging_as_of, by = "Debtor_Name", all.x = TRUE)
  
  return(final_data)
}


# Define UI for the Shiny app
ui <- fluidPage(
  titlePanel("Invoice Trends Dashboard"),
  mainPanel(
    plotOutput("invoicePlot")
  )
)

# Define server logic
server <- function(input, output) {
  # Load and process data
  final_data <- loadAndProcessData()
  
  # Output for invoice plot
  output$invoicePlot <- renderPlot({
    ggplot(final_data, aes(x = Debtor_Name, y = Monthly_Sales, fill = Month)) +
      geom_bar(stat = "identity", position = position_dodge()) +
      geom_text(aes(label = paste0("ADTP: ", round(Feb_24_Avg_DTP, 2), " days")),
                vjust = -1.5, position = position_dodge(width = 0.9), color = 'black', size = 3) +
      labs(x = 'Debtor', y = 'Monthly Sales') +
      scale_fill_manual(values = c("Previous" = "#253F4B", "Current" = "#C8DFEA"), name = "Month") +
      theme_minimal() +
      theme(text = element_text(color = '#152238'),
            axis.text.x = element_text(angle = 45, hjust = 1),
            legend.position = "bottom")
  })
}

browser()

# Run the application
shinyApp(ui, server)
0

There are 0 answers