In R functon, I used fileName as a parameter to read and process the csv data present in this file. I used the 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, only the file name is transferred, and not the full path to the file, I do not get the output in R. So, what kind of modification do I need to do to get the exact result. I use 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") ----- ----- }
source share