[英]How to group_by() and summarise() data reactively with shiny?
[英]How to aggregate data reactively with shiny by the variables you selected from UI?
我正在開發一個 R 閃亮的應用程序,並嘗試根據我從 UI 中選擇的變量按組總和聚合數據。 以下是我的原始數據:
Date Month Site enrollment
3/30/2020 2020-03 14 1
4/6/2020 2020-04 14 21
4/13/2020 2020-04 14 8
4/20/2020 2020-04 14 8
4/27/2020 2020-04 14 13
5/4/2020 2020-05 14 18
5/11/2020 2020-05 14 19
5/18/2020 2020-05 14 13
我想按組總和聚合數據。
Month Site enrollment
2020-03 14 1
2020-04 14 50
2020-05 14 35
我嘗試通過以下代碼創建反應數據:
raw<-subset(aggregate(get(input$ycol) ~ get(input$xcol)+get(input$fill),df, sum),get(input$ycol)!=0);
不幸的是,R 閃亮不承認get(input$ycol)
。 如果我將其更改為我選擇的變量名稱(例如站點、注冊),那么它運行良好。
以下是我閃亮的代碼。
library(dplyr)
# Define UI for overall application
ui <- fluidPage(
# Application title
titlePanel("Data Visualization -- Clinical Study Enrollment"),
tabsetPanel(
# Data upload tab
tabPanel("Upload File",
titlePanel("Upload CSV File"),
# sidebar layout with input and output definitions--
sidebarLayout(
# sidebar panel for inputs ---
sidebarPanel(
# input-- select file
fileInput('file_input',
'Choose CSV(UTF-8) File',
multiple = FALSE,
accept = c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
# Horizontal line ----
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep',
'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote',
'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# main panel to display outputs
mainPanel(
# output-- data file
DT::dataTableOutput('contents')
)
)
),
# point estimate line plot
tabPanel("Clinical study enrollment Plot",
pageWithSidebar(
headerPanel('Clinical study enrollment Plot'),
sidebarPanel(
# drop down menu inputs
selectInput('xcol', 'Select X', ""),
selectInput('ycol', 'Select Y', ""),
selectInput('fill', 'Group', ""),
),
mainPanel(
DT::dataTableOutput('lineplot_table'))
)
)))
server <- function(input, output, session) {
# Read upload file
upload_data <- reactive({
req(input$file_input);
tryCatch({
df <- read.csv(input$file_input$datapath,
header = input$header,
sep = input$sep,
quote = input$quote,
stringsAsFactors = FALSE,check.names=FALSE,fileEncoding = "UTF-8-BOM");
}, error = function(e) {
stop(safeError(e));
})
return(df);
})
# Display
display_data <- reactive({
df <- upload_data();
if(input$disp == "head") {
return(head(df));
} else {
return(df);
}
})
observe({
df <- upload_data();
updateSelectInput(session, inputId = 'xcol',
label = 'Select X',
choices = names(df), selected = "Site");
updateSelectInput(session, inputId = 'ycol',
label = 'Select Y',
choices = names(df), selected = "enrollment");
updateSelectInput(session, inputId = 'fill',
label = 'Group',
choices = names(df), selected = "Date");
})
# Get bar plot data
get_barplot_data <- reactive({
df <- upload_data();
raw<-subset(aggregate(get(input$ycol) ~ get(input$xcol)+get(input$fill),df, sum),get(input$ycol)!=0);
return(raw)
})
# Display data you uploaded
output$contents <- DT::renderDataTable({
display_data();
})
output$lineplot_table <- DT::renderDataTable({
get_barplot_data();
})
}
shinyApp(ui = ui, server = server)
嘗試使用as.formula
或reformulate
在這種情況下,建立自己的公式。 這將允許您在創建aggregate
公式時使用來自閃亮input
字符值。
例如:
raw <- subset(
aggregate(
reformulate(
response = input$ycol,
termlabels = c(input$xcol, input$fill)
),
df,
FUN = sum
),
get(input$ycol) != 0
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.