I want to create a DT data table a barplot and a leaflet map based on a reactive data table where the user can filter the content they are interested in. The idea is that the users choose the levels of several categorical variables that will be displayed in a data table, plot, and map. However, I always get an error that the object "input" is not found. How can I create a reactive data frame that I can display in a DT data table, plot, and map?
I tried creating a reactive function which allows filtering using "subset" or "filter" but I get the same error message as specified above. I looked for similar cases on StackOverflow and google which led me to try to filter the content without defining the reactive function as well as moving the filtered fil to server.R in case it cannot access the content from global R. None of it worked.
global.R
load("sktrad.Rdata")
options ( encoding = "UTF-8" )
data_fil <- reactive({
data <- subset(
sktradjkp,
Stamomkret >= input$stamk[1] & Stamomkret <= input$stamk[2],
Kommun == input$KomS,
Tradslag == input$TraS,
Tradstatus == input$TraSt
)
})
ui.R
sidebar = dashboardSidebar(
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Information",
tabName = "info"
),
sliderInput(inputId = "stamk",
label = "Trädiameter inkluderat",
min = min(sktradjkp$Stamomkret),
max = max(sktradjkp$Stamomkret),
value = c(min(sktradjkp$Stamomkret), max(sktradjkp$Stamomkret)),
sep = "",
step = 10,
post = " cm "
),
selectizeInput(inputId = "KomS",
label = "Välj en eller flera kommuner",
choices = unique(sktradjkp$Kommun),
multiple = T,
selected = "Jönköping"
),
selectizeInput(inputId = "TraS",
label = "Välj en eller flera Trädslag",
choices = unique(sktradjkp$Tradslag),
multiple = T,
selected = "Ek"
),
selectizeInput(inputId = "TraSt",
label = "Välj en eller flera trädstatus",
choices = unique(sktradjkp$Tradstatus),
multiple = T,
selected = "Friskt"
)
)
)
)
server.R
output$DataSk <- DT::renderDataTable(DT::datatable(data_fil(), rownames = FALSE,
options=list(scrollX=TRUE)
)
)
output$BarPlot <- renderPlotly({
ggplotly(
ggplot(data_fil(), aes_string(x=input$StrToPlot, y="Stamomkret")) +
stat_summary(fun.y="mean", geom="bar") + theme(axis.text.x = element_text(angle = 45, hjust = 1,size=12))
)
})
output$coolplot <- renderPlot({
ggplot(data_fil(), aes_string("Stamomkret")) +
geom_histogram()
})
I expected to get a reactive function/data frame which reacts on user input and displays the results in the DT data table, plot and later in the leaflet map.