I have a sample working code as below. The result should group
( based on Species and/or Species2) then print out the mean
of Sepal.Length and Sepal.Width. I appreciate.
library(shiny)
library(dplyr)
iris$Species2 <- iris$Species
ui <- fluidPage(
selectInput(inputId ="column",
label = "Choose Column for Summary",
choices = c("Species", "Species2"),
selected = "Species", multiple = T),
DT::dataTableOutput('mytable')
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
Summarise <-
iris %>%
dplyr::group_by(groupby = input$column) %>%
summarise( mean1 = mean(iris$Sepal.Length))
DT::datatable(data = Summarise)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Expected Answer;
a <- iris %>%
dplyr::group_by(Species) %>%
dplyr::summarise( mean1 = mean(Sepal.Length)); a
Species mean1 mean2
<fct> <dbl>
1 setosa 5.01 5.01
2 versicolor 5.94 5.94
3 virginica 6.59 6.59
CodePudding user response:
You may use across
in group_by
so that you can group by multiple inputs.
library(dplyr)
library(shiny)
iris$Species2 <- iris$Species
ui <- fluidPage(
selectInput(inputId ="column",
label = "Choose Column for Summary",
choices = c("Species", "Species2"),
selected = "Species", multiple = T),
DT::dataTableOutput('mytable')
)
server <- function(input, output) {
output$mytable <- DT::renderDataTable({
Summarise <-
iris %>%
dplyr::group_by(across(all_of(input$column))) %>%
summarise(mean1 = mean(Sepal.Length))
DT::datatable(data = Summarise)
})
}
# Run the application
shinyApp(ui = ui, server = server)