tidyverse / googledrive

Google Drive R API
https://googledrive.tidyverse.org/
Other
322 stars 48 forks source link

Utility to immediately provide shareable link to data #233

Open alexpghayes opened 6 years ago

alexpghayes commented 6 years ago

I've been using the following for a little while to quickly share small datasets via Google Drive.

Would love to submit a PR, not sure about the best final form for this:

# upload a data frame to google drive, make it shareable, and
# copy the shareable link into the clipboard
get_shareable_link_to_data <- function(data, path, direct = TRUE) {
  readr::write_csv(data, path)
  df <- googledrive::drive_upload(path, path)
  df <- googledrive::drive_share(df, role = "reader", type = "anyone")

  if (direct)
    link <- paste0("https://drive.google.com/uc?export=download&id=", df$id)
  else
    link <- googledrive::drive_link(df)

  clipr::write_clip(link)
  fs::file_delete(path)
  cat(link)
  invisible(link)
}

When direct = TRUE, the link can be immediately used in read_csv(). When direct = FALSE, the link send users to the Google Drive preview of the data.

jennybc commented 6 years ago

Good idea!

I think it would be interesting to hash this out in the company of:

44 Uploading an R object w/o writing to file

81 drive_read()?

The common theme seems to be "local data frame" <--> "csv on Google Drive" and making the intermediate local file fade into the background or be unnecessary.

alexpghayes commented 6 years ago

I don't know enough about googledrive to understand what's most idiomatic here. Any guidance?

More than anything the takeaway that I want is a link that I can copy-paste directly into a chat, email or Github issue to avoid the hassle of figuring out how to actually upload a data frame somewhere.

Might also be worth uploading files to a /.auto_uploads/ folder or similar. In my experimentation, it was pretty easy to accidentally fill my main Drive folder with tons of garbage / redundant data sets.

jennybc commented 6 years ago

Did you have trouble with uploading multiple things with the same name? Drive lets you do that.

alexpghayes commented 6 years ago

No trouble with multiple uploads. I have like 28 copies of mtcars.csv in my Drive right now.

jennybc commented 6 years ago

Yeah I meant ... are you unhappy with that situation?

In #230 (Consistent naming and overwrite for drive_upload), there is a discussion about workflow when user wants to upload to a file path that already exists. Right now, we cheerfully and silently upload a new file with a replicated name, because that's how Drive works.

But most people don't mean to do this and it makes it hard for them to distinguish their 28 files with the same name but potentially different contents.

This will come up here too.

alexpghayes commented 6 years ago

Ah, gotcha. What about an option overwrite that defaults to TRUE?

Then if overwrite = TRUE and path = "mtcars.csv", but there are already 2+ files names mtcars.csv, give an informative error, since it won't be clear which file to overwrite.

simonpcouch commented 5 years ago

Not at all a comprehensive solution, but I put together a package this morning to address part of the "local data frame" <--> "csv on Google Drive" workflow that @jennybc and Issue #81 mentioned. This package supplies a grab_data function that is a wrapper around drive_download and some tempfile pushings-around that takes in a URL to a .csv, .Rda, or Google Sheets file and outputs a tibble!