Run a Python code from R

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Run a Python code from R

Nelly Reduan
Hello,


How can I run this Python code from R ?


>>> import nlmpy
>>> nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
>>> nlmpy.exportASCIIGrid("raster.asc", nlm)


Nlmpy is a Python package to build neutral landscape models

https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.


path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"

test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))

Thanks a lot for your help.
Nell


nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy>
pypi.python.org
NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Run a Python code from R

Wensui Liu
take a look at rpython or rPithon package

On Wed, Nov 16, 2016 at 4:53 PM, Nelly Reduan <[hidden email]> wrote:

> Hello,
>
>
> How can I run this Python code from R ?
>
>
>>>> import nlmpy
>>>> nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
>>>> nlmpy.exportASCIIGrid("raster.asc", nlm)
>
>
> Nlmpy is a Python package to build neutral landscape models
>
> https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.
>
>
> path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"
>
> test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))
>
> Thanks a lot for your help.
> Nell
>
>
> nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy>
> pypi.python.org
> NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Run a Python code from R

Nelly Reduan
Thank you very much for your help !


I 'm trying to use the package "rPithon" but I obtain this error message:


> if (pithon.available())

+ {

+   nRow <-50

+   nCol <-50

+   h <- 0.75

+

+   # this file contains the definition of function concat

+   pithon.load("C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py")

+   pithon.call( "mpd", nRow, nCol, h)

+

+ } else {

+   print("Unable to execute python")

+ }

 Show Traceback



 Rerun with Debug



Error in pithon.get("_r_call_return", instance.name = instname) :

  Couldn't retrieve variable: Traceback (most recent call last):

  File "C:/Users/Documents/R/win-library/3.3/rPithon/pythonwrapperscript.py", line 110, in <module>

    reallyReallyLongAndUnnecessaryPrefix.data = json.dumps([eval(reallyReallyLongAndUnnecessaryPrefix.argData)])

  File "C:\Users\ANACON~1\lib\json\__init__.py", line 244, in dumps

    return _default_encoder.encode(obj)

  File "C:\Users\ANACON~1\lib\json\encoder.py", line 207, in encode

    chunks = self.iterencode(o, _one_shot=True)

  File "C:\Users\ANACON~1\lib\json\encoder.py", line 270, in iterencode

    return _iterencode(o, 0)

  File "C:\Users\ANACON~1\lib\json\encoder.py", line 184, in default

    raise TypeError(repr(o) + " is not JSON serializable")

TypeError: array([[ 0.36534654,  0.31962481,  0.44229946, ...,  0.11513079,

         0.07156331,  0.00286971],

       [ 0.41534291,  0.41333479,  0.48118995, ...,  0.19203674,

         0.04192771,  0.03679473],

       [ 0.5188





Nell


________________________________
De : Wensui Liu <[hidden email]>
Envoy� : mercredi 16 novembre 2016 16:00:03
� : Nelly Reduan
Cc : [hidden email]
Objet : Re: [R] Run a Python code from R

take a look at rpython or rPithon package

On Wed, Nov 16, 2016 at 4:53 PM, Nelly Reduan <[hidden email]> wrote:

> Hello,
>
>
> How can I run this Python code from R ?
>
>
>>>> import nlmpy
>>>> nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
>>>> nlmpy.exportASCIIGrid("raster.asc", nlm)
>
>
> Nlmpy is a Python package to build neutral landscape models
>
> https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.
>
>
> path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"
>
> test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))
>
> Thanks a lot for your help.
> Nell
>
>
> nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy>
> pypi.python.org
> NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Run a Python code from R

David Winsemius

> On Nov 16, 2016, at 4:53 PM, Nelly Reduan <[hidden email]> wrote:
>
> Thank you very much for your help !
>
>
> I 'm trying to use the package "rPithon" but I obtain this error message:

Are you sure you are not just misspelling rPython? If that's not the issue than you need to say where you got rPithon,


>
>
>> if (pithon.available())
>
> + {
>
> +   nRow <-50
>
> +   nCol <-50
>
> +   h <- 0.75
>
> +
>
> +   # this file contains the definition of function concat
>
> +   pithon.load("C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py")
>
> +   pithon.call( "mpd", nRow, nCol, h)
>
> +
>
> + } else {
>
> +   print("Unable to execute python")
>
> + }
>
> Show Traceback
>
>
>
> Rerun with Debug
>
>
>
> Error in pithon.get("_r_call_return", instance.name = instname) :
>
>  Couldn't retrieve variable: Traceback (most recent call last):
>
>  File "C:/Users/Documents/R/win-library/3.3/rPithon/pythonwrapperscript.py", line 110, in <module>
>
>    reallyReallyLongAndUnnecessaryPrefix.data = json.dumps([eval(reallyReallyLongAndUnnecessaryPrefix.argData)])
>
>  File "C:\Users\ANACON~1\lib\json\__init__.py", line 244, in dumps
>
>    return _default_encoder.encode(obj)
>
>  File "C:\Users\ANACON~1\lib\json\encoder.py", line 207, in encode
>
>    chunks = self.iterencode(o, _one_shot=True)
>
>  File "C:\Users\ANACON~1\lib\json\encoder.py", line 270, in iterencode
>
>    return _iterencode(o, 0)
>
>  File "C:\Users\ANACON~1\lib\json\encoder.py", line 184, in default
>
>    raise TypeError(repr(o) + " is not JSON serializable")
>
> TypeError: array([[ 0.36534654,  0.31962481,  0.44229946, ...,  0.11513079,
>
>         0.07156331,  0.00286971],
>
>       [ 0.41534291,  0.41333479,  0.48118995, ...,  0.19203674,
>
>         0.04192771,  0.03679473],
>
>       [ 0.5188
>
>
>
>
>
> Nell
>
>
> ________________________________
> De : Wensui Liu <[hidden email]>
> Envoy� : mercredi 16 novembre 2016 16:00:03
> � : Nelly Reduan
> Cc : [hidden email]
> Objet : Re: [R] Run a Python code from R
>
> take a look at rpython or rPithon package
>
> On Wed, Nov 16, 2016 at 4:53 PM, Nelly Reduan <[hidden email]> wrote:
>> Hello,
>>
>>
>> How can I run this Python code from R ?
>>
>>
>>>>> import nlmpy
>>>>> nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
>>>>> nlmpy.exportASCIIGrid("raster.asc", nlm)
>>
>>
>> Nlmpy is a Python package to build neutral landscape models
>>
>> https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.
>>
>>
>> path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"
>>
>> test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))
>>
>> Thanks a lot for your help.
>> Nell
>>
>>
>> nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy>
>> pypi.python.org
>> NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Run a Python code from R

Robert Baer
 From https://www.r-bloggers.com/rpithon-vs-rpython/

<https://www.r-bloggers.com/rpithon-vs-rpython/>"Similar to rPython, the
rPithon package (http://rpithon.r-forge.r-project.org) allows users to
execute Python code from R and exchange the data between Python and R.
However, the underlying mechanisms between these two packages are
fundamentally different. Wihle rPithon communicates with Python from R
through pipes, rPython accomplishes the same task with json. A major
advantage of rPithon over rPython is that multiple Python processes can
be started within a R session. However, rPithon is not very robust while
exchanging large data objects between R and Python."


On 11/16/2016 7:10 PM, David Winsemius wrote:
>> On Nov 16, 2016, at 4:53 PM, Nelly Reduan <[hidden email]> wrote:
>>
>> Thank you very much for your help !
>>
>>
>> I 'm trying to use the package "rPithon" but I obtain this error message:
> Are you sure you are not just misspelling rPython? If that's not the issue than you need to say where you got rPithon,

 From https://www.r-bloggers.com/rpithon-vs-rpython/

"Similar to rPython, the rPithon package
(http://rpithon.r-forge.r-project.org) allows users to execute Python
code from R and exchange the data between Python and R. However, the
underlying mechanisms between these two packages are fundamentally
different. While rPithon communicates with Python from R through pipes,
rPython accomplishes the same task with json. A major advantage of
rPithon over rPython is that multiple Python processes can be started
within a R session. However, rPithon is not very robust while exchanging
large data objects between R and Python."


>> On Wed, Nov 16, 2016 at 4:53 PM, Nelly Reduan <[hidden email]> wrote:
>>> Hello,
>>>
>>>
>>> How can I run this Python code from R ?
>>>
>>>
>>>>>> import nlmpy
>>>>>> nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
>>>>>> nlmpy.exportASCIIGrid("raster.asc", nlm)
>>>
>>> Nlmpy is a Python package to build neutral landscape models
>>>
>>> https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.
>>>
>>>
>>> path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"
>>>
>>> test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))
>>>
>>> Thanks a lot for your help.
>>> Nell
>>>
>>>
>>> nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy>
>>> pypi.python.org
>>> NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...
>>>
>>>
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>>
>>> David Winsemius
>>> Alameda, CA, USA
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Run a Python code from R

Nelly Reduan

Hello,



Thanks a lot for your answers. Finally, I'm using the function system2() to run the python script. However, I don't know how to define a list of numeric values in the character vector of arguments for the function system2 (i.e., in �args� of the function system2() ) ?



In the Python script, the list of numeric values is defined as follows:

import sys

import nlmpy



weights = sys.argv[1] ## where weights = [0.5, 0.5]



outputNLM = nlmpy.classifyArray(�, weights)



print outputNLM



In the script R, how can I specify the argument weights = [0.5, 0.5] to run the Python script ?



Thanks a lot for your time.

Have a nice day

Nell


________________________________
De : Robert Baer <[hidden email]>
Envoy� : samedi 19 novembre 2016 16:05:13
� : David Winsemius; Nelly Reduan
Cc : [hidden email]
Objet : Re: [R] Run a Python code from R


>From https://www.r-bloggers.com/rpithon-vs-rpython/

<https://www.r-bloggers.com/rpithon-vs-rpython/>"Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execute Python code from R and exchange the data between Python and R. However, the underlying mechanisms between these two packages are fundamentally different. Wihle rPithon communicates with Python from R through pipes, rPython accomplishes the same task with json. A major advantage of rPithon over rPython is that multiple Python processes can be started within a R session. However, rPithon is not very robust while exchanging large data objects between R and Python."

On 11/16/2016 7:10 PM, David Winsemius wrote:




On Nov 16, 2016, at 4:53 PM, Nelly Reduan <[hidden email]><mailto:[hidden email]> wrote:

[[elided Hotmail spam]]


I 'm trying to use the package "rPithon" but I obtain this error message:



Are you sure you are not just misspelling rPython? If that's not the issue than you need to say where you got rPithon,


>From https://www.r-bloggers.com/rpithon-vs-rpython/

"Similar to rPython, the rPithon package (http://rpithon.r-forge.r-project.org) allows users to execute Python code from R and exchange the data between Python and R. However, the underlying mechanisms between these two packages are fundamentally different. While rPithon communicates with Python from R through pipes, rPython accomplishes the same task with json. A major advantage of rPithon over rPython is that multiple Python processes can be started within a R session. However, rPithon is not very robust while exchanging large data objects between R and Python."


On Wed, Nov 16, 2016 at 4:53 PM, Nelly Reduan <[hidden email]><mailto:[hidden email]> wrote:


Hello,


How can I run this Python code from R ?




import nlmpy
nlm = nlmpy.mpd(nRow=50, nCol=50, h=0.75)
nlmpy.exportASCIIGrid("raster.asc", nlm)




Nlmpy is a Python package to build neutral landscape models

https://pypi.python.org/pypi/nlmpy . The example comes from this website. I tried to use the function system2 but I don't know how to use it.


path_script_python <- "C:/Users/Anaconda2/Lib/site-packages/nlmpy/nlmpy.py"

test <- system2("python", args = c(path_script_python, as.character(nRow), as.character(nCol), as.character(h)))

Thanks a lot for your help.
Nell


nlmpy 0.1.3 : Python Package Index<https://pypi.python.org/pypi/nlmpy><https://pypi.python.org/pypi/nlmpy>
pypi.python.org
NLMpy. NLMpy is a Python package for the creation of neutral landscape models that are widely used in the modelling of ecological patterns and processes across ...



       [[alternative HTML version deleted]]


David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email]<mailto:[hidden email]> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.