【问题标题】:How can we add groupby taking inputs from the user from the table we have uploaded?我们如何通过从我们上传的表中获取用户的输入来添加 group?
【发布时间】:2019-06-10 07:32:20
【问题描述】:

我正在尝试从用户那里获取 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 的结果,并根据用户选择的列进行计数

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    1) 为用户创建一个选择列的位置。当您为此使用用户数据时,?renderUI 对我来说似乎是一个不错的选择。应该这样做:

    output$group_by_selection <- renderUI({
      req(data())
      selectizeInput(
            'group_by_select', 'Group by', choices = colnames(data()), multiple = TRUE
          ),
    

    它应该在server.R 文件中。在ui.R 中使用uiOutput('group_by_selection') 来显示它。

    现在,您需要在用户完成选择后对数据进行分组和计数。您可以通过按下按钮或其他方式来完成。它可以看起来像这样,使用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 
      }
    })
    

    【讨论】:

    • 它只显示要选择的列名,但不显示该 groupby 的实际结果
    • 另外,它的显示列名只出现在首页,我们看不到其他列
    • 你能帮忙吗?
    • 我正在努力。选择一列后,您需要运行一段代码来计算数据或您需要对其执行的任何操作。你可以用不同的方式来做,我的建议的哪一部分不起作用?您需要添加一个将运行“分组依据”代码的按钮,以及按下按钮时将执行的部分代码。这部分存在于我的 sn-p 中。我不会为你写一切。
    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2020-01-15
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多