R/pithon.call.R

Defines functions pithon.call

Documented in pithon.call

# Code from rPython 0.0-5, by Carlos J. Gil Bellosta
# Small modifications by Jori Liesenborgs

pithon.call <- function( py.foo, ..., simplify = TRUE, as.is = FALSE, instance.name = "" ){

    foo.args <- list( ... )

    if( is.null( names( foo.args ) ) )
        which.dict <- rep( FALSE, length( foo.args ) )
    else
        which.dict <- names( foo.args ) != ""

    n.args.vect <- sum( !which.dict )
    n.args.dict <- sum(  which.dict )

    #foo.args.dict <- toJSON( foo.args[  which.dict ] )
    #foo.args.vect <- toJSON( foo.args[ !which.dict ] )

    foo.args.dict <- toJSON( foo.args[  which.dict ], collapse = " ", asIs = as.is, digits = 32 )
    foo.args.vect <- toJSON( foo.args[ !which.dict ], collapse = " ", asIs = as.is, digits = 32 )

    # Creating the call

    python.command <- c( 
        paste( "_r_args_dict =r'''", foo.args.dict, "'''", sep = "" ),
        paste( "_r_args_vect =r'''", foo.args.vect, "'''", sep = "" ),
        "_r_args_dict = json.loads( _r_args_dict )",
        "_r_args_vect = json.loads( _r_args_vect )",
        python.command <- paste( "_r_call_return = ", py.foo, "(",
                                  ifelse( n.args.vect == 1, "_r_args_vect[0]", "*_r_args_vect" ),
                                  ifelse( n.args.dict == 0, ")", ", **_r_args_dict)" ), 
                                  sep = "" )
    )

    python.command <- paste( python.command, collapse = "\n" )

    instname <- instance.name
    pithon.exec( python.command, TRUE, instance.name )                       # Checks for errors
    ret <- pithon.get( "_r_call_return",  instance.name=instname)

    if( length( ret ) == 1 && simplify ) ret <- ret[[1]]

    ret
}

Try the rPithon package in your browser

Any scripts or data that you put into this service are public.

rPithon documentation built on May 2, 2019, 5:51 p.m.