dqpu4988 2018-12-02 01:41
浏览 78

连接R和PHP

I am trying to connect a php script to a R file that is using packages, specifically caret, MASS, and RMySql. It is a machine learning script. I am having trouble running the PHP and am continuously getting this error:

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

Here is my R script

#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()

Here is my PHP script

<?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"); 
        }    
        ?>

How do you get PHP to run a R script if there are function in it. I am a student and don't have administrative access to R or server.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图