0

I am trying to develop a shiny app for a course, which takes in english premier league data from 1992 till date and sorts all the clubs by various criteria. e.g. the output should show us the top 5 clubs till date in terms of 'Goals Scored' or 'Win Percentage' whichever input the user selects.

I am using the dplyr package's arrange_ function to sort the dataset with the user's input variable but keep getting an error saying :

Error : incorrect size (1), expecting : 47

Following is the ui.R snippet which creates the user Input :

radioButtons("categoryInput", "Criteria", choices = list(
                                                          "OverallRank"= "Rank",
                                                          "Games Played" = "P",
                                                          "Games Won" = "W",
                                                          "Goals Scored" = "F",
                                                          "Total Points" = "Points",
                                                          "Win Percentage" = "WinPercent"

  ), selected = "Points")

Following is the snippet of server.R which I am using to sort the dataset :

output$table <- renderTable({
leagueTable = arrange_(pml, desc((input$categoryInput)))
head(leagueTable)

})

I have read that the dplyr function wants the user inputs to be passed on with an as.symbol conversion but that did't work either. So I am not sure what my options are to sort this dataset in a decreasing order based on user input.

Any help would be appreciated!

s_om
  • 661
  • 2
  • 9
  • 24
  • 1
    The problem is with using `desc`. You'll need to use `interp` within `arrange_`. A comment to [this question](http://stackoverflow.com/questions/27034655/how-to-use-dplyrarrangedesc-when-using-a-string-as-column-name?lq=1) shows how to do this. – aosmith Jun 08 '16 at 22:55

1 Answers1

0

As per mentioned by @aosmith in the comments, you have to use interp:

arrange_(mtcars, lazyeval::interp(~desc(var), var = as.name(input$categoryInput)))

Here's an example ShinyApp:

server <- function(input, output) {
  output$table <- renderDataTable({
    arrange_(mtcars, lazyeval::interp(~desc(var), 
                                      var = as.name(input$categoryInput)))
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      radioButtons("categoryInput", "Criteria", choices = list(
        "Disp"= "disp",
        "Cylinder" = "cyl",
        "Drat" = "drat"), 
        selected = "disp")
    ),
    mainPanel(
      dataTableOutput("table")
    )
  )
)

shinyApp(ui = ui, server = server)

enter image description here

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77