如何使用闪亮的group_by()和summarise()数据?

时间:2019-08-13 23:32:07

标签: r shiny dplyr

我正在构建一个闪亮的Web应用程序,该应用程序使用户无需使用R进行编码即可获得dplyr(数据整理和处理R包)的精华。

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

任何人都知道如何解决这个问题吗?

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

  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", ...)一个新变量时,摘要表会作出反应 使用上面粘贴的代码,它将生成一个空表。

3 个答案:

答案 0 :(得分:1)

执行此操作的两种方法:使用group_bysummarise的作用域版本(将提取字符串)或使用rlang取消输入的引用。

group_bysummarize都不喜欢字符串,而是希望使用裸名:

> 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)

答案 1 :(得分:1)

您是否尝试过使用“ group_by( get(input $ x))”?

当使用无功输入时,这对我有用。

最好, 安妮

答案 2 :(得分:0)

更正第一台服务器。

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

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