Dear all,
I'm trying to create an app that uses heirarchical dropdown menus to select infput for ggplot.
The error lies in the "best_sellers" tab where the user first selects a category to filter by and then and sub category . I then pass these variables to ggplot and am getting errors there. I think I"m doing something wrong with how I"m using uiOutput or reactive() or how I"m passing them to ggplot. I have pasted the non-shiny app version at the bottom.
I have looked at a few posts related to this and feel like i'm on the right path. If someone could take a look and let me know where the error is, that'd be great.
ui <- fluidPage(
# Make a title to display in the app
titlePanel(" Interactive Analysis of Product Sales"),
# Make the Sidebar layout
sidebarLayout(
# Put in the sidebar all the input functions
sidebarPanel(
tabsetPanel(id="tabs",
tabPanel("time",br(),
p("On this tab you can choose to look at trends across the norgen product categories over time\n
first choose your time period of interest and then you can get more data by clicking on the table"),
dateRangeInput("daterange_t", h3("Date range:"),start="2018-01-01"),
radioButtons("radio", h3("Choose Analysis Type"),
choices = list("By Category" = 1, "By Product Name" = 2),selected = 1)
#uiOutput("otu"), br(),
# Add comment
),
tabPanel("best_sellers", br(),
p("On this tab you can look at best selling products over a certain time period"),br(),
dateRangeInput("daterange_b", "Date range:",start="2018-01-01") ,
selectInput("grouping1","group_by",names(df_m)[c(2,4,6,7,8,9,10,11)],selected=1),
# first select the category to filter by ()
selectInput("filter_cat","filter_by",choices=names(df_m)[c(2,4,6,7,8,9,10,11)],selected=1),
# now select a specific sub_category
uiOutput("filter_cat_choice"),
sliderInput("top_numb", "Number of Products:",
min = 1, max = 25, value = 10,step=1)
),
tabPanel("comparison",br(),
dateRangeInput("daterange_c", "Date range:",start="2018-01-01")
#selectInput('datasetD', 'dataset', names(abundance_tables),selected=names(abundance_tables)[1]),
)
),
width=2),
# Put in the main panel of the layout the output functions
mainPanel(
conditionalPanel(condition="input.tabs == 'time'",
plotOutput("time_plot", click = "time_plot_click"),
h2("data"),
tableOutput("time_data_table")
),
conditionalPanel(condition="input.tabs == 'best_sellers'",
fluidRow(plotOutput('best_tree_plot',height="800px")),
tableOutput("best_data_table")
),
conditionalPanel(condition="input.tabs == 'comparison'",
#plotOutput('plot2')
br(),
h2("comparison between categories")
),
width=10)# end mainPanel
)# end SidebarLayout
) # end fluidPage
Here is the server.
server <- function(input, output,session){
## for time series panel
timedat<-reactive({
df_m %>%
filter(date > input$daterange_t[1] & date < input$daterange_t[2] ) %>%
mutate(Quarter=paste(year(date),quarter(date),sep="_"))%>%
group_by(Area,Main_Category,Quarter) %>%
summarise(sum_products=sum(Products_sold))
})
#plot
output$time_plot <- renderPlot({
dat<-timedat()
font_size=14
p1<-dat %>%
filter(Area==unique(dat$Area)[1])%>%
ggplot(aes(x=Quarter,y=sum_products,color=Main_Category)) +
geom_line(size=1.5) +
geom_point(size=3)+
ggtitle(unique(dat$Area)[1])+
xlab(NULL)+
theme_bw() +
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))
p2<-dat %>%
filter(Area==unique(dat$Area)[2])%>%
ggplot(aes(x=Quarter,y=sum_products,color=Main_Category)) +
geom_line(size=1.5) +
geom_point(size=3)+
ggtitle(unique(dat$Area)[2])+
xlab(NULL)+
theme_bw() +
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))
p3<-dat %>%
filter(Area==unique(dat$Area)[3])%>%
ggplot(aes(x=Quarter,y=sum_products,color=Main_Category)) +
geom_line(size=1.5) +
geom_point(size=3)+
ggtitle(unique(dat$Area)[3])+
xlab(NULL)+
theme_bw() +
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))
p4<-dat %>%
filter(Area==unique(dat$Area)[4])%>%
ggplot(aes(x=Quarter,y=sum_products,color=Main_Category)) +
geom_line(size=1.5) +
geom_point(size=3)+
ggtitle(unique(dat$Area)[4])+
xlab(NULL)+
theme_bw() +
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))
ggarrange(p1,p2,p3,p4,nrow=2,ncol=2)
}, res = 96)
output$plot_clickinfo <- renderPrint({
cat("Click:\n")
str(nearPoints(timedat(), input$time_plot_click))
str(input$time_plot_click)
})
output$time_data_table <- renderTable({
req(input$plot_click)
browser()
nearPoints(timedat(), input$time_plot_click, xvar = "Quarter", yvar = "sum_products")
})
## for best seller analysis
grouping1<-reactive({
input$grouping1
})
# UI to show category choices # Return the requested dataset ----
#filter_cat<-reactive({unique(select(df_m,input$filter_cat))})
##
output$filter_cat_choice <- renderUI({
selectInput(inputId = "filter_cat", label = "filter_cat_choice",
choices = unique(select(df_m,input$filter_cat)))
})
input_filterCat<-reactive({input$filter_cat})
input_filterCatChoice<-reactive({input$filter_cat_choice})
bestdat<-reactive({
df_m %>%
filter(date > input$daterange_b[1] & date < input$daterange_b[2] ) %>%
filter(.data[[input_filterCat()]]==input_filterCatChoice()) %>%
group_by(.data[[input$grouping1]]) %>%
summarise(sum_products=sum(Products_sold)) %>%
slice_max(sum_products,n=input$top_numb)
})
#
output$best_data_table<- renderTable({bestdat()})
# treemap or barplot
output$best_tree_plot<-renderPlot({
dat<-bestdat()
dat %>%
ggplot(aes(x=.data[[input$grouping1]],y=sum_products,fill=.data[[input$grouping1]]))+
geom_bar(stat="identity") +
geom_text(aes(label = sum_products), vjust = -0.2, colour = "black")+
ggtitle(paste("top products sold between ",start_date, "and", end_date)) +
xlab(NULL)+
theme_bw() +
guides(fill="none")+
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))
},res=96)
#
}
Here is the error message
Warning: Error in filter: ℹ In argument: `.data[["Name"]] == input_filterCatChoice()`.
Caused by error:
! `..1` must be of size 10860 or 1, not size 0.
Here is the code that works in a Non-ShinyApp version:
grouping1<-"Name"
filter_cat<-"Area"
filter_cat_choice<-"Isolation_and_Purification"
best_dat<- df_m %>%
filter(date > start_date & date < end_date ) %>%
filter(.data[[filter_cat]]==filter_cat_choice) %>%
group_by(.data[[grouping1]]) %>%
summarise(sum_products=sum(Products_sold)) %>%
slice_max(sum_products,n=10)
#
best_dat %>%
ggplot(aes(x=.data[[grouping1]],y=sum_products,fill=.data[[grouping1]]))+
geom_bar(stat="identity") +
geom_text(aes(label = sum_products), vjust = -0.2, colour = "black")+
ggtitle(paste("top 10 products sold between ",start_date, "and", end_date)) +
xlab(NULL)+
theme_bw() +
guides(fill="none")+
theme(text=element_text(size = font_size),axis.text.x = element_text(angle=45, hjust=1,size=14))