I need help with subsetting data in shiny app

I'm creating a simple shiny app using the {golem} package. Below is the code I've used for my first module:

@importFrom shiny NS tagList

mod_plot_module_ui <- function(id){
  ns <- NS(id)
  tagList(
    sidebarLayout(
      sidebarPanel(
        sliderInput(inputId = "life", label = "Life expectancy",
                    min = 0, max = 120,
                    value = c(30, 50)),
        selectInput("continent", "Continent",
                    choices = c("All", levels(gapminder$continent))),
        downloadButton("download_data")
        
      ),
      
      mainPanel(
        tabsetPanel(
          tabPanel("Plot", plotOutput(ns("plot"))),
          tabPanel("Table", DT::renderDataTable(ns("table")))
        )
      )
    )
 
  )
}

plot_module Server Function

@noRd

mod_plot_module_server <- function(input, output, session){
  ns <- session$ns
  
  filtered_data <- reactive({
    data <- gapminder
    data <- data[data$lifeExp >= input$life[1] & data$lifeExp <= input$life[2]]
    
    if (input$continent != "All") {
      data <- data[continent == input$continent]
    }
    data
  })
  
  output$plot <- renderPlot({
    data <- filtered_data()
    ggplot(data, aes(gdpPercap, lifeExp)) +
      geom_point() +
      scale_x_log10()
  })
 
}

However, when I run the application, I get this error message:
Listening on http://127.0.0.1:3775
Warning: Error in : Must subset columns with a valid subscript vector.
:information_source: Logical subscripts must match the size of the indexed input.
x Input has size 6 but subscript data$lifeExp >= input$life[1] & data$lifeExp <= input$life[2] has size 0.
203:

I don't understand what this error message means. Can someone help with this? Many thanks in advance.

input life doesn't exist to the module server as its id isnt wrapped with ns() in its ui definition

1 Like

When I wrap every inputId and outputId in ns() I still get the same warning message. The message comes up whenever I change values in the sliderInput.

the logical subscripts error is not shiny related, its a pure data issue.
Here is a reprex of the problem, and the solution (using tidyverse/dplyr)

library(tidyverse)
library(gapminder)

input <- list()
input$life <- c(38,45)

data <- gapminder
data <- data[data$lifeExp >= input$life[1] & data$lifeExp <= input$life[2]]
# error: Must subset columns with a valid subscript vector.
# i Logical subscripts must match the size of the indexed input.
# x Input has size 6 but subscript `data$lifeExp >= input$life[1] & data$lifeExp <= input$life[2]` has size 1704.

dplyr::filter(data,
              lifeExp >= input$life[1] ,
              lifeExp <= input$life[2])

Thank you very much. I appreciate the help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.