【问题标题】:Load and delete a data set within environment through Shiny in R通过 R 中的 Shiny 在环境中加载和删除数据集
【发布时间】:2020-07-29 16:26:05
【问题描述】:

我正在尝试在我的闪亮应用程序中添加一个功能,该功能允许用户将数据集从他们的设备加载到 R 工作区环境中,以便我可以在不同的数据集之间进行交换以进行分析。我的次要目标是在 UI 中有一个按钮,用于从工作区环境中删除选定的数据集。

在本例中,我将mtcarsUSArrests 数据集加载到环境中;这些将出现在selectInput 函数中,并在选择其中任何一个时呈现在 UI 中。我将airquality 保存为csv 文件,然后通过fileInput 函数将其加载到工作区中。

这是我目前所拥有的:-

用户界面:


#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<-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)

现在我的问题有两个。上传 csv 文件时,我无法将 airquality 数据集加载到 R 工作区环境中。其次,当我尝试通过“删除”actionButton 从工作区中删除文件时,应用程序会崩溃。

我意识到我的服务器代码很糟糕,但我希望它只需要额外的几行代码。任何帮助或指示将不胜感激。提前谢谢你:)

【问题讨论】:

    标签: r shiny environment shinydashboard shinyapps


    【解决方案1】:

    在你的服务器函数中试试这个:

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

    你不能删除input$data_delete

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 2013-11-26
      • 2021-03-02
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多