Home > Enterprise >  Shiny Error: replacement has 2 rows, data has 1
Shiny Error: replacement has 2 rows, data has 1

Time:05-14

I am trying to create a Shiny app, in which users can select an "option" in the drop-down box(created using selectInput) and Shiny will display the data table accordingly. Now, I have the parts where users can click on the link per each observation and it will directly link to the website figure out, but I can't seem to make it works when users select an option other than "All". Basically, when I select the option "AZ" or "NC", it incurred this error "replacement has 2 rows, data has 1." Please see the pictures, you will probably know what I mean. Data table with "All" option selected, no error Data table when "AZ" is selected, incurred error

Thank you for your help!

Here is the reproducible code.

# load packages
library(shiny)

# Create a sample df
names <- c("Mister Car Wash", "Driven Brands Car Wash")
city <- c("Tucson", "Charlotte")
state <- c("AZ", "NC")
locations <- c("350 locations", "300 locations")
website <- c("www.mistercarwash.com", "www.drivenbrandscarwash.com")
car_df <- data.frame(names, city, state, locations, website)

# # create the ui

ui <- fluidPage(
  titlePanel("Top 99 Car Wash Chains in the U.S."),
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId ="statenames",
        label = "Select the State:",
        choices = c("All", "AZ", "NC"),
        selected = "All") # default value
    ),
    mainPanel(
      width=9,
      DT::dataTableOutput('table')
    )
  )
)

# create the server
server <- function(input, output) {
  output$table <- renderDT({
      data <- car_df
      if (input$statenames != "All") {
        data <- data[car_df$state == input$statenames,]
      } else{ 
        (input$statenames == car_df$State)
        
      }
      #add html link tags
      data$Website <- paste0("<a href='//", car_df$website,"'>",car_df$website,"</a>")
      
      datatable(data, extensions = 'Buttons', rownames = FALSE, escape = FALSE, selection = 'none',
                colnames = c('names', 'city', 'state', 'locations','website'),
                options = list(buttons = c("copy", "csv", "excel"), paging = FALSE, dom = 'Bfrtip')
      )
    
    })
      

    
}
  

# Call shinyapp
shinyApp(ui=ui, server=server)

CodePudding user response:

The culprit is your statement

data$Website <- paste0("<a href='//", car_df$website,"'>",car_df$website,"</a>")

As you know when you select a state, data has only one record. However, car_df has 2 records.

Try this

output$table <- renderDT({
    req(input$statenames)
    data <- car_df
    if (input$statenames != "All") {
      data <- data[car_df$state == input$statenames,]
      data$Website <- paste0("<a href='//", data$website,"'>",data$website,"</a>")
    } else{ 
      #(input$statenames == car_df$State)
      data$Website <- paste0("<a href='//", car_df$website,"'>",car_df$website,"</a>")
    }
    #add html link tags
    #data$Website <- paste0("<a href='//", car_df$website,"'>",car_df$website,"</a>")
    
    
    datatable(data, extensions = 'Buttons', rownames = FALSE, escape = FALSE, selection = 'none',
              colnames = c('names', 'city', 'state', 'locations','website'),
              options = list(buttons = c("copy", "csv", "excel"), paging = FALSE, dom = 'Bfrtip')
    )
    
  })
  • Related