read_rda#

read_rda(file_or_path, *, expand_altrep=True, altrep_constructor_dict=DEFAULT_ALTREP_MAP, constructor_dict=DEFAULT_CLASS_MAP, default_encoding=None, force_default_encoding=False, global_environment=None, base_environment=None)[source]#

Read an RDA or RDATA file, containing an R object.

This is a convenience function that wraps rdata.parser.parse_file() and rdata.parser.convert(), as it is the common use case.

Parameters:
  • file_or_path (AcceptableFile | os.PathLike[Any] | Traversable | str) – File in the RDA format.

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

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

  • constructor_dict (ConstructorDict) –

    Dictionary mapping names of R classes to constructor functions with the following prototype:

    def constructor(obj, attrs):
        ...
    

    This dictionary can be used to support custom R classes. By default, the dictionary used is DEFAULT_CLASS_MAP which has support for several common classes.

  • default_encoding (str | None) – Default encoding used for strings with unknown encoding. If None, the one stored in the file will be used, or ASCII as a fallback.

  • force_default_encoding (bool) – Use the default encoding even if the strings specify other encoding.

  • global_environment (MutableMapping[str, Any] | None) – Global environment to use. By default is an empty environment.

  • base_environment (MutableMapping[str, Any] | None) – Base environment to use. By default is an empty environment.

Returns:

Contents of the file converted to a Python object.

Return type:

dict[str, Any]

See also

read_rds(): Similar function that parses a RDS file.

Examples

Parse one of the included examples, containing a dataframe

>>> import rdata
>>>
>>> data = rdata.read_rda(
...              rdata.TESTDATA_PATH / "test_dataframe.rda"
... )
>>> data
{'test_dataframe':   class  value
1     a      1
2     b      2
3     b      3}