Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing file name to R from javascript using Rook package

In a R functon, i used fileName as parameter to read and process the csv data present in that file. I used rook package to integrate R with javascript. In javascript i used the following code to get the file name of the imported file.

<form id='importPfForm'>
<input type='file' name='datafile' size='20'>
<input type='button' value='IMPORT' onclick='importPortfolioFunction()'/>
</form>

function importPortfolioFunction( arg ) {
    var f = document.getElementById( 'importPfForm' );
    var fileName= f.datafile.value;

    $.ajax( {
      type : "POST",
      url : 'http://localhost:'+portNo+'/custom/Ralgotree/hBasedFileImport?fileName='+fileName,
      dataType : "json",
      data : '{ "method" : "hBasedFileImport",  "clientId": "31d0c653-d7e5-44b6-98b5-8c084f99514a", "version": 0 }',
      xhrFields: {
            withCredentials: false
        },
      beforeSend : function(xhr) {},
      success : function(data, textStatus, xmLHttpRequest){
      },
      error : function(xhr, ajaxOptions, thrownError) {
      }
    });     
}

Because of this method passes only the file name instead of full file path , i wont get the output in R. So what modification i need to do to get the exact output. I am using the following R code:

s <- Rhttpd$new()
  s$add(
    name="Ralgotree",
    app=Rook::URLMap$new(
    '/hBasedFileImport' = function(env){
        req <- Rook::Request$new(env)
        params <- Utils$parse_query(env$QUERY_STRING);
        res <- Rook::Response$new(headers = list( "Content-Type"="application/json" , "Access-Control-Allow-Origin"="*"))
        res$write(toJSON(hBasedFileImport(toString(params["fileName"]))))
        res$finish()
      }
 )
  )
  s$start(port = 9000)


hBasedFileImport <- function(fileName){
  portData <- read.csv(fileName,sep="\t")
   -----
   -----
}
like image 244
Dinoop Nair Avatar asked May 10 '13 09:05

Dinoop Nair


1 Answers

rook code

  app=Rook::URLMap$new(
      'hBasedFileImport'= function(env){
        req <- Rook::Request$new(env)
        res <- Rook::Response$new()
        if (!is.null(req$POST())){
          print("post method")
          data <- req$POST()[['file']]
          #print(data)
          Ralgotree::hBasedFileImport(toString(data$tempfile))
        }
        res$finish()
      },

JS code:

<form id="uploadform" method="post" enctype="multipart/form-data" action="http://'+ip+':'+portNo+'/custom/Ralgotree/hBasedFileImport"><font size="2"><table bgcolor="#D2DFEF"><tr><td>select file</td><td><input name="file" id="file" size="27" type="file" /></td></tr><tr><td></td><td><input type="submit" name="action" value="UPLOAD" /> <input type="button" value="CANCEL" onclick="cancelImport();"/></td></tr><tr><td></td><td><span id="status" style="display:none">uploading...</span><iframe id="target_iframe" name="target_iframe" src="" style="width:0;height:0;border:0px"></iframe></td></tr></table></font></form>
like image 162
Dinoop Nair Avatar answered Nov 20 '22 02:11

Dinoop Nair