Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drive_read()? #81

Closed
tiernanmartin opened this issue Jun 8, 2017 · 9 comments · Fixed by #363
Closed

drive_read()? #81

tiernanmartin opened this issue Jun 8, 2017 · 9 comments · Fixed by #363

Comments

@tiernanmartin
Copy link

Is there a way to read a data object out of a dribble (similar to readr::read() or googlesheets::gs_read())?

@jennybc
Copy link
Member

jennybc commented Jun 8, 2017

Not yet but it could happen. Question is: what sort of object do you read it into? Sheets are one thing and will be handled by googlesheets. But what else might you want to read straight into an R object? 🤔 Curious to hear the use you have in mind.

@tiernanmartin
Copy link
Author

While I do use Google Drive to create and edit spreadsheets, I also use it as cloud-based data storage for file formats that aren't editable through the Drive interface. These file formats include .csv, .md, .rds, .zip, .gpkg, etc.

So I guess I'd like a function that could serve a wrapper around the read_* functions, giving them access to my Drive account.

Something like:

## this code is for illustration purpose - not a functional reprex

# Upload file to Drive
write.csv(chickwts, "chickwts.csv")
drive_chickwts <- drive_upload("chickwts.csv")

# Read that file back into R
drive_read(obj = drive_chickwts , fun = read_csv, fun_args = list()) # fun_args would be passed to the read function's params

@jennybc
Copy link
Member

jennybc commented Jun 23, 2021

It's been a while but if you still care about this @tiernanmartin, I'd love to hear if it meets your needs.

See #363. There are tons of examples, which I may have to move to an article, because they involve packages that googledrive has no formal relationship with.

@tiernanmartin
Copy link
Author

Nice! This looks great.

I am not sure whether it will meet my particular needs. I work with a lot of geopackage (.gpkg) files that I store on google drive, and my naïve attempt to access one of those with this new function returned an error.

Here's a reprex:

library(tidyverse)
library(sf)
library(googledrive)

nc <- st_read(system.file("shape/nc.shp", package="sf"))

st_write(nc, "nc.gpkg")

nc_in_drive <- drive_upload("nc.gpkg")

drive_read(nc_in_drive) %>% 
  st_read()

## Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  : 
## Expecting a single value: [extent=167936].
## In addition: Warning message:
## In if (nchar(dsn) < 1) { :
## the condition has length > 1 and only the first element will be used

@jennybc
Copy link
Member

jennybc commented Jun 24, 2021

I'm looking at the docs for sf::st_read():

https://r-spatial.github.io/sf/reference/st_read.html

The first argument dsn is documented as:

data source name (interpretation varies by driver - for some drivers, dsn is a file name, but may also be a folder, or contain the name and access credentials of a database); in case of GeoJSON, dsn may be the character string holding the geojson data. It can also be an open database connection.

It does not surprise me that piping the result of drive_read() directly into this does not work.

For this to "just work", you need to see that the primary input can be a raw vector, which is presumably what you're getting from drive_read() here. (If you re-install from #363, you should get some messaging around how as is being set.)

It is possible that by explicitly requesting as = "string" you might have success, if this branch is relevant to your usage: "in case of GeoJSON, dsn may be the character string holding the geojson data".

drive_read() can only provide ready-to-use input for functions that are already prepared to handle raw or string input. Otherwise, you really do just need to download a file and feed it to the next function.

@jennybc
Copy link
Member

jennybc commented Jun 24, 2021

A great example of the type of function that drive_read() can pipe right into is readr::read_csv(). Its primary input file is documented like so (bold is mine)

Either a path to a file, a connection, or literal data (either a single string or a raw vector).

@jennybc
Copy link
Member

jennybc commented Jun 24, 2021

Note that textConnection() can potentially be inserted as an adapter between string output from drive_read() and a function that accepts a connection.

@tiernanmartin
Copy link
Author

Thanks for these suggestions, Jenny.

I played around with a couple combinations of file types but I didn't come across any that worked. I am ok with that. Your documentation is very clear about what types of files are supported, so I was not really expecting it to "just work" with something exotic like a geopackage. I appreciate you taking the time to brainstorm some possible approaches--and who knows maybe someone else will chime in with a solution at some point.

Thanks for bringing drive_read() to life.

Issue closed! +1

@jennybc
Copy link
Member

jennybc commented Jun 24, 2021

I’m going to reopen and let the PR merge close this. We might use this issue as a demo re: PR review in an upcoming group meeting.

@jennybc jennybc reopened this Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants