#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A Python script that uses numpy and pyper.
"""
from __future__ import division, print_function

import os
import numpy as np
import pyper
import sys

# additional search paths
Rpaths = list()
# win
Rroot_win = "C:\\Program Files\\R"
if os.path.exists(Rroot_win):
    for append in ["", "\\bin\\i386"]:
        # This will work independent of the installed R version
        Rpaths += [ os.path.join(Rroot_win, d+append) for d in os.listdir(Rroot_win) ]
        # win frozen
        if hasattr(sys, "frozen"):
            Rroot_win_frozen = os.path.join(os.path.abspath(sys._MEIPASS), "R")
            if os.path.exists(Rroot_win_frozen):
                Rpaths += [  os.path.join(Rroot_win_frozen, d+append) for d in os.listdir(Rroot_win_frozen) ]
# linux
Rpaths += ["/usr/bin"]

print(Rpaths)

Rexes = list()
for binary in ["R", "R.exe"]:
    Rexes += [ os.path.join(loc, binary) for loc in Rpaths ]

# standard path search:
Rexe = "R"

# find other installation:
for b in Rexes:
    if os.path.exists(b):
        Rexe = b
        break
print("Using R: ", Rexe)

#Open a pyper instance
r1 = pyper.R(use_pandas=True, RCMD=Rexe) 
# Perform a simple computation
xs = np.linspace(0,1,10)
r1.assign("xs", xs) 
r1("DFRAME=data.frame(xs)")
r1("result <- DFRAME[xs]*2")   
# Retrieve the result
result = r1.get("result")
try:
    # R 3.0.4 linux
    data = result.as_matrix().reshape(-1)
except AttributeError:
    # `result` is already an ndarray?
    # R 3.2.2 windows
    data = np.array(result, dtype=float).reshape(-1)

# Validate the result
assert np.allclose(2*xs, data), "pyper does not work"

print("Everything OK: numpy and pyper are working.")

if hasattr(sys, "frozen"):
    print("Freezing of this script succeeded!")

@@ -496,24 +496,16 @@
 _win) %5D%0A
-        
 # win fr
@@ -509,24 +509,16 @@
  frozen%0A
-        
 if hasat
@@ -536,24 +536,16 @@
 ozen%22):%0A
-        
     Rroo
@@ -608,24 +608,16 @@
 ), %22R%22)%0A
-        
     if o
@@ -649,24 +649,16 @@
 rozen):%0A
-        
         
@@ -787,23 +787,8 @@
 %22%5D%0A%0A
-print(Rpaths)%0A%0A
 Rexe
@@ -894,76 +894,30 @@
  %5D%0A%0A
-# standard path search:%0ARexe = %22R%22%0A%0A# find other installation:%0A
+Rexe_avail = %5B r 
 for 
-b
+r
  in 
@@ -921,21 +921,16 @@
 in Rexes
-:%0A   
  if os.p
@@ -944,42 +944,157 @@
 sts(
-b):%0A        Rexe = b%0A        break
+r) %5D%0A%0Aprint(Rexe_avail)%0A%0A%0A# standard path search:%0Aif len(Rexe_avail) == 0:%0A    Rexe = %22R%22%0Aelse:%0A    Rexe = Rexe_avail%5B0%5D%0A%0A# find other installation:%0A
 %0Apri

update R paths