I\'m writing a new Shiny app, and I would like to stay within the Hadleyverse by using dplyr commands for data manipulation. I want Shiny to print a table showing only the t
The problem is that arrange() function expects your argument as a symbol.
However, your input$top3 is a string.
The trick is:
output$t3 <- renderTable({
sorted_stats <- eval(substitute(employeestats %>% arrange(desc(col)),
list(col=as.symbol(input$top3))))
head(sorted_stats,n=3)
},include.rownames=TRUE)
You can use ?substitute to see how it works.
Short version, it parse the expression:
employeestats %>% arrange(desc(col))
into a parse tree consisting of call's (functions) and name's (symbols, constants, objects), allowing us to substitute the components to form a new expression.
To this point, the expression is not evaluated (meaning, the employeestats is not arranged yet).
Here col doesn't really mean anything. It simply serves as a placeholder.
And by passing list(col=as.symbol(input$top3)) to substitute, we replace the dummy symbol col by the actual symbol we would like to arrange by, as.symbol(input$top3).
Depending on the current input, this would either be TotalAwarded or NumberOfAwards (symbols, not strings anymore).
Finally, the eval() function evaluated the expression (with col replaced by the actual symbol), and returns the sorted data.frame.