我正在使用RShiny设置一个交互式仪表板。目的是允许选定的用户输入带有指定参数估计值的文件。我已经在预先存在的R Script文件中开发了一个回归模型。我的计划是使用此预先存在的模型,使用用户已上传的参数来预测因变量。 以下站点特别有用:https://datascience-enthusiast.com/R/shiny_ML.html。但是,当我尝试使用load()命令加载保存的模型(.rda)并使用source()引用模型来自(.R)的脚本时,我的代码崩溃了。
我收到以下错误 “警告文件(文件名,“ r”,编码=编码): 无法打开文件“ PolyFunct.R”:没有此类文件或目录 警告:文件错误:无法打开连接 [没有可用的堆栈跟踪]”
我尝试使用ShinyServer函数上方及其内部的load()和Source()。两者都不起作用。我试图将预先存在的模型作为单独的save R脚本和ShinyServer函数之上的函数包括在内。帮助将不胜感激。
ETL = function(){
setwd("mydirectory")
fulldata <- read_excel("NoFines.xlsx")
finesdata <- read_excel("Model_Param.xlsx")
fulldata <- data.frame(fulldata)
finesdata <- data.frame(finesdata)
fulldata$FinesPerc <- NULL
fulldata$FinesDirectT <- NULL
return(fulldata)
return(finesdata)
} ##end of ETL function
PolyFunct = function(fulldata,finesdata){
library(caret)
library(leaps)
library(MASS)
library(DMwR)
mydata1 <- fulldata
mydata2 <- finesdata
mydata1$Comments <- NULL
mydata1$Date <- NULL
mydata2$Comments <- NULL
mydata2$Date <- NULL
ggpairs(mydata2)
#polynomial
PolyMod <- lm(mydata2$Recovery ~
polym(mydata2$007T,mydata2$007G,mydata2$FinesPerc, degree=2, raw=FALSE))
summary(PolyMod)
par(mfrow=c(2,2)) # init 4 charts in 1 panel
plot(PolyMod)
save(PolyMod , file = 'PolyRegression2.rda')
return(PolyMod)
}
ETL()
PolyFunct(fulldata,finesdata)
```
#setwd("mydirectory")
#load("PolyRegression.rda") # Load saved model
#source("PolyOnly.R")
library(shiny)
shinyServer(function(input, output) {
options(shiny.maxRequestSize = 800*1024^2)
output$sample_input_data_heading = renderUI({ # show only if data has
been uploaded
inFile <- input$file1
if (is.null(inFile)){
return(NULL)
}else{
tags$h4('Sample data')
}
}) #end show data upload
output$sample_input_data = renderTable({ # show sample of uploaded data
inFile <- input$file1
if (is.null(inFile)){
return(NULL)
}else{
input_data = readr::read_csv(input$file1$datapath, col_names = TRUE)
colnames(input_data) = c("007T","007G","FinesPerc")
head(input_data)
}
}) #end show sample of uploaded data
predictions<-reactive({
load("PolyRegression.rda") # Load saved model
source("PolyFunct.R",encoding="utf-8",local = TRUE)
inFile <- input$file1
if (is.null(inFile)){
return(NULL)
}else{
withProgress(message = 'Predictions in progress. Please wait ...', {
input_data = readr::read_csv(input$file1$datapath, col_names = TRUE)
colnames(input_data) = c("007T","C007G","FinesPerc")
input_data$Recovery = predict(PolyMod, input_data)
})
}
})## end of predictions
output$sample_prediction_heading = renderUI({ # show only if data has been uploaded
inFile <- input$file1
if (is.null(inFile)){
return(NULL)
}else{
tags$h4('Sample predictions')
}
}) ## end of sample prediction headings
output$sample_predictions = renderTable({ # the last 6 rows to show
pred = predictions() ##call the reactive function above where predictions are made
head(pred)
})
# Downloadable csv of predictions
output$downloadData <- downloadHandler(
filename = function() {
paste("input_data_with_predictions", ".csv", sep = "")
},
content = function(file) {
write.csv(predictions(), file, row.names = FALSE)
})
})