title: Using R script with Spark
body: <p>I want to run my existing R script from Spark.</p>
  
  <p>I have setup R and Spark on my machine and trying to execute the code but i am getting exception but that is not very helpful.</p>
  
  <p>Spark Code-</p>
  
  <pre><code>String file = "/home/MSA2.R";
          SparkConf sparkConf = new SparkConf().setAppName("First App")
                  .setMaster("local[1]");
  
          @SuppressWarnings("resource")
          JavaSparkContext sparkContext = new JavaSparkContext(sparkConf);
  
          JavaRDD&lt;String&gt; rdd = sparkContext.textFile("/home/test.csv")
          .pipe(file);
  </code></pre>
  
  <p>R code - </p>
  
  <pre><code>f1 &lt;- read.csv("/home/testing.csv")
  </code></pre>
  
  <p>Exception - </p>
  
  <blockquote>
    <p>Exception in thread "main" org.apache.spark.SparkException: Job
    aborted due to stage failure: Task 0 in stage 0.0 failed 1 times, most
    recent failure: Lost task 0.0 in stage 0.0 (TID 0, localhost): 
    java.lang.IllegalStateException: Subprocess exited with status 2.
    Command ran: /home/MSA2.R
    java.util.NoSuchElementException: key not found: 1
    rg.apache.spark.rpc.RpcTimeoutException: Cannot receive any reply in 120 >seconds. This timeout is controlled by spark.rpc.askTimeout at >org.apache.spark.rpc.RpcTimeout.org$apache$spark$rpc$RpcTime‌​>out$$createRpcTimeou‌​tException(RpcTimeou‌​t.scala:48)</p>
  </blockquote>
  
  <p>There is not much in exception to debug the issue.</p>
  
  <p>Can anyone suggest if the approach is correct or not. If yes can anyone help with the issue, If no, please suggest an approach.</p>
  
  <p>Note: I don't want to use Spark R</p>
  
  <p>Reference of above code- <a href="https://www.linkedin.com/pulse/executing-existing-r-scripts-from-spark-rutger-de-graaf" rel="nofollow">https://www.linkedin.com/pulse/executing-existing-r-scripts-from-spark-rutger-de-graaf</a></p>