Skip to content

Commit

Permalink
Merge pull request #26 from statisticsnorway/more_tests
Browse files Browse the repository at this point in the history
More tests
  • Loading branch information
Kss2k authored Oct 30, 2024
2 parents 0ad6e4c + ac1547f commit b9d4fc3
Show file tree
Hide file tree
Showing 3 changed files with 742 additions and 362 deletions.
20 changes: 20 additions & 0 deletions src/rwrapr/renv.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ def rclass(self, x: Any) -> RReturnType:
foo: Callable[..., RReturnType] = rfunc("class")
return foo(x)

def reval(self, expr: str, rview: bool) -> Any:
"""
Evaluates an R expression.
Args:
expr (str): The R expression to evaluate.
rview (bool): If True, returns the result as an RView object. Defaults to False.
Returns:
Any: The result of the R expression, depends on rview argument and setting.
"""
rview = rview or settings.rview_mode

r_object: Any = ro.r(expr, invisible=True, print_r_warnings=False)

if rview:
return RView(r_object)
else:
return convert_r2py(r_object)


def fetch_data(
dataset: str, module: rpkg.Package | None
Expand Down
26 changes: 17 additions & 9 deletions src/rwrapr/rlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def to_py(self) -> list[Any]:
return out


ListTypes = list[Any] | tuple[Any] | set[Any] | RList


class RDict(UserDict[str, Any]):
def __init__(self, x: Any, attributes: dict[str, Any] | None):
super().__init__(x)
Expand All @@ -64,9 +67,10 @@ def to_py(self) -> dict[str, Any]:
return out


def convert_r2pylist(
x_collection: list[Any] | tuple[Any] | RList,
) -> list[Any] | tuple[Any]:
DictTypes = dict[str, Any] | OrderedDict[str, Any] | UserDict[str, Any] | RDict


def convert_r2pylist(x_collection: ListTypes) -> list[Any] | tuple[Any]:
from .convert_r2py import convert_r2py

out: tuple[Any] | list[Any] = [convert_r2py(x) for x in x_collection]
Expand All @@ -80,9 +84,16 @@ def convert_rlist2py(x_collection: vc.ListVector | vc.ListSexpVector) -> Any:
from .rattributes import get_rattributes

names = convert_numpy(x_collection.names, flatten=False)

if isinstance(names, int | str | float | bool):
names = np.array([names], dtype="U")

if names is not None:
fill = np.arange(1, len(names) + 1).astype("U")
if names.itemsize < fill.itemsize:
names = names.astype(fill.dtype)
names[names == ""] = fill[names == ""]

attributes = get_rattributes(x_collection, exclude=["names"])

if attributes is not None:
Expand All @@ -104,10 +115,7 @@ def is_rlist(x_collection: Any) -> bool:
return False


def convert_r2pydict(
x_collection: dict[str, Any] | OrderedDict[str, Any] | UserDict[str, Any] | RDict,
is_rdict: bool = False,
) -> Any:
def convert_r2pydict(x_collection: DictTypes, is_rdict: bool = False) -> Any:
from .convert_r2py import convert_r2py

# this needs to be improved considering named vectors
Expand All @@ -122,13 +130,13 @@ def convert_r2pydict(
return x_collection


def dict2rlist(x: dict[str, Any] | OrderedDict[str, Any] | RDict) -> ro.ListVector:
def dict2rlist(x: DictTypes) -> ro.ListVector:
from .convert_py2r import convert_py2r

return ro.ListVector({k: convert_py2r(v) for k, v in x.items()})


def pylist2rlist(x: list[Any] | tuple[Any] | set[Any] | RList) -> ro.ListVector:
def pylist2rlist(x: ListTypes) -> ro.ListVector:
y: dict[str, Any] = {str(k): v for k, v in enumerate(x)}
unname: Callable[..., Any] = rcall("unname")
return unname(dict2rlist(y))
Loading

0 comments on commit b9d4fc3

Please sign in to comment.