Python Forum
Ignoring errors when using robjects.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignoring errors when using robjects.
#1
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?
Reply
#2
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.
Reply
#3
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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WARNING: Ignoring invalid distribution kucingkembar 1 24,298 Sep-02-2022, 06:49 AM
Last Post: snippsat
  Ignoring a list item hank4eva 2 2,065 Aug-17-2020, 08:40 AM
Last Post: perfringo
  ignoring a signal Skaperen 4 2,645 Sep-27-2019, 07:14 PM
Last Post: Skaperen
  Ignoring non characters in a string evans 1 2,617 Mar-11-2018, 09:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020