Hi there,
That's an exciting functionality. I've included an app (with some fake data for the Sample
dataframe so that you can run it) that does what I think you're trying to do. If I'm not misunderstanding you're essentially trying to get the sum of the column that is selected, right? In my app I've given that input
the name select_column
to refer to it as input$select_column
.
The thing that you need to keep in mind is that you can't pass the character string that you get from selectInput()
into summarise()
. For the summarise()
function you need to input a symbol, not a string. So Sales
instead of "Sales"
. What your summarise function is "seeing" in the code you posted is similar to this:
Sample %>% summarise(`Total Sales` = sum("Sales"))
#Error: Problem with `summarise()` input `Total Sales`.
#x invalid 'type' (character) of argument
#ℹ Input `Total Sales` is `sum("Sales")`.
#Run `rlang::last_error()` to see where the error occurred.
So what you need to do is change the string into a symbol, using the as.symbol
function, and then tell R to evaluate it as such within the environment with the eval
function. So this works:
> Sample %>% summarise(`Total Sales` = sum(eval(as.symbol("Sales"))))
## A tibble: 1 x 1
# `Total Sales`
# <dbl>
#1 600
So for an input
called select_column
this is possible:
Sample %>%
group_by(`Order Date`, Category) %>%
summarize(total = sum(eval(as.symbol(input$select_column))))
Here's that within the context of a functioning demo app that you can run. Hope this helps 
library(shiny)
library(dplyr)
library(DT)
set.seed(0)
Sample <- tibble(`Order Date` = c(rep(as.Date("2020-08-01"), 10),
rep(as.Date("2020-08-02"), 10)),
`Category` = c(rep("Category A", 5),
rep("Category B", 10),
rep("Category A", 5)),
Sales = round(rnorm(n = 20, mean = 30, sd = 5)),
Profit = Sales * rnorm(n = 20, mean = 0.75, sd = 0.1)
)
selectable_columns <- c("Sales", "Profit")
ui <- fluidPage(
selectInput(inputId = "select_column", label = "Select which column to sum",
choices = selectable_columns),
dataTableOutput("my_table")
)
server <- function(input, output, session) {
output$my_table <- renderDataTable({
Sample %>%
group_by(`Order Date`, Category) %>%
summarize(total = sum(eval(as.symbol(input$select_column)))) %>%
DT::datatable()
})
}
shinyApp(ui, server)