通过 Shiny in R 在环境中加载和删除数据集
Load and delete a data set within environment through Shiny in R
我正在尝试将一项功能添加到我闪亮的应用程序中,该功能允许用户将数据集从他们的设备加载到 R 工作区环境中,以便我可以在不同的数据集之间交换以进行分析。我的次要目标是在 UI 中有一个按钮,用于从工作区环境中删除选定的数据集。
在此示例中,我将 mtcars
和 USArrests
数据集加载到环境中;这些将出现在 selectInput
函数中,并在选择其中一个时呈现到 UI 中。我将 airquality
保存为 csv 文件,然后通过 fileInput
函数将其加载到工作区中。
这是我目前的情况:-
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<-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
从工作区删除文件时,应用程序崩溃了。
我意识到我的服务器代码很糟糕,但我希望它只需要额外的几行代码。任何帮助或指示将不胜感激。提前谢谢你:)
在你的服务器函数中试试这个:
observeEvent(input$deletedata,{
#rm(input$data_delete)
output$view <-
renderDataTable({
return(NULL) # this should delete the selected dataframe.
})
})
您无法删除 input$data_delete
。
我正在尝试将一项功能添加到我闪亮的应用程序中,该功能允许用户将数据集从他们的设备加载到 R 工作区环境中,以便我可以在不同的数据集之间交换以进行分析。我的次要目标是在 UI 中有一个按钮,用于从工作区环境中删除选定的数据集。
在此示例中,我将 mtcars
和 USArrests
数据集加载到环境中;这些将出现在 selectInput
函数中,并在选择其中一个时呈现到 UI 中。我将 airquality
保存为 csv 文件,然后通过 fileInput
函数将其加载到工作区中。
这是我目前的情况:-
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<-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
从工作区删除文件时,应用程序崩溃了。
我意识到我的服务器代码很糟糕,但我希望它只需要额外的几行代码。任何帮助或指示将不胜感激。提前谢谢你:)
在你的服务器函数中试试这个:
observeEvent(input$deletedata,{
#rm(input$data_delete)
output$view <-
renderDataTable({
return(NULL) # this should delete the selected dataframe.
})
})
您无法删除 input$data_delete
。