googledrive icon indicating copy to clipboard operation
googledrive copied to clipboard

Fix recursive argument in drive_ls

Open Falnesio opened this issue 2 years ago • 5 comments

"Fixes #265" the problem related to the use of the recursive argument in drive_ls for shared drives.

Falnesio avatar Mar 07 '22 20:03 Falnesio

Sharing working example from before fix and after fix

image

Before fix:

## install googledrive from this PR
## install_github("tidyverse/googledrive#190")
library(googledrive)

## I have many tokens and want to use a specific one.
## Other people can just let the OAuth2 flow happen.
drive_auth("tidyverse-noncaching-token.rds")

drive_ls(as_id("1QPDu46aGHh8EjdJm-i12NzcVVBlVjyvt"), recursive = TRUE, type="folder")
# A dribble: 3 x 3
#  name  id                                drive_resource   
#  <chr> <drv_id>                          <list>           
#1 C     1iuIBsZTQNQPfdYkFqRdsbHX8fcpjb3ya <named list [32]>
#2 A     17qBnOnqjP3nemZdo5bcY4htnp4IZJrxg <named list [32]>
#3 B     186JZdflymqlq-DVa8S0jXbMGhcTEF5Vv <named list [32]>

After fix:

## install googledrive from this PR
## install_github("tidyverse/googledrive#190")
library(googledrive)

## I have many tokens and want to use a specific one.
## Other people can just let the OAuth2 flow happen.
drive_auth("tidyverse-noncaching-token.rds")

drive_ls(as_id("1QPDu46aGHh8EjdJm-i12NzcVVBlVjyvt"), recursive = TRUE, type="folder")
# A dribble: 6 x 3
#  name  id                                drive_resource   
#  <chr> <drv_id>                          <list>           
#1 B.3   1N9BRc3DNfBNa3csrbe8vnmBTO0f8IOSa <named list [32]>
#2 C     1iuIBsZTQNQPfdYkFqRdsbHX8fcpjb3ya <named list [32]>
#3 B.2   1Q0o-D6r8xJgIOkyqkrGCLk5vGOdR-Rkg <named list [32]>
#4 A     17qBnOnqjP3nemZdo5bcY4htnp4IZJrxg <named list [32]>
#5 A.1   1Qy6-CS5KAbYTuvu4jZjj7rL90j9kdg1O <named list [32]>
#6 B     186JZdflymqlq-DVa8S0jXbMGhcTEF5Vv <named list [32]>

Falnesio avatar Mar 10 '22 16:03 Falnesio

Some things to note about drive_ls() in itself when relating to shared drives:

  1. You need to have access to the shared drive for the drive_ls() function to not output an error.
drive_ls(as_id("1QPDu46aGHh8EjdJm-i12NzcVVBlVjyvt"))
#Error: Client error: (404) Not Found
#Shared drive not found: 0AEgEXi4W8aM2Uk9PVA
#• message: Shared drive not found: 0AEgEXi4W8aM2Uk9PVA
#• domain: global
#• reason: notFound
#• location: driveId
#• locationType: parameter
#Run `rlang::last_error()` to see where the error occurred.
  1. Having top level viewer access to the shared drive will make drive_ls() output a blank dribble.
drive_ls(as_id("1QPDu46aGHh8EjdJm-i12NzcVVBlVjyvt"))
# A dribble: 0 x 3
# … with 3 variables: name <chr>, id <drv_id>,
#   drive_resource <list>
  1. All other access works fine.
drive_ls(as_id("1QPDu46aGHh8EjdJm-i12NzcVVBlVjyvt"))
## A dribble: 3 x 3
#  name  id                                drive_resource   
#  <chr> <drv_id>                          <list>           
#1 A     17qBnOnqjP3nemZdo5bcY4htnp4IZJrxg <named list [30]>
#2 B     186JZdflymqlq-DVa8S0jXbMGhcTEF5Vv <named list [30]>
#3 C     1iuIBsZTQNQPfdYkFqRdsbHX8fcpjb3ya <named list [30]>

Falnesio avatar Mar 10 '22 16:03 Falnesio

@jennybc What gmail address may I associate to the shared drive so that my examples work for you?

Actually, I created this google group. Just enter it with a gmail and it should work. https://groups.google.com/u/3/g/teste_dfsdfsdfsdf/about

Falnesio avatar Mar 10 '22 16:03 Falnesio

I'll have to come back to this later, but I have access to shared drives. So if there were setup code (like, make a folder, put a new doc in it), I should be able to replicate any example.

jennybc avatar Mar 10 '22 16:03 jennybc

Here is the requested setup, test and cleanup. The other comments above show test outputs before and after fix.

## install googledrive from this PR
## install_github("tidyverse/googledrive#190")
library(googledrive)

## I have many tokens and want to use a specific one.
## Other people can just let the OAuth2 flow happen.
drive_auth("tidyverse-noncaching-token.rds")

# Choose the shared drive that you have write access to create folders
# and delete access for cleanup.
shared_drive_id <- "0AEgEXi4W8aM2Uk9PVA"
shared_drive_id <- as_id(shared_drive_id)

# Creating test folders.
test_folder <- drive_mkdir("test_folder", shared_drive_id)
A <- drive_mkdir("A", test_folder)
B <- drive_mkdir("B", test_folder)
C <- drive_mkdir("C", test_folder)
A.1 <- drive_mkdir("A.1", A)
B.1 <- drive_mkdir("B.1", B)
B.2 <- drive_mkdir("B.2", B)

# Output to test
 output_to_test <- drive_ls(test_folder, recursive = TRUE, type="folder")

# Test Function
is.recursive <- function(output){
  all_exist <- all(sapply(c("A","C","B","A.1","B.1","B.2"), exists))
  if(isFALSE(all_exist)){stop("Missing Variables")}
  
  recursive <- all(c("A","C","B","A.1","B.1","B.2") %in% output$name)
  not_recursive <- all(c("A","C","B") %in% output$name)
  if(recursive){
    print("Recursive")
    return(TRUE)
  } else if(not_recursive) {
    print("Not recursive")
    return(FALSE)
  } else {
    stop("Something has gone wrong when testing recursiveness: Not the expected output")
  }
}

# Testing
is.recursive(output_to_test)

# Cleanup
drive_rm(test_folder)
rm("shared_drive_id")
rm("test_folder")
rm("A")
rm("B")
rm("C")
rm("A.1")
rm("B.1")
rm("B.2")
rm("output_to_test")
rm("is.recursive")

Falnesio avatar Mar 11 '22 12:03 Falnesio

I changed the solution, to eliminate an extra drive_get() call for each sub-folder. We already have the ID of the shared drive, if relevant, so we just need to pass that info along.

Thanks for such a clear example and a working solution -- that was very helpful!

jennybc avatar Mar 20 '23 03:03 jennybc