# Function to summarize & reshape data summarizer <- function(dat, location, role, self=TRUE){ if(self==TRUE){ filter.curr<- dat |> filter(Location %in% location) |> filter(Role %in% role) |> summarize(year.1=sum(Self.Purchase1), year.2=sum(Self.Purchase2), year.3=sum(Self.Purchase3) ) |> pivot_longer(c(year.1,year.2,year.3)) |> rename(Year=name, Revenue=value) } if(self==FALSE){ filter.curr<- dat |> filter(Location %in% location) |> filter(Role %in% role) |> summarize( year.1=sum(Other.Purchase1), year.2=sum(Other.Purchase2), year.3=sum(Other.Purchase3) ) |> pivot_longer(c(year.1,year.2,year.3)) |> rename(Year=name, Revenue=value) } return(filter.curr) } # Snapshot snapshot<- function(dat, location, role, item="self"){ if(item=="self"){ filter.curr<- dat |> filter(Location %in% location) |> filter(Role %in% role) |> summarise(current.year=sum(Self.Purchase3), last.year=sum(Self.Purchase2)) } if(item=="other"){ filter.curr<- dat |> filter(Location %in% location) |> filter(Role %in% role) |> summarise(current.year=sum(Other.Purchase3), last.year=sum(Other.Purchase2)) } if(item=="total"){ filter.curr<- dat |> filter(Location %in% location) |> filter(Role %in% role) |> summarise(current.year=sum(Other.Purchase3)+sum(Self.Purchase3), last.year=sum(Other.Purchase2)+sum(Self.Purchase2)) } return(filter.curr) }