Base64 encoding a .Rda file

青春壹個敷衍的年華 提交于 2019-12-04 05:10:45

问题


All,

I'm trying to figure out how to put a .Rda file into Base64 encoding for it to be shipped to and from an API. I am really struggling with how to do this. Here's what I've got, but I think it's way off target:

cuse <- read.table("http://data.princeton.edu/wws509/datasets/cuse.dat", header=TRUE)

lrfit <- glm( cbind(using, notUsing) ~ age + education + wantsMore , family = binomial, data=cuse)

filename <- "C:/test.Rda"

save(lrfit, file=filename)

library("base64enc")
tst <- base64encode(filename)
save(tst, file="C:/encode.Rda")

base64decode(file="C:/encode.Rda", output = "C:/decode.Rda")

When I try to open the decode.Rda file, it throws a magic number error. Like I said, I think I'm way off base here, and any help would be appreciated. Thank you so much.


回答1:


Here a correct sequence of steps that should allow for the correct encoding/decoding

#sample data
dd<-iris
fn <- "test.rda"
fnb4 <- "test.rdab64"

#save rda
save(iris, file=fn)

#write base64 encoded version
library(base64enc)
txt <- base64encode(fn)
ff <- file(fnb4, "wb")
writeBin(txt, ff)
close(ff)

#decode base64 encoded version
base64decode(file=fnb4, output = "decode.rda")
(load("decode.rda"))
# [1] "iris"

The problem was your second save(). That was creating another RDA file with the base64 data encoded inside. It was not writing a base64 encoded version of the RDA file to disc.



来源:https://stackoverflow.com/questions/24874266/base64-encoding-a-rda-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!