diff --git a/_pkgdown.yml b/_pkgdown.yml index 9f242a245..005680a91 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -18,11 +18,17 @@ navbar: href: articles/index.html - text: File Identification href: articles/articles/file-identification.html + - text: Dealing with multiple files + href: articles/articles/multiple-files.html + - text: File permissions + href: articles/articles/permissions.html articles: - title: "Articles" contents: - file-identification + - multiple-files + - permissions reference: - title: "Reach out and touch your files" diff --git a/docs/LICENSE.html b/docs/LICENSE.html index cca08de3e..80e68805f 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -67,6 +67,12 @@
Some googledrive functions are built to naturally handle multiple files, while others operate on a single file.
+Functions that expect a single file:
+drive_browse()
drive_cp()
drive_download()
drive_ls()
drive_mkdir()
drive_mv()
drive_rename()
drive_update()
drive_upload()
Functions that allow multiple files:
+drive_publish()
drive_reveal()
drive_rm()
drive_share()
drive_trash()
In general, the principle is: if there are multiple parameters that are likely to vary across multiple files, the function is designed to take a single input. In order to use these function with multiple inputs, use them together with your favorite approach for iteration in R. Below is a worked example, focusing on tools in the tidyverse, namely the map()
functions in purrr.
Scenario: we have multiple local files we want to upload into a folder on Drive. Then we regret their original names and want to rename them.
+Load packages.
+library(googledrive)
+library(glue)
+library(tidyverse)
+#> + ggplot2 2.2.1 Date: 2017-08-27
+#> + tibble 1.3.4 R: 3.3.2
+#> + tidyr 0.6.3 OS: OS X El Capitan 10.11.6
+#> + readr 1.1.1 GUI: X11
+#> + purrr 0.2.3 Locale: en_CA.UTF-8
+#> + dplyr 0.7.2 TZ: America/Vancouver
+#> + stringr 1.2.0
+#> + forcats 0.2.0
+#> Conflicts -----------------------------------------------------------------
+#> * collapse(), from dplyr, masks glue::collapse()
+#> * filter(), from dplyr, masks stats::filter()
+#> * lag(), from dplyr, masks stats::lag()
Use the example files that ship with googledrive. This looks a bit odd, but the first call returns their names and the second returns full paths on the local system.
+local_files <- drive_example() %>%
+ drive_example()
Create a folder on your Drive and upload all files into this folder by iterating over the local_files
using purrr::map()
.
folder <- drive_mkdir("upload-into-me-article-demo")
+#> Auto-refreshing stale OAuth token.
+#> Folder created:
+#> * upload-into-me-article-demo
+files <- map(local_files, drive_upload, path = folder, verbose = FALSE)
files
is now a list of dribbles, one per uploaded file. Let’s confirm that we uploaded the file into the new folder.
str(files, max.level = 1)
+#> List of 4
+#> $ :Classes 'dribble', 'tbl_df', 'tbl' and 'data.frame': 1 obs. of 3 variables:
+#> $ :Classes 'dribble', 'tbl_df', 'tbl' and 'data.frame': 1 obs. of 3 variables:
+#> $ :Classes 'dribble', 'tbl_df', 'tbl' and 'data.frame': 1 obs. of 3 variables:
+#> $ :Classes 'dribble', 'tbl_df', 'tbl' and 'data.frame': 1 obs. of 3 variables:
+drive_ls(folder)
+#> # A tibble: 4 x 3
+#> name id drive_resource
+#> * <chr> <chr> <list>
+#> 1 chicken.txt 0B0Gh-SuuA2nTR1g4UmFvRXg2aE0 <list [36]>
+#> 2 chicken.pdf 0B0Gh-SuuA2nTV0ZUQ1ByT2lJTVU <list [36]>
+#> 3 chicken.jpg 0B0Gh-SuuA2nTZzF0N2VuNlViVzg <list [38]>
+#> 4 chicken.csv 0B0Gh-SuuA2nTalRLdmlNRlJfVGM <list [36]>
Imagine that we now wish these file names had a date prefix. First, form the new names. We use glue::glue()
for string interpolation but you could also use paste()
. Second, we map over two inputs: the list of dribbles from above and the vector of new names.
new_names <- glue("{Sys.Date()}_{basename(local_files)}")
+files_dribble <- map2_df(files, new_names, drive_rename)
+#> File renamed:
+#> * chicken.csv -> 2017-08-27_chicken.csv
+#> File renamed:
+#> * chicken.jpg -> 2017-08-27_chicken.jpg
+#> File renamed:
+#> * chicken.pdf -> 2017-08-27_chicken.pdf
+#> File renamed:
+#> * chicken.txt -> 2017-08-27_chicken.txt
+## alternative: do this to get a list of dribbles for more downstream mapping
+# files_list <- map2(files, new_names, drive_rename)
We use purrr::map2_df()
to work through the list of dribbles (= Drive files) and the vector of new names and row bind the returned dribbles into a single dribble holding all files. In commented out code, we show an alternative using purrr::map2()
that would return another list of dribbles. This would set you up better for downstream operations that required more map()
ing.
Let’s check on the contents of this folder again:
+drive_ls(folder)
+#> # A tibble: 4 x 3
+#> name id drive_resource
+#> * <chr> <chr> <list>
+#> 1 2017-08-27_chicken.txt 0B0Gh-SuuA2nTR1g4UmFvRXg2aE0 <list [37]>
+#> 2 2017-08-27_chicken.pdf 0B0Gh-SuuA2nTV0ZUQ1ByT2lJTVU <list [37]>
+#> 3 2017-08-27_chicken.jpg 0B0Gh-SuuA2nTZzF0N2VuNlViVzg <list [38]>
+#> 4 2017-08-27_chicken.csv 0B0Gh-SuuA2nTalRLdmlNRlJfVGM <list [36]>
Note that you can always row bind individual dribbles into one big dribble yourself. We demo that with dplyr::bind_rows()
:
bind_rows(files)
+#> # A tibble: 4 x 3
+#> name id drive_resource
+#> <chr> <chr> <list>
+#> 1 chicken.csv 0B0Gh-SuuA2nTalRLdmlNRlJfVGM <list [36]>
+#> 2 chicken.jpg 0B0Gh-SuuA2nTZzF0N2VuNlViVzg <list [38]>
+#> 3 chicken.pdf 0B0Gh-SuuA2nTV0ZUQ1ByT2lJTVU <list [36]>
+#> 4 chicken.txt 0B0Gh-SuuA2nTR1g4UmFvRXg2aE0 <list [36]>
Our trashing function, drive_trash()
is vectorized and can therefore operate on a multi-file dribble. We could trash these files like so:
drive_trash(files_dribble)
If you’re absolutely sure of yourself and happy to do something irreversible, you could truly delete these files with drive_rm()
, which is also vectorized:
drive_rm(files_dribble)
Finally – and this is the code we will actually execute – the easiest way to delete these files is to delete their enclosing folder.
+drive_rm(folder)
+#> Files deleted:
+#> * upload-into-me-article-demo: 0B0Gh-SuuA2nTQ3lUSm42OVBsdUE
You can use googledrive to manage permissions on your Drive files, i.e. grant different people or groups of people various levels of access (read, comment, edit, etc.).
+Let’s upload a file and view its permissions.
+library(googledrive)
+file <- drive_example("chicken.txt") %>%
+ drive_upload(name = "chicken-perm-article.txt") %>%
+ drive_reveal("permissions")
+#> Auto-refreshing stale OAuth token.
+#> Local file:
+#> * /Users/jenny/resources/R/library/googledrive/extdata/chicken.txt
+#> uploaded into Drive file:
+#> * chicken-perm-article.txt: 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
+#> with MIME type:
+#> * text/plain
+file
+#> # A tibble: 1 x 5
+#> name shared id
+#> <chr> <lgl> <chr>
+#> 1 chicken-perm-article.txt FALSE 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
+#> # ... with 2 more variables: drive_resource <list>,
+#> # permissions_resource <list>
The shared
column shows that this file is not yet shared with anyone and, for those so inclined, detailed information on permissions can be found in the permissions_resource
list-column.
Let’s give a specific person permission to edit this file and a customized message, using the emailAddress
and emailMessage
parameters.
file <- file %>%
+ drive_share(
+ role = "writer",
+ type = "user",
+ emailAddress = "serena@example.com",
+ emailMessage = "Would appreciate your feedback on this!"
+ )
+file
#> Permissions updated
+#> * role = writer
+#> * type = user
+#> For files:
+#> * chicken-perm-article.txt: 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
+#> # A tibble: 1 x 5
+#> name shared id
+#> <chr> <lgl> <chr>
+#> 1 chicken-perm-article.txt TRUE 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
+#> # ... with 2 more variables: drive_resource <list>,
+#> # permissions_resource <list>
+We see that the file is now shared. We also want anyone to be able to read the file.
+file <- file %>%
+ drive_share(role = "reader", type = "anyone")
+#> Permissions updated
+#> * role = reader
+#> * type = anyone
+#> For files:
+#> * chicken-perm-article.txt: 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
Now that we’ve made a few updates to our permissions, the permissions_resource
list-column has become more interesting. Here’s how to pull important information out of this and put into a tibble with one row per permission. (Permission handling will become more formalized in future versions of googledrive. See the issue). We use other packages in the tidyverse now for this data wrangling.
library(tidyverse)
+#> + ggplot2 2.2.1 Date: 2017-08-27
+#> + tibble 1.3.4 R: 3.3.2
+#> + tidyr 0.6.3 OS: OS X El Capitan 10.11.6
+#> + readr 1.1.1 GUI: X11
+#> + purrr 0.2.3 Locale: en_CA.UTF-8
+#> + dplyr 0.7.2 TZ: America/Vancouver
+#> + stringr 1.2.0
+#> + forcats 0.2.0
+#> Conflicts -----------------------------------------------------------------
+#> * filter(), from dplyr, masks stats::filter()
+#> * lag(), from dplyr, masks stats::lag()
+perm <- pluck(file, "permissions_resource", 1, "permissions")
+permissions <- tibble(
+ id = map_chr(perm, "id", .null = NA_character_),
+ name = map_chr(perm, "displayName", .null = NA_character_),
+ type = map_chr(perm, "type", .null = NA_character_),
+ role = map_chr(perm, "role", .null = NA_character_),
+ email = map_chr(perm, "emailAddress", .null = NA_character_)
+)
+as.data.frame(permissions)
+#> id name type role
+#> 1 01555823402173812461 tidyverse testdrive user owner
+#> 2 14650328737125225074 Jennifer Bryan user writer
+#> 3 anyoneWithLink <NA> anyone reader
+#> email
+#> 1 tidyverse.testdrive@gmail.com
+#> 2 jenny@stat.ubc.ca
+#> 3 <NA>
drive_rm(file)
+#> Files deleted:
+#> * chicken-perm-article.txt: 0B0Gh-SuuA2nTaWhkdEpBVFl3TEE
Notice that file was uploaded as text/csv
. Since this was a .csv
document, and we didn’t specify the type, googledrive guessed the MIME type. We can overrule this by using the type
parameter to upload as a Google Spreadsheet. Let’s delete this file first.
drive_rm(chicken)
#> Files deleted:
-#> * README-chicken.csv: 0B0Gh-SuuA2nTdEtweWJvaTB4Yzg
+#> * README-chicken.csv: 0B0Gh-SuuA2nTSDRScVhUbWlEaTA
## example of using a dribble as input
chicken_sheet <- drive_upload(
@@ -203,7 +209,7 @@
#> Local file:
#> * /Users/jenny/resources/R/library/googledrive/extdata/chicken.csv
#> uploaded into Drive file:
-#> * README-chicken.csv: 1IlgEdwAC47WyRmQbQTovGPPfQDnIbmyekNuSQNexfxo
+#> * README-chicken.csv: 1zDxruu6Wz98SpZU2MjI5kGSDidb-NWwnvpcJIFFpHpY
#> with MIME type:
#> * application/vnd.google-apps.spreadsheet
Much better!
@@ -217,7 +223,7 @@Here’s how to grant anyone with the link permission to be able to view this dataset.
@@ -227,11 +233,11 @@By default, drive_publish()
will publish your most recent version.
(chicken_sheet <- drive_publish(chicken_sheet))
#> Files now published:
-#> * README-chicken.csv: 1IlgEdwAC47WyRmQbQTovGPPfQDnIbmyekNuSQNexfxo
+#> * README-chicken.csv: 1zDxruu6Wz98SpZU2MjI5kGSDidb-NWwnvpcJIFFpHpY
#> # A tibble: 1 x 7
#> name published shared
#> <chr> <lgl> <lgl>
@@ -297,7 +303,7 @@
#> Local file:
#> * /Users/jenny/resources/R/library/googledrive/extdata/chicken.txt
#> uploaded into Drive file:
-#> * text-file.txt: 0B0Gh-SuuA2nTUHI4bGxQb1JHVGc
+#> * text-file.txt: 0B0Gh-SuuA2nTbmdKUGUybGlJa28
#> with MIME type:
#> * text/plain
@@ -321,8 +327,8 @@
Clean up
drive_rm(chicken_sheet, text_file)
#> Files deleted:
-#> * README-chicken.csv: 1IlgEdwAC47WyRmQbQTovGPPfQDnIbmyekNuSQNexfxo
-#> * text-file.txt: 0B0Gh-SuuA2nTUHI4bGxQb1JHVGc
+#> * README-chicken.csv: 1zDxruu6Wz98SpZU2MjI5kGSDidb-NWwnvpcJIFFpHpY
+#> * text-file.txt: 0B0Gh-SuuA2nTbmdKUGUybGlJa28
Retrieves the pre-configured API key. Learn more in Google's document
-Credentials, access, security, andidentity.
+Credentials, access, security, and identity.
By default, this API key is initialized to one that ships with googledrive.
But the user can store their own key via drive_auth_config()
, i.e.
overwrite the default.
Optional; path to an .rds
file with a previously stored
-oauth token.
.httr-oauth
For even deeper control over auth, use drive_auth_config()
to use your own
-oauth app or API key. drive_auth_config()
also allows you to
+OAuth app or API key. drive_auth_config()
also allows you to
deactivate auth, sending only an API key in requests, which works if you
only need to access public data.
The googledrive auth state. The default is active, meaning all requests
are sent with a token and, if one is not already loaded, OAuth flow is
-initiated. It is possible, however, to place unauthorizeded requests to
+initiated. It is possible, however, to place unauthorized requests to
the Drive API, as long as you are accessing public resources. Set active
to FALSE
to enter this state and never send a token.
The OAuth app. If you want to use your own app, setup a new project in diff --git a/docs/reference/drive_browse.html b/docs/reference/drive_browse.html index 28628b387..225b3f592 100644 --- a/docs/reference/drive_browse.html +++ b/docs/reference/drive_browse.html @@ -67,6 +67,12 @@
This is a helper to determinine which MIME type should be used +
This is a helper to determine which MIME type should be used for a file. Three types of input are acceptable:
Native Google Drive file types. Important examples:
"document" for Google Docs