Hello! I've made a small reproducible example based partially on this thread.
What I'm trying to do: I have two editable tables, where the summary values of the top table partially depend on edits made in the bottom table Petal.Width
column updates. When the user updates the bottom table values of that column, their values will be summarized in the top table value[[1]]
cell. The problem starts when I also want to update other top table cells independently of the bottom table updates made so far. Example:
- I change a value in the bottom table which updates the top table
- I update a value in the top table
- I change a value in the bottom table again which clears any independent changes made to the top table in point 2)
Ideally I would like those changes to accumulate incrementally.
Questions:
-
Is my current approach of using
eventReactive()
wrapped intoobserve()
,reactiveValues()
and finallyObserveEvent()
optimal? I created this code based on a couple of different examples I was able to find online but I have trouble understanding the true nature of all those dependencies and functions used. I was reading already a couple of different references (including shiny website) but I'm missing a step-by-step guide to really putting all of this together. -
How can I achieve this level of dependency-indenpendency of the top table from bottom table I mentioned above in the example?
Here's my current code:
library(tidyverse)
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
actionButton(
inputId = "go",
label = "Update"
),
DTOutput('x1'),
DTOutput('x2'),
verbatimTextOutput("print")
),
server = function(input, output, session) {
evt_go_iris <- eventReactive(input$go, {
iris
})
evt_go_mtcars <- eventReactive(input$go, {
mtcars %>%
gather() %>%
group_by(key) %>%
summarise(value = sum(value))
})
rec_val = reactiveValues(df = NULL)
observe({
tmp_iris <- evt_go_iris()
tmp_mtcars <- evt_go_mtcars()
rec_val$iris <- tmp_iris
rec_val$mtcars <- tmp_mtcars
})
output$x1 = renderDT(
rec_val$mtcars,
editable = TRUE
)
output$x2 = renderDT(rec_val$iris, selection = 'none', editable = TRUE)
observeEvent(input$x2_cell_edit, {
info = input$x2_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
rec_val$iris[i, j] <- isolate(DT::coerceValue(v, rec_val$iris[i, j]))
rec_val$mtcars$value[[1]] <- sum(rec_val$iris$Petal.Width)
})
output$print <- renderPrint({
rec_val$iris
})
}
)