如何将 group_by_() 与闪亮和反勾一起使用>

How to use group_by_() with shiny and back ticks>

我想让我的例子起作用。当我不使用反引号并且例如将 Miles Per Gallon 更改为 MilesPerGallon 时,它会按列正确分组。但是,一旦我把它放在后面的刻度线中,它就不起作用了。

library(shiny)
library(DT)
library(tidyverse)

ui <- fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(

        selectInput("groups", "Choose Groupings", choices = "Miles Per Gallon", multiple = TRUE, selected = "")

      ),


      mainPanel(

        DT::dataTableOutput("data")

      )
   )
)


server <- function(input, output) {

  output$data <- DT::renderDataTable({

    mtcars %>%
      select(`MilesPerGallon` = mpg, cyl, wt) -> dat

    if(length(input$groups) == 0) {

     dat

    } else {

      dat %>%
        dplyr::group_by_(input$groups) %>%
        summarise(n = n())

    }


  })

}

# Run the application 
shinyApp(ui = ui, server = server)

使用接受字符串输入的group_by_at

library(shiny)
library(DT)
library(tidyverse)

ui <- fluidPage(

  titlePanel("Old Faithful Geyser Data"),
  sidebarLayout(
      sidebarPanel( 
      selectInput("groups", "Choose Groupings", choices = "Miles Per Gallon", 
                    multiple = TRUE, selected = "") 
),

mainPanel(  
  DT::dataTableOutput("data")
    )
  )
)

server <- function(input, output) {
    output$data <- DT::renderDataTable({
    mtcars %>%
      select(`Miles Per Gallon` = mpg, cyl, wt) -> dat

    if(length(input$groups) == 0) {
     dat
   } else {
      dat %>%
         dplyr::group_by_at(input$groups) %>%
         summarise(n = n())
     }
   })
}
# Run the application 
shinyApp(ui = ui, server = server)