2016-06-27 63 views
1

我想按兩列排序數據幀,第一個數字是因子和第二個數字,但是這是選擇閃亮的。當用戶選擇一個數據框時,將數據框排序爲兩列

當我在指定的列上使用這條線時,效果很好。

iris1<-iris[with(iris,order(Species,Sepal.Length,decreasing = T)), ] 

但是裏面閃閃發亮,我將Sepal.Length替換爲無效值並返回錯誤。請參閱MRE

# ui.R 
library(shiny) 


shinyUI(pageWithSidebar(

    # Application title 
    headerPanel("Miles Per Gallon"), 

    sidebarPanel(selectInput("a","select column",c("Sepal.Length","Sepal.Width"))), 

    mainPanel(tableOutput("b")) 
)) 

# server.R 
library(shiny) 


shinyServer(function(input, output) { 

    col<-reactive({input$a}) 
output$b<- renderTable({ 
########### this line probably causes the problem 
    iris1<-iris[with(iris,order(Species,col(),decreasing = T)), ] 
}) 
}) 

錯誤我得到的是

Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, : 
    argument lengths differ 
+0

哪一列山坳()是指在沒有山坳由用戶選擇? –

+0

Sepal.Length在那裏作爲默認 –

回答

3

試試這個,使用get()字符串轉換爲列名:

col <- "Sepal.Width" 
iris[with(iris, order(Species, get(col), decreasing = TRUE)), ] 

而且內有光澤應該是:get(col())

注意input$a已經是被動的,所以我們可以說:get(input$a)

+0

優秀我認爲它可能是問題物種沒有報價和反應價值與報價,但不知道從哪裏去:) –

+0

@TomasH'col()'是一個函數在基地R,我會避免使用現有的函數名稱作爲變量。 – zx8754

相關問題