首页 > 解决方案 > 如何用闪亮的反应性地 group_by() 和 summarise() 数据?

问题描述

我正在构建一个闪亮的网络应用程序,它允许用户在不使用 R 编码的情况下获得最好的 dplyr(数据整理和操作 R 包)。

我想group_by()并分别使用一个 selectInput 允许在“电影”文件( http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdatasummarise())中的所有可用变量中选择一个变量")

请问有人知道如何解决这个问题吗?

使用以下服务器功能时,我得到一个空表...

  shinyServer(function(input, output){
    output$table <- DT::renderDataTable({
      moviesSummarise <- movies %>% 
        group_by(inputx) %>%
        summarise(Moyenne = mean(input$y)) 

      DT::datatable(data = moviesSummarise)
    })
  })

非常感谢。

 if (interactive()){

  load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))

  # Libraries 
     library(dplyr)
      library(shiny)
      library(shinydashboard)
      library(DT)

  ui1 <- shinyUI(
    dashboardPage(
      dashboardHeader(title = "Data Viewer"), 
      dashboardSidebar(
        selectInput(inputId = "x", 
                    label = "Grouper par :", 
                    choices = c("title_type", "genre", "mpaa_rating",                               
                   "studio", "thtr_rel_year"), 
                    selected = "thtr_rel_year"),
        selectInput(inputId = "y", 
                    label = "Résumé par :", 
                    choices = c("runtime","imdb_num_votes", 
                    "critics_score", "audience_score"), 
                    selected = "runtime")
      ), 
      dashboardBody(
        fluidPage(
          box(DT::dataTableOutput(outputId = "table"), title="Résumé des données :", height = 300)
        )
      )
    )
  )
server1 <- shinyServer(function(input, output){
    output$table <- DT::renderDataTable({
      moviesSummarise <- movies %>% 
        group_by(genre) %>% 
        ## It doesn't work when I tried group_by(input$x)

        summarise(Moyenne = mean(runtime)) 
        ## It doesn't work when I tried summarise(input$y)

      DT::datatable(data = moviesSummarise)
    })
  })
  shinyApp(ui = ui1, server = server1)
}

我希望汇总表会在我group_by()/summarise()使用selectInput("x", ...)selectInput("y", ...) 使用上面粘贴的代码的新变量时发生反应性变化,它会生成一个空表。

标签: rshinydplyr

解决方案


有两种方法可以做到这一点:使用group_byand的作用域版本summarise(它将获取字符串)或rlang取消引用输入。

既不group_by也不summarize喜欢字符串,而是期望裸名:

> movies %>% 
+         group_by("title_type") %>%
+         summarise(Moyenne = mean("runtime")) 
# A tibble: 1 x 2
  `"title_type"` Moyenne
  <chr>            <dbl>
1 title_type          NA

> movies %>% 
+         group_by(title_type) %>%
+         summarise(Moyenne = mean(runtime)) 
# A tibble: 3 x 2
  title_type   Moyenne
  <fct>          <dbl>
1 Documentary      NA 
2 Feature Film    107.
3 TV Movie        102.

使用范围版本:

> movies %>% 
+         group_by_at("title_type") %>%
+         summarise_at(vars(one_of("runtime")), list(Moyenne = mean))
# A tibble: 3 x 2
  title_type   Moyenne
  <fct>          <dbl>
1 Documentary      NA 
2 Feature Film    107.
3 TV Movie        102.

或者您可以使用该rlang方法并取消字符串:

> movies %>% 
+         group_by(!! sym("title_type")) %>% 
+         summarise(Moyenne = mean(!!sym("runtime")))
# A tibble: 3 x 2
  title_type   Moyenne
  <fct>          <dbl>
1 Documentary      NA 
2 Feature Film    107.
3 TV Movie        102.

所以:

if (interactive()){

    load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))

    # Libraries 
    library(dplyr)
    library(shiny)
    library(shinydashboard)
    library(DT)

    ui1 <- shinyUI(
        dashboardPage(
            dashboardHeader(title = "Data Viewer"), 
            dashboardSidebar(
                selectInput(inputId = "x", 
                            label = "Grouper par :", 
                            choices = c("title_type", "genre", "mpaa_rating",                               
                                        "studio", "thtr_rel_year"), 
                            selected = "thtr_rel_year"),
                selectInput(inputId = "y", 
                            label = "Résumé par :", 
                            choices = c("runtime","imdb_num_votes", 
                                        "critics_score", "audience_score"), 
                            selected = "runtime")
            ), 
            dashboardBody(
                fluidPage(
                    box(DT::dataTableOutput(outputId = "table"), title="Résumé des données :", height = 300)
                )
            )
        )
    )
    server1 <- shinyServer(function(input, output){
        output$table <- DT::renderDataTable({
            moviesSummarise <- movies %>% 
                group_by(!! sym(input$x)) %>% 
                summarise(Moyenne = mean(!!sym(input$y)))

            DT::datatable(data = moviesSummarise)
        })
    })
    shinyApp(ui = ui1, server = server1)

推荐阅读