我在R中有一个长时间运行的进程(通过Rstudio-server),我怀疑它可能存在内存问题,导致R会话崩溃。不幸的是,我无法准确监控发生的事情:存在崩溃日志,如果存在,我会在哪里找到它们?
我的设置如下:
我让程序一夜之间运行,然后回来在rstudio界面中找到以下错误消息:
之前的R会话由于意外崩溃而异常终止。 由于此次崩溃,您可能丢失了工作区数据。
以下代码似乎导致问题(不是可重现的样本)。该代码采用一系列回归公式(约250k),一个1500行乘70列的数据框,还允许您指定计算中使用的核心数:
get_models_rsquared = function(combination_formula,df,cores = 1){
if (cores == "ALL"){cores <- detectCores()}
require(parallel) #if parallel processing is not required, mclapply should be changed to lapply.
#using mclapply to calculate linear models in parallel,
#storing adjusted r-squared and number of missing rows in a list
combination_fitted_models_rsq = mclapply(seq_along(combination_formula), function(i)
list(summary(lm(data = df, combination_formula[[i]]))$adj.r.squared,
length(summary(lm(data = df, combination_formula[[i]]))$na.action)), mc.cores = cores )
#now store the adjusted r-squared and missing rows of data
temp_1 = lapply(seq_along(combination_fitted_models_rsq), function(i)
combination_fitted_models_rsq[[i]][[1]])
temp_1 = as.numeric(temp_1)
temp_2 = lapply(seq_along(combination_fitted_models_rsq), function(i)
combination_fitted_models_rsq[[i]][[2]])
temp_2 = as.numeric(temp_2)
#this is the problematic line
temp_3 = lapply(seq_along(combination_formula), function(i) {
length(attributes(terms.formula(combination_formula[[i]]))$term.labels)
}#tells you number of predictors in each formula used for linear regression
)#end lapply
result = data.frame(temp_1,temp_2,temp_3)
names(result) = c("rsquared","length.na","number_of_terms")
return(result)
}
temp_3
的计算似乎在调用函数时给出了问题。但是,如果您将temp_3
的代码移出函数并在运行函数后计算,则一切正常。