简体   繁体   中英

ggplotly and shiny - Error in order: argument 1 is not a vector

Hello, I am new to coding in R and using a Tidy Tuesday dataset to build a shiny app ( https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-04-07/tdf_winners.csv ).

I am getting an error reading, "Error in order: argument 1 is not a vector" and have narrowed down the error to be with the "nationality" column. Here are the columns of my dataset (I created the year_raw column using lubridate from the start_date column):

[1] "edition" "start_date" "winner_name" "winner_team" "distance" "time_overall"
[7] "time_margin" "stage_wins" "stages_led" "height" "weight" "age"
[13] "born" "died" "full_name" "nickname" "birth_town" "birth_country" 
[19] "nationality" "year_raw"

Here's the sever.R code chunk where I think the error is:

shinyServer(function(input, output) {

output$plot <- renderPlotly({
    
    ds <- tdf_winners_years %>%
        filter(year_raw >= input$startyear, year_raw <= input$endyear, nationality %in% input$nationalities)
    
    ggplot.plot <- ggplot(ds, aes(x = year_raw, y = time_overall, color = nationality)) +
        geom_line(size = 0.5)
    
    ggplotly(ggplot.plot)

Here's the corresponding ui.R code chunk:

numericInput(inputId = "startyear", label = "Enter starting year",
    value = 1960, min = 1903, max = 2019, step = 1),
numericInput(inputId = "endyear", label = "Enter ending year",
    value = 1970, min = 1903, max = 2019, step = 1),
checkboxGroupInput(inputId = "nationalities", label = "Nationalities to display",
    sort(unique(nationality_unique)),
    selected = c('France', 'Luxembourg'))

When I type each part of the filter function separately, I find the error is with:

filter(nationality %in% input$nationalities)

You need a req() . Try this

  output$plot <- renderPlotly({
    req(input$nationalities)
    ds <- tdf %>%
      filter(nationality %in% input$nationalities)
      #filter(year_raw >= input$startyear, year_raw <= input$endyear, nationality %in% input$nationalities)
    
    ggplot.plot <- ggplot(ds, aes(x = year_raw, y = time_overall, color = nationality)) +
      geom_line(size = 0.5)
    
    ggplotly(ggplot.plot)
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM