2016-06-08 95 views
0

我試圖開發一個閃亮的應用程序,從英國首屈一指的聯賽數據從1992年到日期,並按各種條件排序所有俱樂部。例如根據用戶選擇的「輸入目標」或「輸贏比例」,輸出結果應顯示前5名俱樂部。dplyr函數與閃亮的應用程序與desc的問題

我使用dplyr包的arrange_功能進行排序與​​用戶的輸入變量的數據集,但不斷收到一個錯誤說:

Error : incorrect size (1), expecting : 47 

以下是ui.R片段中創建用戶輸入:

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

), selected = "Points") 

以下是我現在用排序的數據集server.R的片段:

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

})

我讀過dplyr函數希望用戶輸入是通過as.symbol轉換來傳遞的,但也沒有效果。所以我不確定我的選擇是根據用戶輸入按降序對數據集進行排序。

任何幫助,將不勝感激!

+1

的問題是使用'遞減'。你需要在'arrange_'中使用'interp'。 [這個問題]的評論(http://stackoverflow.com/questions/27034655/how-to-use-dplyrarrangedesc-when-using-a-string-as-column-name?lq=1)顯示如何做這個。 – aosmith

回答

0

按在評論中提到的@aosmith,你必須使用interp

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

下面是一個例子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

+1

謝謝你做到了! –