连接R和PHP

时间:2018-12-02 01:41:44

标签: php mysql r

我正在尝试将php脚本连接到使用软件包的R文件,特别是插入符号,MASS和RMySql。这是一个机器学习脚本。我在运行PHP时遇到问题,并不断收到此错误:

Error in library(caret) : there is no package called 'caret' Execution halted

这是我的R脚本

#setwd('W:/www/machlearn/')
#install.packages('caret')
#nstall.packages('MASS')
library(caret)
library(MASS)
library(RMySQL)
#setwd('W:/www/machlearn/')


args <- commandArgs(TRUE)
total_capacity<-as.numeric(args[1])
wh_loading_docks<-as.numeric(args[2])
wh_temp_control<-as.numeric(args[3])
wh_security<-as.numeric(args[4])
wh_24hr_access<-as.numeric(args[5])
result <- NULL

#initially selecting the data to create prediction model to use for machine learning
connection <- dbConnect(MySQL(), user='g1090445', password='33224sql', dbname='g1090445', host='mydb.itap.purdue.edu')
query <- "select wh_loading_docks, total_capacity, wh_temp_control, wh_security, wh_24hr_access, wh_price from fake_Warehouse"
TotalWH <- as.data.frame(dbFetch(dbSendQuery(connection, query), n=-1))
TotalWH$wh_price <- price_range_convert(TotalWH$wh_price)
#prediction model
final_model <- lda(wh_price ~ ., data = TotalWH)
dbDisconnect(connection)

machine_learning <- function(total_capacity, wh_loading_docks, wh_temp_control, wh_security, wh_24hr_access){
  #library(caret)
  #library(MASS)
  #warehouse data from database
  connection <- dbConnect(MySQL(), user='g1090445', password='33224sql', dbname='g1090445', host='mydb.itap.purdue.edu')
  query <- "select wh_loading_docks, total_capacity, wh_temp_control, wh_security, wh_24hr_access, wh_price from fake_Warehouse"
  TotalWH <- as.data.frame(dbFetch(dbSendQuery(connection, query), n=-1))
  #converting prices to ranges
  TotalWH$wh_price <- price_range_convert(TotalWH$wh_price)
  #making price prediction with the lda model
  price_prediction <- predict(final_model, data.frame(wh_loading_docks=wh_loading_docks, total_capacity=total_capacity, wh_temp_control=wh_temp_control, wh_security=wh_security, wh_24hr_access=wh_24hr_access))
  #updating fit.lda function
  new_data <- data.frame(wh_loading_docks=wh_loading_docks, total_capacity=total_capacity, wh_temp_control=wh_temp_control, wh_security=wh_security, wh_24hr_access=wh_24hr_access, wh_price=price_prediction$class)
  TotalWH <- rbind(TotalWH, new_data)
  final_model <- update(final_model) 
  result <- price_prediction$class
  result <<- result
  #return(price_prediction$class)
  #result <- price_prediction$class
  #closing connection to database
  dbDisconnect(connection)
  #print(paste(result))
  #Result3 <- as.array(paste(result))
  #dev.off

  #IF TOO MANY CONNECTIONS RUN THE CODE BELOW
  #all_cons <- dbListConnections(MySQL())
  #for(con in all_cons)
  #{
  #  dbDisconnect(con)
  #}
  #sink('result.txt', append=FALSE)
  #paste(result)
  #dev.off('result.txt')
}
sink('result.txt', append=FALSE)
#paste(result)
#dev.off('result.txt')
sink()

这是我的PHP脚本

<?php
    if(isset($_GET['submit']))    
    {
       // insert drive data here
       $total_cap = $_GET['total_capacity'];
       $wh_loading_dock = $_GET['wh_loading_dock'];
       $wh_temp_control = $_GET['wh_temp_control'];
       $wh_security = $_GET['wh_security'];
       $wh_24hr = $_GET['wh_24hr'];




     echo $output = shell_exec("Rscript final_machine_learning.R  $total_cap 
$wh_loading_dock $wh_temp_control $wh_security $wh_24hr 2>&1");
           echo file_get_contents("result.txt"); 
        }    
        ?>

如果其中包含函数,如何使PHP运行R脚本。我是学生,对R或服务器没有管理权限。

0 个答案:

没有答案