Python Forum
Ignoring errors when using robjects. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ignoring errors when using robjects. (/thread-33523.html)



Ignoring errors when using robjects. - Rav013 - May-02-2021

I am trying to download packages for R script via python.


import rpy2.robjects as robjects

    data = robjects.r('if(!require(rmarkdown)) { install.packages("rmarkdown", repos = "https://cran.revolutionanalytics.com"); require(rmarkdown)}')
    data = robjects.r('if(!require(installr)) { install.packages("installr", repos = "https://cran.revolutionanalytics.com"); require(installr)}')
    data = robjects.r('if(!require(DT)) { install.packages("DT", repos = "https://cran.revolutionanalytics.com"); require(DT)}')
    data = robjects.r('if(!require(lubridate)) { install.packages("lubridate", repos = "https://cran.revolutionanalytics.com"); require(lubridate)}')
    data = robjects.r('if(!require(ggplot2)) { install.packages("ggplot2", repos = "https://cran.revolutionanalytics.com"); require(ggplot2)}')
    data = robjects.r('if(!require(crayon)) { install.packages("crayon", repos = "https://cran.revolutionanalytics.com"); require(crayon)}')
Everything works fine and the packages are downloading correctly but for each package I receive errors:

From cffi callback <function _consolewrite_ex at 0x0000000004D57670>:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\rpy2\rinterface_lib\callbacks.py", line 132, in _consolewrite_ex
    s = conversion._cchar_to_str_with_maxlen(buf, n, _CCHAR_ENCODING)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rpy2\rinterface_lib\conversion.py", line 133, in _cchar_to_str_with_maxlen
    s = ffi.string(c, maxlen).decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 0: invalid start byte
From cffi callback <function _consolewrite_ex at 0x0000000004D57670>:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\rpy2\rinterface_lib\callbacks.py", line 132, in _consolewrite_ex
    s = conversion._cchar_to_str_with_maxlen(buf, n, _CCHAR_ENCODING)
My question is, how can I force python to ignore those errors and not show them?


RE: Ignoring errors when using robjects. - Gribouillis - May-02-2021

The character encoding could be latin-1 (iso8859-1)
>>> x = b'\xa3'
>>> x.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 0: invalid start byte
>>> x.decode('latin-1')
'£'
There may be a way to tell the library that it must use the latin-1 encoding instead of utf-8.


RE: Ignoring errors when using robjects. - Rav013 - May-04-2021

I have no clue how to change encoding via code.

The only solution I found:

import sys
# sys.setdefaultencoding() does not exist, here!
reload(sys)  # Reload does the trick!
sys.setdefaultencoding('UTF8')

(Note for Python 3.4+: reload() is in the importlib library.)
But it does not work.

Can someone help me with that?


RE: Ignoring errors when using robjects. - Gribouillis - May-04-2021

If you look into rpy2 source code hor the callbacks.py module, you'll see that the last commit was about
Quote:Better handling of R string encodings
You could perhaps install the latest development version of rpy2 and see if it solves the problem.

Also it seems that some less drastic workarounds are described in the devs discussion about the encoding issue.