How to create a Load Dataset action button in shiny

We have a Shiny app that allows users to visualize data interactively. The app's code was written so that it can operate on multiple datasets. However, only one dataset can be loaded at any given time. The datasets are in a directory called "data", which is located in the same directory as our app.R file. In the "data" directory, there is one .RData file per dataset. Each .RData file is composed of multiple objects (some lists, some data frames). Because only one dataset should be loaded at any given time, we need a pull-down menu that allows users to select their dataset of choice, and an action button that will do two things: unload the data from the previously loaded dataset, and load the .RData file for the selected dataset. After much reading and browsing, I have not been able to come up with a good solution to this, and I sincerely hope that one or more of you will help us solve this problem.

Although there will be many datasets in the actual app, for this representative example I have only two .RData files in the "data" directory: one is called Xfiles.RData and the other is called Yfiles. RData. Also for the purpose of this example, each of these .RData files is composed of a single list object, called theXlist and theYlist, respectively. The goal is to create a "Load dataset" button that will allow users to switch between these two datasets, as described above. This is the closest that I have come up to a solution:

# Load Shiny:
library(shiny)

# Define the user interface:
ui <- fluidPage(
  titlePanel(h4("How can we create a 'Load Dataset' button?", align = "center")),
  br(),
  
  fluidRow(
    column(12, align = "center",
           ##### Add a control widget for the user to select the dataset to be loaded:
           selectInput("select_dataset", label = "Select a dataset",
                       choices = list('X' = c("Choose dataset X",""), 'Y' = c("Choose dataset Y","")),
                       multiple = FALSE
                       )
    ) # close column
  ), # close fluidRow
  
  br(),
  
  fluidRow(
    column(12, align = "center",
           ##### Add an action button for the user to load the selected dataset (and unload the datat from the previous dataset):
           actionButton("load_dataset", label = "Load dataset", style="color: white; background-color: #262626")
    ) # close column
  ), # close fluidRow
) # close fluidPage

# Define the server function
server <- function(input,output,session){

  # Create a reactive object that will convert the user choice into the name of the corresponding file in the "data" directory 
  SelDS <- reactive({switch(input$select_dataset,
                                      "Choose dataset X" = "Xfiles.RData",
                                      "Choose dataset Y" = "Yfiles.RData")})
  
  # When the action button is pressed, remove the file from the previously loaded dataset (if present), then load the new dataset's .RData file:
  
  observeEvent(input$load_dataset, {
    rm(list=ls(pattern="list"))
    filepath <- paste0("data/", SelDS())
    load(filepath)
  }) # close observeEvent
  
} # close server function
  
shinyApp(ui=ui,server=server)

My specific questions are two:

  1. Why is the observeEvent() function above not doing what I need it to do (remove data from previous dataset and upload data from newly selected dataset)? It seems to me like the type of commands that I can write in the second argument of observeEvent() is limited: things like rm() and ls() don't seem to work as expected.

  2. Assuming that a Load Dataset button like the one I'm thinking of is possible, is it possible (and is it better) to load the .RData files to a separate environment so that, rather than choosing which files to remove upon changing datasets, I can then simply remove all the contents of that environment with something like rm(list=ls(), envir = <new.environment>)?

Any advice and guidance would be greatly appreciated.