r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

Enable override the is_interactive function. #634

Closed jobdiogenes closed 3 years ago

jobdiogenes commented 4 years ago

In some cases checking if is interactive mode are not as we need. Since is_interactive parameter are deprecated and the is_interactive function enable to mocked the native interactive function. This pull request add just two lines to export _isinteractive function that is in oauth-init.R, enabling developers to override _isinteractive.

I use this to help me use R and Google Drive / Sheets in Google Colaboratory, which are also useful to many users. But its also useful to others cases and tests.

jennybc commented 4 years ago

I suspect a better eventual solution is for httr to use rlang::is_interactive(), which did not exist when httr first appeared.

https://rlang.r-lib.org/reference/is_interactive.html

That affords various options for temporarily overriding the inferred interactive state. It would be odd for httr itself to export such a function.

As maintainer of the packages you are probably using for "R and Google Drive / Sheets in Google Colaboratory", I'd be curious to hear more. Now that I think about it, I suspect this is related to https://github.com/tidyverse/googledrive/issues/284, which I think is a more profound problem than just overriding interactive(). But I'd be curious to hear more about your workarounds, over in that issue.

jobdiogenes commented 4 years ago

@jennybc using this workaround I could successfully use googledrive and googlesheets4 to access files and sheets. But its really heavy.

I restart my httr fork and make your suggestion.

Yes, changing to:

is_interactive <- function() rlang::is_interactive()

also solve with use of: options(rlang_interactive=TRUE) and its more simple and light.

another hack that I test was to verify if is running under colab. which have the advantage that workbooks could works seamlessly without changes. but its a terrible hack at now.
I will continue looking to find a better solution.

is_interactive <- function() { 
  if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")) {
     return(TRUE) 
  } else { 
    return(interactive())
  }
}
nacnudus commented 4 years ago

@jobdiogenes Do you have an example colab notebook where you can connect to Google Drive? I have tried here using your fork, but it still fails.

devtools::install_github("jobdiogenes/httr")
devtools::install_github("tidyverse/googledrive")
library(googledrive)
options(rlang_interactive = TRUE)
drive_auth(use_oob = TRUE)
Is it OK to cache OAuth access credentials in the folder '/root/.R/gargle/gargle-oauth' between R sessions?
Error: Can't get Google credentials.
Are you running googledrive in a non-interactive session? Consider:
  * `drive_deauth()` to prevent the attempt to get credentials.
  * Call `drive_auth()` directly with all necessary specifics.
  * Read more in: https://gargle.r-lib.org/articles/non-interactive-auth.html
Traceback:

1. drive_auth(use_oob = TRUE)
2. stop("Can't get Google credentials.\n", "Are you running googledrive in a non-interactive session? Consider:\n", 
 .     "  * `drive_deauth()` to prevent the attempt to get credentials.\n", 
 .     "  * Call `drive_auth()` directly with all necessary specifics.\n", 
 .     "  * Read more in: https://gargle.r-lib.org/articles/non-interactive-auth.html", 
 .     call. = FALSE)
jobdiogenes commented 4 years ago

@nacnudus Yes, I did some more tests. And make a Notebook that are working, for both in colab or external. and publish as Gist.

demo-of-use-google-drive-sheets-in-r-with-google-colaboratory.ipynb

nacnudus commented 4 years ago

Thank you @jobdiogenes, that works!

nacnudus commented 4 years ago

The Notebook no longer works for me.

nacnudus commented 4 years ago

The notebook works if you do the same for rlang as for httr. Both of them have to be overridden. I'll comment on the gist too.

jobdiogenes commented 4 years ago

The notebook works if you do the same for rlang as for httr. Both of them have to be overridden. I'll comment on the gist too.

The notebook are working again. Although reassign is_interactive in rlang solves. I opt to use options(rlang_interactive=TRUE) which also solve. I hope that in future the use of options could be enough.

jennybc commented 4 years ago

This conversation should be informed by https://github.com/r-lib/rlang/pull/1033.

It doesn't get at or solve the heart of the issue, but it's related.