ajax - passing file name to R from javascript using Rook package -
in r functon, used filename parameter read , process csv data present in file. used rook package integrate r javascript. in javascript used following code file name of 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 method passes file name instead of full file path , wont output in r. modification need exact output. using 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") ----- ----- }
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>
Comments
Post a Comment