call python with system() in R to run a python script emulating the python console

后端 未结 3 1530
感情败类
感情败类 2020-12-31 11:19

I want to pass a chunk of Python code to Python in R with something like system(\'python ...\'), and I\'m wondering if there is an easy way to emulate the pytho

相关标签:
3条回答
  • 2020-12-31 11:52

    The system command has an option called intern = FALSE. Make this TRUE and Whatever output was just visible before, will be stored in a variable.

    Now run your system command with this option and you should get your output directly in your variable. Like this

    tmp <- system("python -c 'print \"hello world\"'",intern=T)
    
    0 讨论(0)
  • 2020-12-31 11:56

    Do you mean something like this?

    export NUM=10
    R -q -e "rnorm($NUM)"
    

    You might also like to check out littler - http://dirk.eddelbuettel.com/code/littler.html

    UPDATED

    Following your comment below, I think I am beginning to understand your question better. You are asking about running python inside the R shell.

    So here's an example:-

    # code in a file named myfirstpythonfile.py
    
    a = 1 
    b = 19
    c = 3 
    mylist = [a, b, c]
    for item in mylist:
        print item
    

    In your R shell, therefore, do this:

    > system('python myfirstpythonfile.py')
    1
    19
    3
    

    Essentially, you can simply call python /path/to/your/python/file.py to execute a block of python code.

    In my case, I can simply call python myfirstpythonfile.py assuming that I launched my R shell in the same directory (path) my python file resides.

    FURTHER UPDATED

    And if you really want to print out the source code, here's a brute force method that might be possible. In your R shell:-

    > system('python -c "import sys; sys.stdout.write(file(\'myfirstpythonfile.py\', \'r\').read());"; python myfirstpythonfile.py')
    a = 1
    b = 19
    c = 3
    mylist = [a, b, c]
    for item in mylist:
        print item
    1
    19
    3
    

    AND FURTHER FURTHER UPDATED :-)

    So if the purpose is to print the python code before the execution of a code, we can use the python trace module (reference: http://docs.python.org/library/trace.html). In command line, we use the -m option to call a python module and we specify the options for that python module following it.

    So for my example above, it would be:-

    $ python -m trace --trace myfirstpythonfile.py
     --- modulename: myfirstpythonfile, funcname: <module>
    myfirstpythonfile.py(1): a = 1
    myfirstpythonfile.py(2): b = 19
    myfirstpythonfile.py(3): c = 3
    myfirstpythonfile.py(4): mylist = [a, b, c]
    myfirstpythonfile.py(5): for item in mylist:
    myfirstpythonfile.py(6):     print item
    1
    myfirstpythonfile.py(5): for item in mylist:
    myfirstpythonfile.py(6):     print item
    19
    myfirstpythonfile.py(5): for item in mylist:
    myfirstpythonfile.py(6):     print item
    3
    myfirstpythonfile.py(5): for item in mylist:
     --- modulename: trace, funcname: _unsettrace
    trace.py(80):         sys.settrace(None)
    

    Which as we can see, traces the exact line of python code, executes the result immediately after and outputs it into stdout.

    0 讨论(0)
  • 2020-12-31 11:57

    My work around for this problem is defining my own functions that paste in parameters, write out a temporary .py file, and them execute the python file via a system call. Here is an example that calls ArcGIS's Euclidean Distance function:

    py.EucDistance = function(poly_path,poly_name,snap_raster,out_raster_path_name,maximum_distance,mask){
    
        py_path = 'G:/Faculty/Mann/EucDistance_temp.py'
        poly_path_name = paste(poly_path,poly_name, sep='')
    
        fileConn<-file(paste(py_path))
        writeLines(c(
            paste('import arcpy'),
            paste('from arcpy import env'),
            paste('from arcpy.sa import *'),
            paste('arcpy.CheckOutExtension("spatial")'),
    
            paste('out_raster_path_name = "',out_raster_path_name,'"',sep=""),
            paste('snap_raster = "',snap_raster,'"',sep=""),
            paste('cellsize =arcpy.GetRasterProperties_management(snap_raster,"CELLSIZEX")'),
            paste('mask = "',mask,'"',sep=""),
            paste('maximum_distance = "',maximum_distance,'"',sep=""),
            paste('sr = arcpy.Describe(snap_raster).spatialReference'),
    
            paste('arcpy.env.overwriteOutput = True'),
            paste('arcpy.env.snapRaster = "',snap_raster,'"',sep=""),
            paste('arcpy.env.mask = mask'),
            paste('arcpy.env.scratchWorkspace ="G:/Faculty/Mann/Historic_BCM/Aggregated1080/Scratch.gdb"'),
            paste('arcpy.env.outputCoordinateSystem = sr'),
    
    
            # get spatial reference for raster and force output to that
            paste('sr = arcpy.Describe(snap_raster).spatialReference'),
            paste('py_projection = sr.exportToString()'),     
            paste('arcpy.env.extent = snap_raster'),
            paste('poly_name = "',poly_name,'"',sep=""),
            paste('poly_path_name = "',poly_path_name,'"',sep=""),
    
            paste('holder = EucDistance(poly_path_name, maximum_distance, cellsize, "")'),
            paste('holder = SetNull(holder < -9999, holder)'),
            paste('holder.save(out_raster_path_name) ')
    
        ), fileConn, sep = "\n")
        close(fileConn)
    
        system(paste('C:\\Python27\\ArcGIS10.1\\python.exe', py_path))
    }
    
    0 讨论(0)
提交回复
热议问题