0

I am trying to aggregate a reactive table from Shiny. My structure is similar to this example

library(shiny)

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                     sidebarPanel(textInput("text1", "Column 1"),
                                  textInput("text2", "Column 2"),
                                  actionButton("update", "Update Table")),
                     mainPanel(tableOutput("table1"))),
  server=function(input, output, session) {
    values <- reactiveValues()
    values$df <- data.frame(Column1 = NA, Column2 = NA)
    newEntry <- observe({
      if(input$update > 0) {
        newLine <- isolate(c(input$text1, input$text2))
        isolate(values$df <- rbind(values$df, newLine))
      }
    })
    output$table1 <- renderTable({values$df})
  }))

I am trying with several ways, for example:

output$table2 <- renderTable({
  as.data.frame(values$Column1, list(values$Column2), sum
})

But until now I could not have the expected result. Do you have an idea, please?

Community
  • 1
  • 1
RRuiz
  • 2,159
  • 21
  • 32
  • sorry for the edition. It is my first time here... – RRuiz Oct 02 '16 at 22:57
  • What is the expected result? It is just to add the values? or add a second table? – Geovany Oct 03 '16 at 03:03
  • Add a second table with aggregated values. For example if column 1 is numerical and column 2 is letters from A to E in thousend of entries. Then for example to aggregate the Column one by letters (function sum). – RRuiz Oct 03 '16 at 09:57
  • Oh sorry, I just see. There is a mistake in my code: Agreggate does not appear output$table2 <- renderTable({ as.data.frame(aggregate(values$Column1, list(values$Column2), sum )) – RRuiz Oct 03 '16 at 10:04

1 Answers1

1

Are you looking for something like a row sum? I used the apply function with sum, but the

output$table1 <- renderTable({cbind(values$df, Rowsum = apply(values$df, 1, function(x) sum(as.numeric(x))))})

Also, if you would like to remove the first empty line, you could use the advice in the SO question you linked

It looks like this:

enter image description here

Complete code:

library(shiny)

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                     sidebarPanel(textInput("text1", "Column 1"),
                                  textInput("text2", "Column 2"),
                                  actionButton("update", "Update Table")),
                     mainPanel(tableOutput("table1"))),
  server=function(input, output, session) {
    values <- reactiveValues()
    values$df <- data.frame(Column1 = numeric(0), Column2 = numeric(0))
    newEntry <- observe({
      if(input$update > 0) {
        newLine <- isolate(c(input$text1, input$text2))
        isolate(values$df[nrow(values$df) + 1,] <- c(input$text1, input$text2))
      }
    })
    output$table1 <- renderTable({cbind(values$df, Rowsum = apply(values$df, 1, function(x) sum(as.numeric(x))))})
  }))

Please note, that there is no exception handling implemented.

Community
  • 1
  • 1
GyD
  • 3,902
  • 2
  • 18
  • 28
  • Thanks....there is a mistake in my code: output$table2 <- renderTable({ as.data.frame(aggregate(values$Column1, list(values$Column2), sum) I forgot to write aggregate... – RRuiz Oct 03 '16 at 10:06
  • No problem. Check out the solution advised in the linked stackoverflow question for removing the empty line (also implemented in my code). Also welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted. – GyD Oct 03 '16 at 11:05
  • Yes! That was! Put away the NAS. Thank yoou very much. Here my code: output$table2 <- renderTable({(aggregate(as.numeric(values$df$Column1), (list(Variety=values$df$Column2)), sum))}) – RRuiz Oct 04 '16 at 10:25
  • Glad it works. Tip: in comments you can use two backtick characters to embed code. (Same way as in questions/answers) Example: `print(x)` – GyD Oct 04 '16 at 11:25