Reputation: 713
I want to sort data frame by two columns first is factor and second numeric but this is selected in shiny.
When I use this line with specified columns it works well.
iris1<-iris[with(iris,order(Species,Sepal.Length,decreasing = T)), ]
But inside shiny I replace Sepal.Length with reactive values and this returns error. Please see 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 I get is
Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, :
argument lengths differ
Upvotes: 1
Views: 279
Reputation: 56219
Try this, using get()
to convert string to column name:
col <- "Sepal.Width"
iris[with(iris, order(Species, get(col), decreasing = TRUE)), ]
And within shiny it should be: get(col())
.
Note that input$a
is already reactive, so we can say: get(input$a)
Upvotes: 3