convert#

convert(data, constructor_dict=DEFAULT_CLASS_MAP, *, default_encoding=None, force_default_encoding=False, global_environment=None, base_environment=None)[source]#

Use the default converter (SimpleConverter()) to convert the data.

Parameters:
  • data (parser.RData | parser.RObject) – Parsed data.

  • 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.

Return type:

Any

Examples

Parse one of the included examples, containing a vector

>>> import rdata
>>>
>>> parsed = rdata.parser.parse_file(
...              rdata.TESTDATA_PATH / "test_vector.rda")
>>> converted = rdata.conversion.convert(parsed)
>>> converted
{'test_vector': array([1., 2., 3.])}

Parse another example, containing a dataframe

>>> import rdata
>>>
>>> parsed = rdata.parser.parse_file(
...              rdata.TESTDATA_PATH / "test_dataframe.rda")
>>> converted = rdata.conversion.convert(parsed)
>>> converted
{'test_dataframe':   class  value
1     a      1
2     b      2
3     b      3}