2013-05-10 5 views
6

का उपयोग कर एक आर functon में, मैं पैरामीटर के रूप में इस्तेमाल किया fileName पढ़ सकते हैं और है कि फाइल में मौजूद csv डेटा संसाधित करने के लिए। मैं rook पैकेज का इस्तेमाल किया जावास्क्रिप्ट के साथ एकीकृत करने के लिए आर। जावास्क्रिप्ट में मैं आयात की गई फ़ाइल का फ़ाइल नाम पाने के लिए निम्नलिखित कोड का इस्तेमाल किया।जावास्क्रिप्ट से आर के लिए फ़ाइल नाम गुजर रूक पैकेज

<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) { 
     } 
    });  
} 
इस पद्धति की वजह से

पूर्ण फ़ाइल पथ के बजाय केवल फ़ाइल नाम से गुजरता है, मैं अभ्यस्त आर में उत्पादन तो क्या संशोधन मैं सही उत्पादन प्राप्त करने के लिए क्या करने की जरूरत मिलता है।

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

उत्तर

9

किश्ती कोड

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() 
     }, 

जे एस कोड:

<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> 
मैं निम्नलिखित आर कोड का उपयोग कर रहा
संबंधित मुद्दे