Reputation: 160
As an example lets take df <- head(iris)
. How would I encode this into a base64 string. I need it for the GitHub API content argument.
At the moment I have been trying to use the base64encode()
function but get the "error in normalizePath".
Upvotes: 1
Views: 1743
Reputation: 160
Specifically for the GitHub API after some research the following code worked perfectly as it was able to decode and move the table to Github:
df1<-charToRaw(paste(capture.output(write.table(Table, quote=FALSE, row.names=FALSE , sep=",")), collapse="\n"))
base64_code<-base64enc::base64encode(df1)
This was able to be placed in the content argument of the rest API to update the file.
Upvotes: 1
Reputation: 174278
You can use serialize
to get the raw bytes of the object and convert them to base 64:
df <- head(iris)
b64_string <- base64enc::base64encode(serialize(df, NULL))
b64_string
#> [1] "WAoAAAADAAMGAgADBQAAAAAGQ1AxMjUyAAADEwAAAAUAAAAOAAAABkAUZmZmZmZ
#> mQBOZmZmZmZpAEszMzMzMzUASZmZmZmZmQBQAAAAAAABAFZmZmZmZmgAAAA4AAAA
#> GQAwAAAAAAABACAAAAAAAAEAJmZmZmZmaQAjMzMzMzM1ADMzMzMzMzUAPMzMzMzM
#> zAAAADgAAAAY/9mZmZmZmZj/2ZmZmZmZmP/TMzMzMzM0/+AAAAAAAAD/2ZmZmZmZ
#> mP/szMzMzMzMAAAAOAAAABj/JmZmZmZmaP8mZmZmZmZo/yZmZmZmZmj/JmZmZmZm
#> aP8mZmZmZmZo/2ZmZmZmZmgAAAw0AAAAGAAAAAQAAAAEAAAABAAAAAQAAAAEAAAA
#> BAAAEAgAAAAEABAAJAAAABmxldmVscwAAABAAAAADAAQACQAAAAZzZXRvc2EABAA
#> JAAAACnZlcnNpY29sb3IABAAJAAAACXZpcmdpbmljYQAABAIAAAABAAQACQAAAAV
#> jbGFzcwAAABAAAAABAAQACQAAAAZmYWN0b3IAAAD+AAAEAgAAAAEABAAJAAAABW5
#> hbWVzAAAAEAAAAAUABAAJAAAADFNlcGFsLkxlbmd0aAAEAAkAAAALU2VwYWwuV2l
#> kdGgABAAJAAAADFBldGFsLkxlbmd0aAAEAAkAAAALUGV0YWwuV2lkdGgABAAJAAA
#> AB1NwZWNpZXMAAAQCAAAAAQAEAAkAAAAJcm93Lm5hbWVzAAAADQAAAAKAAAAAAAA
#> ABgAABAIAAAL/AAAAEAAAAAEABAAJAAAACmRhdGEuZnJhbWUAAAD+"
You can undo this operation after transmission with:
unserialize(base64enc::base64decode(b64_string))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
However, if this is for github, it may be best to transmit the code that reproduces the data frame rather than binary information. This can be achieved with dput
, e.g.
ascii_string <- paste(capture.output(dput(df)), collapse = "\n")
ascii_string
#> [1] "structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4), \n
#> Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, \n
#> 1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, \n 0.2, 0.2,
#> 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, \n 1L), .Label =
#> c(\"setosa\", \"versicolor\", \"virginica\"), class = \"factor\")),
#> row.names = c(NA, \n6L), class = \"data.frame\")"
This could be written to a .R file as a way of reproducing the data frame. It can also be used to reconstitute the data frame programatically with eval(parse(text = ascii_string))
Upvotes: 6