rshiny

Error when trying to filter my dataset based on a vector of strings


As you can see in my app below I do the filtering but I get Warning: Error in order: argument 1 is not a vector when trying to filter my data with widget.

library(shiny)
library(shinydashboard)
library(dplyr)
library(plotly)
library(DT)
library(zoo)
library(shinyWidgets)

# Sample data
sample_data <- tibble::tibble(
  keyword = c("closing costs", "competitive commission", "curb appeal", "expert negotiation", 
              "home inspection", "market analysis", "mortgage pre-approval", "open house", 
              "price reduction", "staging tips"),
  gmb_id = rep("43763", 10),
  date = seq.Date(from = as.Date("2025-03-10"), by = "1 day", length.out = 10),
  pin_count = rep(39, 10),
  gmb_return = runif(10, -0.05, 0.05),
  gmb_abs_return = abs(runif(10, -0.05, 0.05))
)

ui <- dashboardPage(
  dashboardHeader(title = "🔧 Toy Pin Volatility"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Pin Volatility per Keyword", tabName = "pin_tab", icon = icon("map-pin")),
      pickerInput(
        inputId = "selected_keywords_pins",
        label = "Select Keyword(s)",
        choices = unique(sample_data$keyword),
        selected = unique(sample_data$keyword)[1:3],
        multiple = TRUE,
        options = list(`actions-box` = TRUE)
      )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        tabName = "pin_tab",
        
        fluidRow(
          box(width = 12, DTOutput("volatility_table_pins"))
        )
      )
    )
  )
)

server <- function(input, output, session) {
  rolling_pins <- reactive({
    req(input$selected_keywords_pins)
    
    sample_data %>%
      filter(trimws(keyword) %in% trimws(input$selected_keywords_pins)) %>%
      arrange(keyword, gmb_id, date) %>%
      group_by(keyword, gmb_id) %>%
      mutate(
        rolling_volatility = zoo::rollapply(gmb_abs_return,
                                            width = 3,
                                            FUN = sd,
                                            fill = NA,
                                            align = "right")
      ) %>%
      ungroup() %>%
      na.omit()
  })
  
  
  output$volatility_table_pins <- renderDT({
    datatable(
      rolling_pins(),
      options = list(pageLength = 10, scrollX = TRUE),
      rownames = FALSE
    )
  })
}

shinyApp(ui, server)

Solution

  • At least with the sample data, you are getting that error because there are no matching rows in your data frame when you are trying to plot a line. You can set the same error with

    p <- ggplot(sample_data[0,], aes(x = date, y = gmb_abs_return, color = keyword)) +
       geom_line()
    ggplotly(p)
    

    But if you have at least some data, you will not get the same error.

    p <- ggplot(sample_data, aes(x = date, y = gmb_abs_return, color = keyword)) +
       geom_line()
    ggplotly(p)
    

    Your sample data doesn't have more than one value for any keyword so you can't really draw a line.