1

I am trying to update a given cell in rhandson table, based on value introduced by the user in another rhandson table.

Basically, I would like to extract the value introduced in the second column of the second table, from the second column of the first table.

Example: I put the value 50 in the first row, column Budget of table2, and I want the value to be subtracted from the first row, column Budget of table 1.

I adapted the example from here:

library(shiny)
library(rhandsontable)

channel <- c("Budget")
start.date <- as.Date("2017-01-01")
end.date <- as.Date("2017-01-03")
date.range1 <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")
date.range1 <- as.data.frame(date.range1)

date.range2 <- as.Date((seq(start.date,end.date,by="day")), origin = "1970-01-01")
date.range2 <- as.data.frame(date.range2)

colnames(date.range1) <- c("date")
colnames(date.range2) <- c("date")

date.range1[channel] <- 1000
date.range2[channel] <- 0

table1 <- date.range1
table2 <- date.range2
#Define the tables.

ui <- fluidPage(
  br(),
  fluidRow(
    column(4, rHandsontableOutput("table1output")),
    column(4, rHandsontableOutput("table2output"))
  ))

server <- function(input,output,session){
  table <- reactiveValues()
  table$table1 <- table1
  table$table2 <- table2
  
  #Define the tables
  
  output$table1output <- renderRHandsontable({rhandsontable(table$table1)})
  output$table2output <- renderRHandsontable({rhandsontable(table$table2)})
  
  observeEvent(input$table1output,{
    df <- hot_to_r(input$table1output)
    df <- as.data.frame(df)
    #table$table1 <- df
  }, ignoreInit = TRUE, ignoreNULL = TRUE
  )
  
  observeEvent(input$table2output,{
    df <- hot_to_r(input$table2output)
    df <- as.data.frame(df)
  }, ignoreInit = TRUE, ignoreNULL = TRUE
  )
  
}

shinyApp(ui = ui, server = server)
guir
  • 69
  • 5

1 Answers1

2

In this case, input$table2output$changes$changes will have the information needed to modify your other table.

In particular:

# [[1]][[1]] will have the row edited 
# [[1]][[2]] will have the column edited 
# [[1]][[4]] will have the new value

Note that these are zero-indexed.

You can include an if statement to make sure you only change values based on budget column changes, and not other columns like the date.

observeEvent(input$table2output,{
  df <- hot_to_r(input$table2output)
  df <- as.data.frame(df)
    
  table_changes <- input$table2output$changes$changes
    
  if (!is.null(table_changes[[1]][[2]]) && table_changes[[1]][[2]] == 1) {
    table$table1[table_changes[[1]][[1]] + 1, table_changes[[1]][[2]] + 1] <- table$table1[table_changes[[1]][[1]] + 1, table_changes[[1]][[2]] + 1] - table_changes[[1]][[4]]
  }
}, ignoreInit = TRUE, ignoreNULL = TRUE
)
Ben
  • 28,684
  • 5
  • 23
  • 45
  • many thanks! it works indeed! I overlooked the possibility of having "$changes$changes" – guir Sep 17 '20 at 19:30
  • how would you do to extract only from the first row of the Budget column of table1, the SUM of all the values in column Budget in table2? Many thanks for any ideas! – guir Sep 17 '20 at 21:03
  • Hi @guir - I'm not sure I follow. Can you describe further? Is the question about summing all the values in one of the tables? Or setting a value in one table to be the sum of values in another table? Or maybe you could give an example? – Ben Sep 17 '20 at 21:06
  • Hi @Ben - indeed, the cell [1,2] in table 1 to equal the sum of column 2 in table 2. Option 2 in your comment. Example: table1$Budget[1] = table1$Budget[1] - sum(table2$Budget) – guir Sep 17 '20 at 23:09
  • Maybe you might want something like: `table$table1[1,2] <- table1[1,2] - sum(df$Budget)` ... this will subtract the *sum* of the `Budget` column in Table 2, from the *initial* value of `table1`, and store in the reactive `table$table1`...maybe that is closer to what you had in mind? – Ben Sep 17 '20 at 23:32