【问题标题】:How to build an aggregate table based on the another shiny table?如何基于另一个闪亮的表构建聚合表?
【发布时间】:2018-04-10 19:05:28
【问题描述】:

我想在 UI 中有一个输出表,它有一个基于“第 3 列”文本的条件列总和。

这是我的示例代码:

library(shiny)
runApp(list(
ui=pageWithSidebar(headerPanel("Adding entries to table"),
                 sidebarPanel(textInput("text1", "Column 1", "66"),
                              textInput("text2", "Column 2", "100"),
                              textInput("text3", "Column 3", "Tony"),
                              actionButton("update", "Update Table")),
                 mainPanel(tableOutput("table1"))),
server=function(input, output, session) {
values <- reactiveValues()
values$df <- data.frame(Column3 = character(), 
                        Column1 = numeric(0), 
                        Column2 = numeric(0),
                        stringsAsFactors = FALSE)
newEntry <- observe({
  if(input$update > 0) {
    newLine <- isolate(c(input$text3, input$text1, input$text2))
    isolate(values$df[nrow(values$df) + 1,] <- c(input$text3, 
input$text1, input$text2))
  }
})
output$table1 <- renderTable({values$df})
}))

如果你点击“更新表”按钮两次,我想看到的输出表是:

`1    Tony    132    200`

“第 3 列”中的名称会有所不同,具体取决于侧边栏中的输入值。输出表应该观察到这一点,然后返回该名称下所有“Column 1”之和和“Column 2”之和的名称。

我尝试使用values$df 上的聚合函数创建一个新的数据框,但它不起作用。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我想这就是你的目标。我在代码中添加了适当的 cmets 供您理解。希望对您有所帮助!

    library(shiny)
        runApp(list(
          ui=pageWithSidebar(headerPanel("Adding entries to table"),
                             sidebarPanel(textInput("text1", "Column 1", "66"),
                                          textInput("text2", "Column 2", "100"),
                                          textInput("text3", "Column 3", "Tony"),
                                          actionButton("update", "Update Table")),
                             mainPanel(tableOutput("table1"))),
          server=function(input, output, session) {
            values <- reactiveValues()
            values$df <- data.frame(Column3 = character(), 
                                    Column1 = numeric(0), 
                                    Column2 = numeric(0),
                                    stringsAsFactors = FALSE)
            observeEvent(input$update,{
              # if(input$update > 0) {#This is not required as this observeEvent will only be triggered when we click 'update' button 
                newLine <- isolate(c(input$text3, input$text1, input$text2))
                #If any column 3 has already been updated, just add the other columns
                if(any(values$df$Column3 == input$text3))
                {
                  #Get the row where there is match in the Column3
                  matchRow = which(values$df$Column3 == input$text3)
                  #Just add the values in column2 and column3 with input values
                  values$df$Column1[matchRow] <- as.numeric(values$df$Column1[matchRow])+as.numeric(input$text1)
                  values$df$Column2[matchRow] <- as.numeric(values$df$Column2[matchRow])+as.numeric(input$text2)
    
                }else{#Just add the new rows
    
                  isolate(values$df[nrow(values$df) + 1,] <- c(input$text3, 
                                                               input$text1, input$text2))
                }
              # }
            })
            output$table1 <- renderTable({values$df})
          }))
    

    【讨论】:

      猜你喜欢
      • 2023-01-09
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2021-09-26
      • 2013-05-12
      • 2020-07-30
      相关资源
      最近更新 更多