简体   繁体   中英

Load and delete a data set within environment through Shiny in R

I am trying to add a feature into my shiny app which allows the user to load a data set from their device into the R workspace environment, so that I can swap between different data sets for analysis. My secondary aim is to have a button in the UI which deletes a selected data set from the workspace environment.

In this example, I load the mtcars and USArrests data sets into the environment; these will appear in both the selectInput functions and will render in to the UI when either is selected. I save the airquality one as a csv file and then load it into the workspace via the fileInput function.

Here is what I have thus far:-

UI:


#load some data into the workspace
mtcars<-as.data.frame(mtcars)
USArrests<-as.data.frame(USArrests)

#then save one as a csv for the example
write.csv(airquality, file="airquality.csv")



ui<-fluidPage(
  titlePanel('Minimal example'),
  tabsetPanel(


tabPanel("Data analysis", 
         
         
         fluidPage(
           titlePanel('Data Analysis'),
           
           fluidRow(
             sidebarPanel(
               fileInput("c_file","Upload your csv file",
                         accept = c(
                           "text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")
               ),
               selectInput("data_switch","Select a dataframe in your workspace to analyse", 
                           choices =ls()[sapply(ls(), function(i) class(get(i))) =="data.frame"]), 
               #works fine, allows me to cycle through the data in my workspace
               selectInput("data_delete", "Select a dataframe in your workspace to delete",
                           choices = ls()[sapply(ls(), function(i) class(get(i))) =="data.frame"]),
               #works fine, allows me to cycle through the data in my workspace
               actionButton("deletedata", "Delete"),
               tags$hr(),
               ))),
         
         mainPanel(width = 6,
                   h4("Here's your data"),
                   textOutput("selected_df"),
                   dataTableOutput("view")))#renders the dataframe which has been selected in data_switch


))#end of ui

Server:

server<-function(input,output,session){
  
  
  
  
  
  filedata <- reactive({
    infile <- input$c_file
    if (is.null(infile)) {
      
      return(NULL)
    }
    read.csv(infile$datapath)
  })
  
  
  observeEvent(input$deletedata,{
    
    rm(input$data_delete)
    
  })


  
  output$view <-
  renderDataTable({
    as.data.frame(get(input$data_switch)) # this should render the selected dataframe. If you replace this with mtcars then that dataset is correctly rendered.
  })





}

shinyApp(ui,server)

Now my problem is two-fold. I can't get the airquality data set to load into the R workspace environment when I upload the csv file. Secondly, when I attempt to delete a file from the workspace via my "Delete" actionButton , the app just crashes.

I realise that my server code is horrendous but I am hoping that it only needs an extra few lines of code. Any help or pointers would be really appreciated. Thank you in advance:)

Try this in your server function:

observeEvent(input$deletedata,{
    
    #rm(input$data_delete)
    output$view <-
      renderDataTable({
        return(NULL) # this should delete the selected dataframe. 
      })
    
  })

You cannot delete input$data_delete .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM