Closed querenker closed 4 years ago
When a polyglot object
which is an array
is passed to a FastR builtin then it is automatically converted (or handled) as an native R vector.
In your example that would be the simple Python object (polyglot.export_value('l', [1, 2, 3]
), but not the Pandas data frame.
the same problem can be reproduced with java interop from the R console
> ja <- new('int[]', 2) # creates a java int array polyglot object
> sum(ja)
[1] 0
> matrix(ja)
[,1]
[1,] 0
[2,] 0
> class(ja) <- 'test'
vs
> calendar <- new('java.util.GregorianCalendar')
> sum(calendar)
Error in sum(calendar) : invalid 'type' (polyglot.value) of argument
> class(calendar) <- 'test'
An internal error occurred: "UnsupportedSpecializationException"
Nevertheless, instead of the UnsupportedSpecializationException
we should provide a proper error msg in the update class builtin.
for more information about interop in R see also the Fastr Java Interoperability doc and also the Rules for lazy conversion of foreign objects to R objects
thanks
Thank you for your insightful answer.
I am wondering, if it is intended that classes can not be assigned to polyglot values. I am new to R, but as far as I understand, (nearly?) every object in R can get an class attribute (even data types like integers, which are considered in many languages as primitive data type)
I am wondering, if it is intended that classes can not be assigned to polyglot values. I am new to R, but as far as I understand, (nearly?) every object in R can get an class attribute (even data types like integers, which are considered in many languages as primitive data type)
the issue here is that, ideally, foreign objects should be able to "flow" through R as-is, for example:
function(a, one, other) {
return(if (a == 1L) one else other)
}
this function can be called from another language, like Python, and whatever was in one
or two
should be returned back. If we allow to add attributes (like class) to foreign objects, what should we return to another language? Returning the foreign object w/o the attributes may be confusing and returning some wrapper with the attributes wouldn't keep this "identity" property. Moreover, returning the object w/o attributes wouldn't keep the "identity" property the other way around: if it finds its way back to R, it suddenly wouldn't have the attributes.
Currently, our philosophy is based on
as.xyz
functions, like as.integer(my_foreign_object)
. The implicit and explicit conversions are documented in those rules that @tstupka already pointed out.The issue here is that we don't have any implicit conversion your foreign object (Pandas data frame). If you used an ordinary Python list, for examle, the class<-
builtin would convert it to a vector and then assign the class attribute to it and would return that as the result (Note: class(x) <- "abc'
is syntax sugar for x <- `class<-`(x, "abc")
).
We should eventually add a support for either implicit and/or explicit conversion of Pandas DFs to R DFs. So far I think the implicit conversion can be bit cleaner: as.data.frame(my_pandas_df)
. Maybe it can already be implemented in user code with Java interop and proxy objects like in this example where a "data frame" like Java object is exposed to FastR as an R data frame.
Thank you very much for your detailed explanation 👍 Now I understand why it is handled that way and will close this issue.
If I want to assign a class to a polyglot value (in this case a Pandas data frame), I get the following error:
Interestingly, it works for simpler Python objects like lists:
Log File