i am trying to plot graphs based on user input from checkboxes. it all works fine until i uncheck the first checkbox and an error pops up saying "no applicable method for 'ggplotly' applied to an object of class "NULL"". Even though other checkbox/es are checked, it gives anerror. for my codde to work, the first checkbox has to be mandatorily always checked. How do i resolve my code such that the graph is plotted based on user input and doesn't depend on the first checkbox only? my sample data has 3 columns, namely "distributor_name", "outlet_type" and "total_sales". it is a csv file and here, i am showing how my data looks like. EDIT- for these 8 rows, i get no errors, when number of rows increase, i get the following error.
library(ggplot2)
mydata <-structure(list(State_Name = c("ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS","ANDAMAN AND NICOBAR ISLANDS"),
District_Name = c("ANDAMANS","ANDAMANS","ANDAMANS","ANDAMANS","ANDAMANS","ANDAMANS","ANDAMANS","ANDAMANS"),
Place_Name= c("PORT BLAIR", "PORT BLAIR", "PORT BLAIR", "PORT BLAIR", "PORT BLAIR", "PORT BLAIR", "PORT BLAIR", "PORT BLAIR"),
Distributor_Name = c("M.A. MOHMAD & SONS(S1145)","M.A.MOHMAD & SONS(S1145)","M.A.MOHMAD & SONS(S1145)","M.A.MOHMAD & SONS(S1145)", "M.A.MOHMAD & SONS(S1145)","M.A.MOHMAD & SONS(S1145)","M.A.MOHMAD & SONS(S1145)","M.A. MOHMAD & SONS(S1145)"),
Product_Code= c("ALHF", "ARFM", "ARTT", "BNEF", "BNPP", "BNSS", "BNTI","COFM"),
Product_Value=c(8839.2, 39777.3, 19092.96, 254577.61, 63640.8, 10608, 28284.8, 21214.57),
Qty =c(80,90,72,720,720,240,320,48),
Tto= c(8662.42, 38981.76, 18711.1, 249486.05, 62367.99, 10395.84, 27719.1, 20790.28)),
.Names = c("State_Name", "District_Name","Place_Name","Distributor_Name","Product_Code","Product_Value","Qty", "Tto"), row.names = c(NA,-8L), class = "data.frame")
print(mydata)
mydata <- head(mydata,n=20)
dput(mydata)
depvar <- mydata$Tto
avail_wise <- setdiff(colnames(mydata), depvar)
avail_wise <- setNames(avail_wise,
paste0(avail_wise, "-wise"))
set.seed(20180307)
# random fill/color assignments
colors <- data.frame(
field = avail_wise,
fill = sample(palette(), length(avail_wise), replace=TRUE),
color = sample(palette(), length(avail_wise), replace=TRUE)
)
str(colors)
# de-magic-constant something later in the code
checkboxes_max_levels <- 10 # an arbitrary number, seems reasonable
ui <- fluidPage(
theme = "bootstrap.css",
titlePanel("Hello User"),
fluidRow(
column(3, wellPanel(
selectInput("input_type", "Input type",
choices = avail_wise, selected = avail_wise[1] )
) ),
column(9, wellPanel( uiOutput("ui") ))
),
fluidRow(
column(12, plotOutput("dynamic_value") )
)
)
Server <- function(input, output) {
output$ui <- renderUI({
req(input$input_type)
choices <- sort(unique(mydata[[input$input_type]]))
if (is.factor(choices) || is.character(choices) || length(choices) < checkboxes_max_levels) {
checkboxGroupInput("dynamic", paste0(input$input_type, "-wise"),
choices = choices, selected = choices[1],
inline = TRUE)
} else {
shiny::sliderInput("dynamic", paste0(input$input_type, "-wise"),
min = min(choices), max = max(choices),
value = round(quantile(choices, c(0.25,0.75)), 1))
}
})
filtered <- reactive({
req(input$dynamic)
col <- filteredcolors()
it <- isolate(input$input_type)
if (is.character(input$dynamic)) {
# checkboxGroupInput
ind <- mydata[[it]] %in% input$dynamic
} else {
# sliderInput
ind <- input$dynamic[1] <= mydata[[it]] & mydata[[it]] <= input$dynamic[2]
}
mydata[ind,,drop=FALSE]
})
filteredcolors <- reactive({
dplyr::filter(colors, field == input$input_type)
})
# mydata.ordered <- mydata[order(mydata[,8]),]
output$dynamic_value <- renderPlot({
req(filtered())
col <- filteredcolors()
ggplot(filtered(), aes_string(depvar)) +
geom_histogram(fill=col$fill, col=col$color)
})
}
shinyApp(ui = ui, server = Server)
]3