使用Dropbox API读取R中的原始数据以另存为.RData文件 [英] reading raw data in R to be saved as .RData file using the dropbox api

查看:166
本文介绍了使用Dropbox API读取R中的原始数据以另存为.RData文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经设计出了oauth签名批准系统,对于Dropbox,我想使用API​​和 httr 下载一个保存在其中的.RData文件。 GET 函数。

Having worked out the oauth signature approval system, for dropbox, I wanted to download an .RData file that I had saved there, using the API, and httr's GET function.

该请求已成功完成并返回数据,但它是原始格式,想知道如何在我上再次将其转换为RData文件

The request was sucessfull and comes back with data, but it is in a raw format, and was wondering how do I go about converting it into an RData file again on my local drive.

这是我到目前为止所做的:...

This is what I've done so far:...

require(httr)
db.file.name <- "test.RData"
db.app <- oauth_app("db",key="xxxxx", secret="xxxxxxx")
db.sig <- sign_oauth1.0(db.app, token="xxxxxxx", token_secret="xxxxxx")

response <- GET(url=paste0("https://api-content.dropbox.com/1/files/dropbox/",db.file.name),config=c(db.sig,add_headers(Accept="x-dropbox-metadata")))

str(response)
List of 8
 $ url        : chr "https://api-content.dropbox.com/1/files/dropbox/test.RData"
 $ handle     :List of 2
  ..$ handle:Formal class 'CURLHandle' [package "RCurl"] with 1 slots
  .. .. ..@ ref:<externalptr> 
  ..$ url   :List of 8
  .. ..$ scheme  : chr "https"
  .. ..$ hostname: chr "api-content.dropbox.com"
  .. ..$ port    : NULL
  .. ..$ path    : chr ""
  .. ..$ query   : NULL
  .. ..$ params  : NULL
  .. ..$ username: NULL
  .. ..$ password: NULL
  .. ..- attr(*, "class")= chr "url"
  ..- attr(*, "class")= chr "handle"
 $ status_code: num 200
 $ headers    :List of 14
  ..$ server                       : chr "nginx/1.2.6"
  ..$ date                         : chr "Tue, 29 Jan 2013 10:18:58 GMT"
  ..$ content-type                 : chr "application/octet-stream"
  ..$ content-length               : chr "1142953"
  ..$ connection                   : chr "keep-alive"
  ..$ access-control-expose-headers: chr "X-Dropbox-Metadata, Accept-Ranges, Content-Range"
  ..$ accept-ranges                : chr "bytes"
  ..$ x-dropbox-metadata           : chr "{\"revision\": 8398, \"rev\": \"20ce0573b0e8\", \"thumb_exists\": false, \"bytes\": 1142953, \"modified\": \"Thu, 24 Jan 2013 2"| __truncated__
  ..$ etag                         : chr "8398n"
  ..$ pragma                       : chr "public"
  ..$ cache-control                : chr "max-age=0"
  ..$ access-control-allow-origin  : chr "*"
  ..$ status                       : chr "200"
  ..$ statusmessage                : chr "OK"
  ..- attr(*, "class")= chr [1:2] "insensitive" "list"
 $ cookies    : list()
 $ content    : raw [1:1142953] 1f 8b 08 00 ...
 $ times      : Named num [1:6] 0 0.4 0.518 0.879 1.898 ...
  ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
 $ config     :List of 1
  ..$ httpheader: Named chr [1:2] "x-dropbox-metadata" "OAuth oauth_consumer_key=\"xxxxxx\", oauth_nonce=\"xxxxxxxx\", oauth_signature=\"xxxxxxxxxxxxxx\", o"| __truncated__
  .. ..- attr(*, "names")= chr [1:2] "Accept" "Authorization"
  ..- attr(*, "class")= chr "config"
 - attr(*, "class")= chr "response"


raw.content.of.file <- content(response)
head(raw.content.of.file)
[1] 1f 8b 08 00 00 00

基本上我想以某种方式将 raw.content.of.file 对象保存到名为 downloaded.RData 的文件中,这应该与 test.RData 相同,否则将至少能够加载 test.RData 进入我的全球环境。

basically I want to somehow save the raw.content.of.file object into a file called downloaded.RData, which should be identical to test.RData or failing that at least be able to load the objects that are in test.RData into my Global environment.

推荐答案

您可以使用 writeBin 编写 Rda 文件的二进制响应内容。这是一个完整的工作示例:

You can use writeBin to write the binary response content to a Rda file. Here is a complete working example :

library(httr)
test <- 1:10
save(test, file="~/Dropbox/test.Rda")
response <- GET(url="https://dl.dropbox.com/s/9rjbjwqxid7yj53/test.Rda?dl=1")
writeBin(response$content, "test2.Rda")
rm(test)
load("test2.Rda")
test
 [1]  1  2  3  4  5  6  7  8  9 10

如果您不这样做,还有一种更简单的方法不想将二进制数据保存到文件中。您可以直接执行以下操作:

And there is an even simpler way if you don't want to save your binary data to a file. You can just do directly :

rm(test)
load(rawConnection(response$content))
test
 [1]  1  2  3  4  5  6  7  8  9 10

这篇关于使用Dropbox API读取R中的原始数据以另存为.RData文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆