weixin_44871335
Mouuuuuuuuu
2021-03-10 11:24
采纳率: 0%
浏览 68

python调用R语言画图,UnicodeDecodeError: 'utf-8' 问题

尝试python调用R语言绘制heatmap,

import os
os.environ["R_HOME"] = r"C:\Program Files\R\R-3.6.1"
os.environ["PATH"]   = r"C:\Program Files\R\R-3.6.1\bin\x64" + ";" + os.environ["PATH"]
from rpy2.robjects import pandas2ri, packages
pandas2ri.activate()
stats = packages.importr('stats')
import rpy2.robjects as robjects

r_script ='''
#热图
#清空暂存数据
rm(list=ls())

#载入R包
library('pheatmap')

#设置工作目录
setwd("C:/Users/shumou/Desktop/R语言组学分析SOP")

#载入数据
library('openxlsx')
data <- read.xlsx("画图数据.xlsx", encoding='UTF-8',sheet=2, rowNames = T, colNames = T,)

#构建样本分类数据
Sample_type=c(rep('FYX',3),
              rep('QX',3),
              rep('NX',3),
              rep('JX',3))
annotation_col <- data.frame(Sample_type)
rownames(annotation_col) <- colnames(data)
Compound_type=c(rep('Acid',11),
                rep('Alcohol',13),
                rep('Aldehyde',1),
                rep('Ester',12),
                rep('Ketone',1),
                rep('Pyrazine',1),
                rep('Others',3))
annotation_row <- data.frame(Compound_type)
rownames(annotation_row) <- rownames(data)

#breaks
bk <- c(seq(-9,-0.1,by=0.01),seq(0,9,by=0.01))

#绘制热图
pheatmap(data, #表达数据
         cluster_columns = T, #列聚类
         cluster_rows = F, #行聚类
         scale = "row", #标准化行
         annotation_col = annotation_col, #列样本分类数据
         annotation_row = annotation_row, #行样本分类数据
         cutree_rows = 7, #行分为7部分
         gaps_row = c(11,24,25,37,38,39,42),
         treeheight_col = 20,
         show_rownames = T, #显示行名
         show_colnames = T, #显示列名
         cellwidth = 18, cellheight = 8, #格子宽度与高度
         fontsize = 6,#字体大小
         color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/2),
                   colorRampPalette(colors = c("white","red"))(length(bk)/2)),
         border_color = 'NA', #去除描边
         angle_col = "90")

'''
robjects.r(r_script)

报错:

From cffi callback <function _consolewrite_ex at 0x000001CCF2CC3F28>:
Traceback (most recent call last):
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\callbacks.py", line 131, in _consolewrite_ex
    s = conversion._cchar_to_str_with_maxlen(buf, maxlen=n)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 130, in _cchar_to_str_with_maxlen
    s = ffi.string(c, maxlen).decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 20: invalid start byte
R[write to console]: In addition: 
R[write to console]: Warning messages:

From cffi callback <function _consolewrite_ex at 0x000001CCF2CC3F28>:
Traceback (most recent call last):
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\callbacks.py", line 131, in _consolewrite_ex
    s = conversion._cchar_to_str_with_maxlen(buf, maxlen=n)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 130, in _cchar_to_str_with_maxlen
    s = ffi.string(c, maxlen).decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 3: invalid start byte
From cffi callback <function _consolewrite_ex at 0x000001CCF2CC3F28>:
Traceback (most recent call last):
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\callbacks.py", line 131, in _consolewrite_ex
    s = conversion._cchar_to_str_with_maxlen(buf, maxlen=n)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 130, in _cchar_to_str_with_maxlen
    s = ffi.string(c, maxlen).decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 3: invalid start byte
Traceback (most recent call last):
  File "C:/Users/shumou/Desktop/dash平台开发/R-map/heatmap.py", line 65, in <module>
    robjects.r(r_script)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\robjects\__init__.py", line 416, in __call__
    res = self.eval(p)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\robjects\functions.py", line 198, in __call__
    .__call__(*args, **kwargs))
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\robjects\functions.py", line 125, in __call__
    res = super(Function, self).__call__(*new_args, **new_kwargs)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 44, in _
    cdata = function(*args, **kwargs)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface.py", line 624, in __call__
    raise embedded.RRuntimeError(_rinterface._geterrmessage())
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\_rinterface_capi.py", line 387, in _geterrmessage
    res = _string_getitem(res, 0)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\_rinterface_capi.py", line 235, in _string_getitem
    openrlib.rlib.R_CHAR(elt)
  File "C:\Users\shumou\AppData\Local\Programs\Python\Python37\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 124, in _cchar_to_str
    s = ffi.string(c).decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 20: invalid start byte

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 20: invalid start byte

是编码解码的问题么?如何解决?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • moon_and_mars
    moon_and_mars 2021-06-30 23:44

    你好!请问问题解决了吗?怎么解决的?

    点赞 评论

相关推荐