Skip to content
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

Fix attempt to apply non-function #7

Closed
wants to merge 1 commit into from
Closed

Fix attempt to apply non-function #7

wants to merge 1 commit into from

Conversation

timonbldw
Copy link

FuzzywuzzyR would always return the error "attempt to apply non-function" when calling a function after loading the library. By running reticulate::py_config() beforehand the correct python environments are discovered and the library works correctly.

FuzzywuzzyR would always return the error "attempt to apply non-function" when calling a function after loading the library. By running reticulate::py_config() beforehand the correct python environments are discovered and the library works correctly.
@mlampros
Copy link
Owner

mlampros commented Sep 2, 2021

hi @timonbldw and thanks for the pull request. I took a look also to your message in issue 1.
Although your solution might solve the problem in your operating system, by adding

reticulate::py_config()

it will cause problems when submitting to CRAN. R packages that make use of 'reticulate' have to take into consideration that during CRAN checking python might not be installed.

Therefore by calling reticulate::py_config() you force the initialization of python and this will throw errors when submitting to CRAN. You can also see the documentation of the function where is mentioned,

If Python has not yet been initialized, then calling py_config() will force the initialization of
 Python. See py_discover_config() for more details.

If you see the examples in the documentation of the package most have the following code snippet for this purpose,

try({
  if (reticulate::py_available(initialize = FALSE)) {

    if (check_availability()) {

    ........
  }
}

Besides using reticulate::py_config() before you perform fuzzy string matching you can also use

reticulate::py_available(initialize = TRUE)    # initializes python bindings
fuzzywuzzyR::check_availability()              # checks if the fuzzywuzzyR python dependencies are installed

then you can continue using the fuzzywuzzyR package if the previous 2 commands returned TRUE

@timonbldw
Copy link
Author

timonbldw commented Sep 3, 2021

Thanks for your reply! I see, it's likely OS specific in this case. I am using Win10 with conda and Python 3.6.13. I have tried your suggestion before, unfortunately (at least on my OS), it still throws the same error:

Besides using reticulate::py_config() before you perform fuzzy string matching you can also use

reticulate::py_available(initialize = TRUE)    # initializes python bindings
fuzzywuzzyR::check_availability()              # checks if the fuzzywuzzyR python dependencies are installed

then you can continue using the fuzzywuzzyR package if the previous 2 commands returned TRUE

> reticulate::py_available(initialize = TRUE)    # initializes python bindings
[1] TRUE
> fuzzywuzzyR::check_availability()              # checks if the fuzzywuzzyR python dependencies are installed
[1] TRUE
> library("fuzzywuzzyR")
> matcher <- FuzzMatcher$new()
> matcher$Partial_ratio(string1 = "abcd", string2 = "def")
Error in matcher$Partial_ratio(string1 = "abcd", string2 = "def") : 
    attempt to apply non-function

I see that adding reticulate::py_config() is not an option due to CRAN. I would still suggest to add a warning to the onLoad function if e.g. the check reticulate::py_available(initialize = FALSE) fails. What do you think?

@mlampros
Copy link
Owner

mlampros commented Sep 3, 2021

@timonbldw I'll tell you the truth. Last time that I submitted to CRAN I spent many hours to figure out how to make things work so that I can keep the fuzzywuzzyR package both on Github and on CRAN (I also have other R packages that depend on reticulate and Python). I think that it's not that easy to make an R package communicate with all different versions that exist in Python (I mean also the Conda environments, you can see also issues related to Conda in the reticulate repository) so I'm kind of hesitant to modify something in the .onLoad function that might break things.
If you are willing and have the time feel free to modify this PR so that by adding any lines of code it doesn't break anything (on all 3 OS's - Linux, Macintosh, Windows) on CRAN.

@timonbldw
Copy link
Author

timonbldw commented Sep 3, 2021

@mlampros I totally see where you're coming from. I'd just add a simple warning() to notify the user that something is off, this shouldn't break anything at all.

I think I might have found part of the root cause for the issue on Windows 10. When using devtools to import the package (without calling reticulate::py_config()) I get the error Can't open 'C:\Users\user_name\AppData\Local\MICROS~1\WINDOW~1\python3.exe': cannot access the specified device path or file. When looking at the file, it's a 0-Byte file called python3.exe. This seems to be a feature from Windows 10 version 1903 (announcement here) and can be disabled. Looks like it's more of a reticulate issue on my OS that requires me to initialize it first.

@mlampros
Copy link
Owner

mlampros commented Sep 12, 2021

@timonbldw,

I just wanted to inform you that I've added a startup message to the fuzzywuzzyR package (and to all my other R packages that use the 'reticulate' R package to call Python code). You were right that there are cases where the "reticulate::py_config()" is required before loading the fuzzywuzzyR package otherwise the user receives the error 'attempt to apply non-function' (I experienced this error the past week, so I guess something has changed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants