New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
py_to_r() fails when when importing a python module with an alias #319
Comments
|
The If I'm misunderstanding the issue and you can see your way to another resolution do let me know as I'm not sure I follow the issue you are seeing fully! |
|
I fixed the problem by removing the |
|
The docs on the This certainly does allude to the fact (albeit somewhat obliquely) that S3 method dispatch will be affected. Nevertheless, I added some additional docs cautioning users against using this parameter outside of package development. |
|
I suspect naming the parameter whereas that construct is more accurately reflected on the R side with a plain old It might be more appropriate to call that parameter something like |
|
Yeah, if I rename it |
|
FYI - made a change to reticulate::import() function which renames the S3 methods associated with the module that is being renamed to the alias. This allowed all related methods to function. There is much more testing and I'm sure a better way of doing this that would have to occur yet I think the idea is correct. Take a look if you want at the reticulate::import method and the additional function add_functions_by_alias() I added in. https://raw.githubusercontent.com/hitfuture/reticulate/fix/alias_functions/R/python.R |
|
I don't think I want to be dynamically renaming methods though. The real question is what does the R user that specifies |
|
Your answer makes sense. I'm not having any problems with reticulate in the package I'm building since I realized what was happening. It is interesting having been trained in Python over the last 3 months where I was required to use pd for pandas and np for numpy which I also used in reticulate because of all of the online education from DataCamp. Thanks again for the feedback and documentation is all that matters here. Reticulate is a huge win for the language R! |
|
Okay, great. Glad it's working so well for you! |
|
I run into the same error even though I import the module without the use of 'as' or 'alias'
any suggestions ? |
|
What kind of Python object is returned by: ? I suspect you'll need to use |
|
(((Was just about to answer as well :-))) I recently used |
hitfuture commentedJul 23, 2018
I am using the reticulate package to integrate Python into an R package I'm building (hitfuture/Neo4jDriveR). One of the capabilities I need is to return R data.frames from a method in the R6 based object model. I utilize Python Pandas package to create a DataFrame in the reticulate Python environment. My objective is to return an R data.frame. The issue I'm seeing is that when I used reticulate::py_to_r(df) it does not convert to R and instead it returns a python DataFrame object. The problem is based on the way reticulate::import() is utilized.
I had forked reticulate into my github repository so I am using the latest version (1.9.0.9001). I see that there are well defined S3 methods to handle pandas DataFrame conversion in the reticulate py_to_r() S3 class (e.g. py_to_r.pandas.core.frame.DataFrame). The following test executes correctly in a new R session.
The failure occurs when I utilize the function 'reticulate::import("pandas", as="pd")' with the
asparameter. You can see below that the pandas.DataFrame is not converted into an R data.frame. The problem is due to the S3 method for the pandas DataFrame not matching based on the name of the python module.#expect_is(after,class = "data.frame")I have tested this on two different Docker containers, and on my MacBook Pro where the same error occurs. The problem is based on the fact that if you assign an Alias in the import() function, the S3 methods will not be correctly selected. This will apply to all py_to_r conversions if the alias is not equivalent to the default name. There is not an easy solution to solve this with the S3 methodology. It would be good if the S3 methods are exported in the reticulate package so this can be handled manually. This should also be documented.
Thanks,
Brett
The text was updated successfully, but these errors were encountered: