parse_data#

parse_data(data, *, expand_altrep=True, altrep_constructor_dict=DEFAULT_ALTREP_MAP, extension=None)[source]#

Parse the data of a R file, received as a sequence of bytes.

Parameters:
  • data (bytes) – Data extracted of a R file.

  • expand_altrep (bool) – Whether to translate ALTREPs to normal objects.

  • altrep_constructor_dict (AltRepConstructorMap) – Dictionary mapping each ALTREP to its constructor.

  • extension (str | None) – Extension of the file.

Returns:

Data contained in the file (versions and object).

Return type:

RData

See also

parse_file(): Similar function that parses a file directly.

Examples

Parse one of the included examples, containing a vector

>>> import rdata
>>>
>>> with open(rdata.TESTDATA_PATH / "test_vector.rda", "rb") as f:
...     parsed = rdata.parser.parse_data(f.read())
>>>
>>> parsed
RData(versions=RVersions(format=2,
                         serialized=196610,
                         minimum=131840),
      extra=RExtraInfo(encoding=None),
      object=RObject(info=RObjectInfo(type=<RObjectType.LIST: 2>,
                     object=False,
                     attributes=False,
                     tag=True,
                     gp=0,
                     reference=0),
      value=(RObject(info=RObjectInfo(type=<RObjectType.REAL: 14>,
                                      object=False,
                                      attributes=False,
                                      tag=False,
                                      gp=0,
                                      reference=0),
                     value=array([1., 2., 3.]),
                     attributes=None,
                     tag=None,
                     referenced_object=None),
             RObject(info=RObjectInfo(type=<RObjectType.NILVALUE: 254>,
                                      object=False,
                                      attributes=False,
                                      tag=False,
                                      gp=0,
                                      reference=0),
                     value=None,
                     attributes=None,
                     tag=None,
                     referenced_object=None)),
             attributes=None,
             tag=RObject(info=RObjectInfo(type=<RObjectType.SYM: 1>,
                                          object=False,
                                          attributes=False,
                                          tag=False,
                                          gp=0,
                                          reference=0),
                         value=RObject(info=RObjectInfo(type=<RObjectType.CHAR: 9>,
                                                        object=False,
                                                        attributes=False,
                                                        tag=False,
                                                        gp=64,
                                                        reference=0),
                                       value=b'test_vector',
                                       attributes=None,
                                       tag=None,
                                       referenced_object=None),
                         attributes=None,
                         tag=None,
                         referenced_object=None),
             referenced_object=None))