1

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.

  • 1
    At first sight, `data_fil <- reactive({ ... })` belongs inside the `server` function, not in `global.R` – Aurèle Apr 01 '19 at 09:29
  • 2
    The content of `server.R` should be wrapped in `server <- function(input, output, session) { .... }`, and `data_fil <- reactive({ ... })` should be inside that `server` function definition. – Aurèle Apr 01 '19 at 09:32
  • Thanks, Aurèle moving the reactive function inside the server file and function helped at least partially. I got the table and plots to react to the sliderInput. However, I was less fortunate when it comes to categorical variables / selectizeInput. There I got an error message: Warning: Error in: Length of logical index vector for `[` must equal number of columns (or 1): * `.data` has 12 columns * Index vector has length 13 How can I create inputs that are conditional if the user wants to filter for levels of a variable? – Matti Ermold Apr 02 '19 at 19:19
  • Maybe you meant to use `subset` like this instead: `subset( sktradjkp, Stamomkret >= input$stamk[1] & Stamomkret <= input$stamk[2] & Kommun %in% input$KomS & Tradslag %in% input$TraS & Tradstatus %in% input$TraSt )` (separating conditions with `&` rather than `,`, and also replacing `==` with `%in%` since multiple selection is allowed). Also, welcome to StackOverflow! Could you make your example [reproducible](https://stackoverflow.com/questions/5963269) by providing example data? – Aurèle Apr 04 '19 at 09:32
  • Maybe the error comes from the fact that the second condition is interpreted as the `select` argument to `subset`, and your data frame happens to have 13 rows and 12 columns. Safer to name the arguments to `subset`: `subset(x = ...., subset = ...., select = ....)` (though I have to admit I never do it consistently myself). – Aurèle Apr 04 '19 at 09:35
  • Thanks, Aurèle no need to show some reproducible code because your assumption was spot on. I guess that happens when you want to run before you learn to walk i.e. learn to make dashboards instead of learning about data transformation/wrangling first. :) – Matti Ermold Apr 04 '19 at 21:59

0 Answers0