我正在尝试从用户那里获取group_by的输入,并计算用户从上传的CSV文件中选择的列上的数据。简而言之,用户应选择需要分组的列并获取数据计数
我能够上载文件并在“加载”部分中获取摘要,我已经为此group_by部件创建了一个准备列。
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
ui<-dashboardPage(
dashboardHeader(title = "Model"),
dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
menuSubItem("Load", tabName = "data1"),
menuSubItem("Prep", tabName = "prep")
),
menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
menuItem("Result", icon=icon("cog"), tabName = "result")
)
),
dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }"
),
tabItems(
tabItem(tabName = "data1",
fluidPage(
fluidRow(
fileInput("file1","Choose CSV File",
accept = c("text/csv",
"text/comma-seperated-values, text/plain",
".csv")
),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep","Separator",
choices=c(Comma=",",
semicolon=";",
Tab="\t"),
selected = ";")
),
mainPanel(
uiOutput("tb")
)
)
)
),
tabItem(tabName = "prep",
fluidPage(
fluidRow(
mainPanel(
uiOutput("Pre")
)
)
))
)
)
server <- shinyServer(function(input,output){
data <- reactive({
file1 <- input$file1
if(is.null(file1)){return()}
read.csv(file = file1$datapath, sep=input$sep)
})
output$filedf <- renderTable({
if(is.null(data())){return()}
input$file1
})
output$sum <- renderTable({
if(is.null(data())){return()}
summary(data())
})
output$table <- renderTable({
if(is.null(data())){return()}
data()
})
output$tb <- renderUI({
if(is.null(data())){return()}
tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})
#----- Data Preparation------
output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
label="Select Variables",
choices = names(filedf))
})
filedf_sel <- reactive({
req(input$select_vars)
filedf_sel<- data()%>% select(input$select_var)
})
})
shinyApp(ui,server)
输出应该是group_by的结果,并取决于用户选择的列
答案 0 :(得分:0)
1)创建一个供用户选择列的位置。当您使用用户数据时,?renderUI
对我来说似乎是一个不错的选择。这样的事情应该做:
output$group_by_selection <- renderUI({
req(data())
selectizeInput(
'group_by_select', 'Group by', choices = colnames(data()), multiple = TRUE
),
它应该在server.R
文件中。在uiOutput('group_by_selection')
中使用ui.R
进行显示。
现在,您需要在用户完成选择后对数据进行分组和计数。您可以通过按按钮或其他方式来实现。使用data.table
库可以很容易地进行分组和计数,它看起来像这样:
grouped_data <- eventReactive(input$group_by_button, {
if (length(input$group_by_select) > 0 ) {
data()[, Count := .N, by = input$group_by_select]
} else {
NULL
}
})