googledrive icon indicating copy to clipboard operation
googledrive copied to clipboard

Utility to immediately provide shareable link to data

Open alexpghayes opened this issue 7 years ago • 7 comments

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.

alexpghayes avatar Oct 10 '18 16:10 alexpghayes

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.

jennybc avatar Oct 10 '18 18:10 jennybc

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.

alexpghayes avatar Oct 11 '18 02:10 alexpghayes

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

jennybc avatar Oct 11 '18 07:10 jennybc

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

alexpghayes avatar Oct 11 '18 16:10 alexpghayes

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.

jennybc avatar Oct 11 '18 20:10 jennybc

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.

alexpghayes avatar Oct 12 '18 16:10 alexpghayes

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!

simonpcouch avatar Oct 18 '19 18:10 simonpcouch