2

I have two problems: -First I cannot start the table showing only zeros or blank for that reason I've created a button that set's every thing to zero which kind of works but its not ideal. -Secondly, I'm trying to calculate column sum for each column in a rhandontable but so far I havent been able to make it work.

I have been looking around for similar problems and I have found some code that looked promissing but in the end it only provided the sum for a single column or with some changes it would sum up all columns which is not what I'm after.

ui <- fluidPage(
  rHandsontableOutput('table'),
  textOutput('result'),
  actionButton("recalc", "re-enter data")
)

season<-c("Spring","Summer","Autum","Winter")

server <- function(input,output,session)({
  values <- reactiveValues(data = NULL) ## assign it with NULL

  ## button press resets now the data frame
  observeEvent(input$recalc, { 
    values$data[] <- 0
  })

  ## changes in numericInput sets all (!) new values
  observe({
    values$data <-data.frame(row.names=season,Lake=1:4,Beach=1:4, Garden=1:4,stringsAsFactors = FALSE)

  })

  observe({
    if(!is.null(input$table))
      values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    req(values$data)
    rhandsontable(values$data,rowHeaderWidth = 100)
  })

})

shinyApp(ui = ui, server = server)

The expected results would be a 5th column with the sum of each column. A starting table filled with zeros or blank.

If anyone could point me in the right direction it would be very much appreciated.

PatraoPedro
  • 197
  • 1
  • 16
  • If you want to start your table with zeros, why don't you simply replace 1:4 with rep(0,4) in your code above? – StatsStudent Oct 09 '19 at 01:30
  • @StatusStudent, thank you for the solution on the zeros it worked perfectly. Now I'm only missing the columns sums. cheers – PatraoPedro Oct 09 '19 at 07:15

1 Answers1

3

You can create empty cells with NA. Please check the following example to calculate colSums:

library(shiny)
library(rhandsontable)

ui <- fluidPage(
  br(),
  rHandsontableOutput('table'),
  textOutput('result'),
  br(),
  actionButton("recalc", "re-enter data")
)

rowNames <- c("Spring", "Summer", "Autum", "Winter", "Sum")
defaultDF <- data.frame(
    row.names = rowNames,
    Lake = rep(NA_integer_, 5),
    Beach = rep(NA_integer_, 5),
    Garden = rep(NA_integer_, 5),
    stringsAsFactors = FALSE
  )

server <- function(input, output, session)
  ({
    values <- reactiveValues(data = defaultDF) ## assign it with NULL

    ## button press resets now the data frame
    observeEvent(input$recalc, {
      values$data[] <- NA_integer_
    })

    observe({
      req(input$table)
        DF <- hot_to_r(input$table)
        DF[setdiff(rowNames, "Sum"),]
        DF["Sum",] <- colSums(DF[setdiff(rowNames, "Sum"),], na.rm = TRUE)
        values$data <- DF
    })

    output$table <- renderRHandsontable({
      req(values$data)
      rhandsontable(values$data, rowHeaderWidth = 100) %>%
        hot_row(nrow(values$data), readOnly = TRUE)
    })

  })

shinyApp(ui = ui, server = server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78