我正在构建一个shiny
应用程序,您可以在其中训练模型。一种功能是能够下载模型对象(在这种情况下为glm
对象),以便用户以后可以在应用程序外部使用它。我的代码的相关部分如下所示
library(shiny)
library(car)
ui <- fluidPage(
# What parameter do you wish to estimate
selectInput(inputId = "dependent_variable",
label = "Select dependent variable",
choices = c("education",
"vocabulary")),
# Download button for model
downloadButton(outputId = "download_model", label = 'Download Model')
)
server <- function(input, output){
strip_glm <- function(cm) {
cm$y <- c()
cm$model <- c()
cm$residuals <- c()
cm$fitted.values <- c()
cm$effects <- c()
cm$qr$qr <- c()
cm$linear.predictors <- c()
cm$weights <- c()
cm$prior.weights <- c()
cm$data <- c()
cm$family$variance <- c()
cm$family$dev.resids <- c()
cm$family$aic <- c()
cm$family$validmu <- c()
cm$family$simulate <- c()
attr(cm$terms,".Environment") <- c()
attr(cm$formula,".Environment") <- c()
return(cm)
}
reactive_glm_model <- reactive(glm(paste0(input$dependent_variable, "~."), data = Vocab))
stripped_glm <- reactive(strip_glm(reactive_glm_model()))
stripped_glm_summary <- reactive(summary(reactive_glm_model()))
output$download_model <- downloadHandler(
filename = function() {
"report.Rd"
},
content = function(file) {
glm_object <- stripped_glm()
glm_summary <- stripped_glm_summary()
save(glm_object, glm_summary, file = file)
}
)
}
shinyApp(ui, server)
我使用strip_glm()
函数,因为我不希望glm
对象成为too big and carry unnecessary东西。它应该只能预测。但是,通过剥离glm
,summary()
不再起作用,因此,我也想返回摘要。
所以这是我的问题:如果下载该对象,仍然会有一些“隐藏”对象使文件太大。在此reprex中,它为16.2 MB,而如果我将相应的对象加载回内存,则发现实际对象的大小要小得多
load("report.Rd")
object.size(glm_object) # 22 kB
object.size(glm_summary) # 2.5 MB
这里发生了什么?在我使用的模型中,我的数据可能具有数百万行,导致该对象为数GB,并且下载需要很长时间。
UPDATE
它似乎与版本或基础设置有关。在上述设置中,我确实遇到了使用的问题]
platform x86_64-redhat-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 5.2
year 2018
month 12
day 20
svn rev 75870
language R
version.string R version 3.5.2 (2018-12-20)
nickname Eggshell Igloo
很遗憾,由于策略限制,我无法更新R的版本
UPDATE II
似乎问题与R
或shiny
无关,并且在不同平台上均不可复制