Passing a Python variable to R using rpy2

后端 未结 2 411
遥遥无期
遥遥无期 2021-01-27 07:41

I have basic R script that performs a GLM on a MySQL dataset. This runs fine using Rscript in bash. However I would like to call it within a python script so I can add it to a l

相关标签:
2条回答
  • 2021-01-27 07:48

    Use double quotes around the "%s" and single quotes around the robjects.r string:

    robjects.r('results = fetch(dbSendQuery(mydb, "%s"))') % (sql_scores)
    

    or use the format() method:

    robjects.r('fetch(dbSendQuery(mydb, {0}))'.format(sql_scores))
    
    0 讨论(0)
  • 2021-01-27 07:55

    You can access variables in the R environment with robjects.globalenv['varname']. So an alternative way is:

    robjects.globalenv['sql_scores'] = sql_scores
    robjects.r("results = fetch(dbSendQuery(mydb, sql_scores))")
    
    0 讨论(0)
提交回复
热议问题