0

I am looking at the distribution of multiple invoice types for 5 financial years and want to filter the histogram by the financial year while the plot is separated by invoice type via facet_grid

I have included the mean as a geom_vline to understand the distribution better, and by creating a static graph through ggplot and manually filtering the financial year, the plot is accurate.

The plot acts as intended in the RStudio Viewer, yet the html document output does not show the plot, and the end users do not have access to RStudio to view through the viewer

library(tidyverse)
library(shiny)

**test data**

test <- structure(list(`FY Year` = structure(c(22L, 21L, 20L, 22L, 19L, 
19L, 19L, 18L, 21L, 19L, 21L, 21L, 20L, 18L, 20L, 21L, 19L, 20L, 
18L, 19L, 21L, 20L, 21L, 18L, 20L, 19L, 18L, 18L, 18L, 20L, 19L, 
18L, 21L, 21L, 19L, 19L, 20L, 19L, 18L, 20L, 21L, 20L, 19L, 20L, 
21L, 18L, 21L, 19L, 20L, 21L, 19L, 20L, 18L, 21L, 20L, 21L, 19L, 
20L, 21L, 21L, 20L, 21L, 18L, 18L, 19L, 19L, 20L, 21L, 20L, 18L, 
18L, 21L, 18L, 20L, 21L, 19L, 18L, 18L, 21L, 22L, 18L, 20L, 20L, 
19L, 20L, 20L, 20L, 20L, 19L, 18L, 21L, 20L, 21L, 21L, 21L, 19L, 
19L, 20L, 20L, 18L, 19L, 20L, 22L, 20L, 18L, 21L, 18L, 21L, 20L, 
19L, 19L, 20L, 19L, 21L, 20L, 21L, 20L, 20L, 18L, 20L, 21L, 18L, 
20L, 22L, 20L, 22L, 19L, 18L, 21L, 20L, 21L, 21L, 21L, 21L, 20L, 
19L, 21L, 19L, 21L, 19L, 21L, 21L, 21L, 19L, 21L, 21L, 22L, 18L, 
21L, 18L, 19L, 19L, 19L, 21L, 22L, 21L, 18L, 22L, 18L, 20L, 21L, 
18L, 20L, 20L, 21L, 18L, 20L, 19L, 21L, 22L, 20L, 19L, 19L, 18L, 
20L, 20L, 22L, 18L, 22L, 21L, 21L, 19L, 21L, 21L, 19L, 21L, 21L, 
21L, 21L, 22L, 20L, 18L, 18L, 21L, 18L, 21L, 22L, 18L, 21L, 21L
), levels = c("1900", "2003", "2004", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", 
"2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"
), class = "factor"), number_of_days = c(2, 3, 0, 3, 21, 14, 
0, 0, 5, 5, 13, 19, 3, 14, 3, 6, 0, 32, 0, 4, 2, 13, 1, 4, 11, 
12, 22, 4, 1, 16, -1, 12, 7, 5, 14, 0, 0, 4, 0, 7, 4, 0, 8, 0, 
6, 1, 29, 35, 3, 0, -7, 0, 1, 0, 19, 13, 5, 6, -1, 1, 14, 25, 
0, 7, 3, 2, 1, 10, 6, 5, 0, 38, 25, 3, 3, -1, 18, 7, 13, 10, 
26, 7, 0, 6, 4, 7, 7, 3, 0, 0, 7, 5, 0, 0, -2, 0, 3, 0, 33, 9, 
36, 12, 4, 0, 3, 35, 20, 7, 0, 4, 6, 1, 5, 7, 10, 2, 16, 37, 
0, 0, 12, 43, 3, 4, 14, 0, 3, 7, 1, 0, 5, 4, 4, 3, 11, 5, 0, 
10, 4, 21, 9, 17, 19, 0, 3, 13, 4, 35, 6, 7, 18, 1, 1, 14, 3, 
7, 10, 1, 14, 1, -1, -1, 6, -15, 3, 10, 24, 1, 3, -1, 33, 6, 
11, 40, 11, 10, 3, 4, 12, 13, 8, 8, -2, 4, 7, 0, 25, 35, 20, 
3, 4, 0, 6, 11, 0, 38, 7, 10, 6, 21), Invoice_type_name = c("Other", 
"Footings", "Final", "Contract Signing", "Final", "Footings", 
"Contract Signing", "Contract Signing", "Roofing", "Framing", 
"Roofing", "Footings", "Framing", "Framing", "Initial Work Deposit", 
"2nd Fix Carpentry", "Final", "2nd Fix Carpentry", "Final", "Framing", 
"Contract Signing", "Framing", "Contract Signing", "Linings", 
"Roofing", "Framing", "Other", "Footings", "Initial Work Deposit", 
"Final", "Contract Signing", "Framing", "Linings", "Framing", 
"Framing", "Contract Signing", "Initial Work Deposit", "Footings", 
"Initial Work Deposit", "Linings", "Contract Signing", "Initial Work Deposit", 
"Initial Work Deposit", "Contract Signing", "Framing", "Contract Signing", 
"Other", "Framing", "Roofing", "Contract Signing", "Initial Work Deposit", 
"Linings", "Contract Signing", "Framing", "Framing", "Roofing", 
"Linings", "Linings", "Contract Signing", "Initial Work Deposit", 
"Footings", "2nd Fix Carpentry", "2nd Fix Carpentry", "Initial Work Deposit", 
"Footings", "Contract Signing", "Final", "Framing", "Initial Work Deposit", 
"Initial Work Deposit", "Contract Signing", "Footings", "Footings", 
"2nd Fix Carpentry", "2nd Fix Carpentry", "Initial Work Deposit", 
"Contract Signing", "Linings", "Footings", "Roofing", "Roofing", 
"Footings", "Initial Work Deposit", "Roofing", "Framing", "Framing", 
"Contract Signing", "Footings", "Initial Work Deposit", "Initial Work Deposit", 
"Contract Signing", "Linings", "Initial Work Deposit", "Contract Signing", 
"Contract Signing", "Initial Work Deposit", "Initial Work Deposit", 
"Initial Work Deposit", "Linings", "Final", "Final", "2nd Fix Carpentry", 
"Initial Work Deposit", "Initial Work Deposit", "Linings", "2nd Fix Carpentry", 
"Linings", "Footings", "Initial Work Deposit", "Linings", "Linings", 
"Initial Work Deposit", "Linings", "Framing", "Framing", "2nd Fix Carpentry", 
"Framing", "Final", "Initial Work Deposit", "Initial Work Deposit", 
"2nd Fix Carpentry", "Final", "Initial Work Deposit", "Contract Signing", 
"2nd Fix Carpentry", "Initial Work Deposit", "Initial Work Deposit", 
"Footings", "Initial Work Deposit", "Contract Signing", "Roofing", 
"Framing", "Footings", "Initial Work Deposit", "Framing", "Footings", 
"Initial Work Deposit", "Footings", "Roofing", "Framing", "Roofing", 
"Linings", "Linings", "Contract Signing", "Framing", "Initial Work Deposit", 
"Footings", "Final", "Contract Signing", "Footings", "Footings", 
"Initial Work Deposit", "Initial Work Deposit", "Linings", "Roofing", 
"Framing", "Linings", "Initial Work Deposit", "Framing", "Final", 
"Initial Work Deposit", "Contract Signing", "2nd Fix Carpentry", 
"Contract Signing", "Footings", "Linings", "Roofing", "Initial Work Deposit", 
"2nd Fix Carpentry", "Contract Signing", "Final", "Linings", 
"Linings", "Linings", "Roofing", "Final", "Framing", "Final", 
"Linings", "Footings", "Final", "Contract Signing", "Initial Work Deposit", 
"Linings", "Initial Work Deposit", NA, "Roofing", "Linings", 
"Footings", "Footings", "2nd Fix Carpentry", "Initial Work Deposit", 
"Framing", "Footings", "Contract Signing", "Roofing", "Linings", 
"Linings", "Linings", "Footings")), row.names = c(NA, -200L), class = "data.frame")


stat <- test %>%
  group_by(Invoice_type_name, `FY Year`) %>%
  summarise(
    mean = mean(number_of_days)
  )

selectInput("financialYear", label = "Financial Year", selected = "2019",
            choices = c("2019", "2020", "2021", "2022", "2023"))
  
renderPlot({
  test %>%
    filter(`FY Year` == input$financialYear) %>%
    ggplot(aes(x = `number_of_days`)) +
    geom_histogram() +
       
    geom_vline(data = (stat %>%
                 filter(`FY Year` == input$financialYear)),
               aes(xintercept = mean),
               linewidth = 1) +
    facet_grid(rows = vars(Invoice_type_name), scales = "free")},

  outputArgs = list(width = "100%", height = "1000px")
  )

Manually entered the financial year filter

Shiny interactive plot, selecting the filtered year

HTML output after rendering

  • Welcome to SO! Based on the information you provide it's hard to help you as we don't have your data and can't run your code. Have a look at [How to create a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to fix that. – stefan Dec 15 '22 at 08:21

0 Answers0