'Update variable created by eventReactive in another observeEvent

I'm struggling to update a reactive variable, that is created with eventReactive(), in an observeEvent() with new data.

The background is following: I have a data.frame df with some variables (x and y) and number of observations depending on the selected city (created randomly for this example). x and y are initialized with zeros. Because I need to further process df, I pass df to city_df in an eventReactive().

So far, so good. Next, I want to add new data to city_df. The computation of this new data is dependent on the "compute" actionButton (input$compute), wherefore I update city_df in an observeEvent(). I manage to read the data stored in city_df, but I am struggling to overwrite its content. Actually, I am a bit unsure if this is possible at all, but I hope that some of you could give me a hint on how to update the reactive variable city_df with the new data in this observeEvent() and have its output evaluated in the app(?).

library(shiny)

# global variables
cities <- c("Nairobi", "Kansas", "Uppsala", "Sangon", "Auckland", "Temuco")

# ui
ui <- fluidPage(
  fluidPage(
    fluidRow(
      column(2,
             selectInput("city", "Select city",
                         choices = cities,
                         selected = sample(cities,
                                           size = 1)
             ),
             actionButton("compute",
                          "Compute")),
      column(8,
             verbatimTextOutput("the_city"))
    ))
)

# server
server <- function(input, output, session) {
  # create variable
  city_df <- eventReactive(input$city, {
    len <- round(runif(1, 20, 50), 0)
    df <- data.frame(city = rep(input$city, len))
  # initialize x and y with zeros
    df <- cbind(df,
                data.frame(x = rep.int(0, len),
                           y = rep.int(0, len)))
  })

  output$the_city <- renderText({
    paste(city_df())
  })

  observeEvent(input$compute, {
    # grab data
    test <- city_df()
    # compute new data
    test$x <- runif(dim(test)[1], 11, 12)
    test$y <- runif(dim(test)[1], 100, 1000)
    # and how to send this values back to city_df?
  })
  }

# run app
shinyApp(ui, server)

The actual app is far more complex--so forgive me if this MWE app seems a bit overly complicated to achieve this usually simple task (I hope I managed to represent the more complex case in the MWE). Instead of a data.frame, I am parsing layers of a GeoPackage and append some variables initialized with zeros. The selected layer is displayed in a Leaflet map. On pressing the "compute" button, a function computes new data that I wish to add to the layer to then have it displayed on the map. The alternative solution I have on mind is to write the new data to the GeoPackage and then, reread the layer. However, I would appreciate if I could avoid this detour as loading the layer takes some time...

Many thanks :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source