rpy2 : Korean characters are not working on rpy2

自作多情 提交于 2019-12-12 12:16:39

问题


python code:

import rpy2.robjects as robjects

rCommand='''gender <- c("남자", "남자", "남자", "여자", "여자", "여자", "여자", "여자")
  age    <- c(23, 25, 27, 29, 31, 33, 35, 37)
  outdf <- data.frame(gender, age)
'''
robjects.r(rCommand)
resultDf_r=robjects.globalenv["outdf"]

print type(resultDf_r)

Korean characters makes the python.exe killed.

In R command terminal, "rCommand" above works well.

I couldn't find any solution.

Any help would be appreciated.

my env: window 7 x64, python 2.7.8 x64, rpy2 2.5.4, R 3.1.2


回答1:


At the time of writing, there is unfortunately no official support of rpy2 for windows. The code snippet you are providing is working fine on Linux.

Your options might be:

  • run your code in a Linux VM (or container - MS has announced to be backing Docker)

  • submit a patch for rpy2

  • file a bug report on the bitbucket page for rpy2 hoping that this translates into a patch by someone

Edit: The comments suggest that helping out Python 2.7 with encoding might help (no promise - everything is working on Linux so this might be Windows-specific). A string can be explicitly specified to be in unicode with (note the prefix u before '''):

rCommand=u'''
    gender <- c("남자", "남자", "남자", "여자", "여자", "여자", "여자", "여자")
    age    <- c(23, 25, 27, 29, 31, 33, 35, 37)
    outdf <- data.frame(gender, age)
'''
robjects.r(rCommand)


来源:https://stackoverflow.com/questions/28247851/rpy2-korean-characters-are-not-working-on-rpy2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!