Prepare a python string for R using rpy2

后端 未结 2 1526
梦谈多话
梦谈多话 2020-12-20 08:43

This question relates to python variable to R and perhaps also to this python objects to rpy2 but none of the two completely overlaps and the first one is actually unanswere

相关标签:
2条回答
  • 2020-12-20 09:17

    Just add the strg value to the command string:

    robjects.r('''
    
    test <- gsub("to", "",''' + strg + ''')
    
    ''')
    

    or, by using .format:

    robjects.r('''
    
    test <- gsub("to", "",%s)
    
    '''.format(strg))
    

    Do note that you'll need to watch out for backslashes, see the question here

    0 讨论(0)
  • 2020-12-20 09:33

    I'd recommend you to create an function, as the R function exposed by rpy2 can be called just as if it was a Python function.

    my_func = robjects.r('''
    function(strg) {
        test <- gsub("to", "",strg)
        test
    }
    ''')
    
    my_func(strg)
    
    0 讨论(0)
提交回复
热议问题