Hi,
There seems to be some misunderstanding of what observers and reactives do, in general if you're creating a reactive dataset you should use reactive() rather than observe().
In your case, you want the dataset to only update in response to a single action so you'd use the reactive equivalent of observeEvent(), which is eventReactive().
If you want to understand the difference between reactive() and observe() better, I'd recommend this talk (and part 2) by Joe Cheng.
Additionally, observers (like observeEvent()) should almost never be used to create other observers (you should never have to nest observeEvent()s like you did in your example. In your first post you mention doors that remain open and that is more or less correct, observeEvent() essentially creates a listener so if you nest them, every time it is run it is creating a new observer.
I've put below your example, using eventReactive instead of observeEvent. Important point: when you call a reactive dataset created with eventReactive remember to call it as a function (dataset_to_use() rather than dataset_to_use).
Hope that helps.
library(shiny)
library(palmerpenguins)
library(DT)
dataset_1<-penguins
dataset_2<-iris
ui <- fluidPage(
titlePanel("Rstudio community issue"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "select_dataset",
label = "Select the dataset to visualize",
choices = c("Penguin dataset"="dataset_1",
"Iris dataset"="dataset_2")),
actionButton("dataset_selection",
label = "Select the dataset"),
br(),
br(),
uiOutput("more_selection"),
uiOutput("look_dataset")),
mainPanel(uiOutput("datatable"),
uiOutput("last_datatable"))
)
)
server <- function(input, output) {
dataset_to_use <- eventReactive(input$dataset_selection, {
switch(input$select_dataset,
"dataset_1"=dataset_1,
"dataset_2"=dataset_2)
})
output$datatable<-renderUI({
DT::dataTableOutput("datatable_1")
})
output$datatable_1<-renderDT({
datatable(dataset_to_use())
})
output$more_selection<-renderUI({
wellPanel(
selectInput("filter_to_analyze",
label = "Select variable to see",
choices = names(dataset_to_use()),
multiple = TRUE))
})
output$look_dataset<-renderUI({
actionButton("run_analysis",
label = "Run Analysis")
})
dataset_filtered <- eventReactive(input$run_analysis, {
dataset_to_use() %>%
dplyr::select(input$filter_to_analyze)
})
output$last_datatable<-renderUI({
DT::dataTableOutput("datatable_2")
})
output$datatable_2<-renderDT({
DT::datatable(dataset_filtered())
})
}
shinyApp(ui, server)