r - 如何用闪亮的反应性地 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", ...)
使用上面粘贴的代码的新变量时发生反应性变化,它会生成一个空表。
解决方案
有两种方法可以做到这一点:使用group_by
and的作用域版本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)
推荐阅读
- python - 这两个代码的时间复杂度是多少
- vb.net - 反转数字
- ember.js - 如何在 ember 2.17 中为 queryParams 设置动态值
- swiftui - Xcode 11.1 SwiftUI 预览失败
- javascript - 在 mxgraph 编辑器的侧边栏中添加子菜单?
- .net - asp.net Web 应用程序 Azure App 服务远程调试中的问题
- graphql - 适用于 100% 公开、未经身份验证的 API 的 AppSync 安全最佳实践
- python - VS-Code 不绘制 gpd.GeoSeries().plot()
- java - 如何检查日期是否大于新年日期?
- c++ - 通过分配大量内存来跟踪堆损坏?