How to write gzcon output to a variable in R? - r

The documentation for gzcon states:
Use a writable rawConnection to compress data into a variable.
Here's my code:
output <- raw(0)
z <- rawConnection(output, "wb")
zz <- gzcon(z, text = TRUE)
writeLines("TEST", zz)
close(zz)
So at this point, I'm not sure how I can retrieve the compressed data.
I would like to retrieve the value using rawConnectionValue but gzcon has messed up z.
Thanks in advance!

Related

Save output of method in variable instead of file in R

Is there a way to store an output file of a method in a variable instead of a file?
I want to use the method:
decode("base64.txt", "output.tif")
But instead of writing the output in "output.tif", I want to store it in a variable to plot it:
# Something like this
output <- decode("base64.txt", "output.tif")
plotRGB(output, b=3, g=2, r=1, stretch="lin")
Is there a way to do this?
You could use a raw connection to get the data
rawcon <- rawConnection(raw(), "w")
base64::decode("base64.txt", rawcon)
output <- rawConnectionValue(rawcon)
close(rawcon)
output
You could also use a different package for base64 encoding like openssl
buf <- readBin("base64.txt", raw(), file.info("base64.txt")$size)
output <- openssl::base64_decode(buf)

How to open .json.gz format file and create dataframe n R?

I have downloaded abcd.json.gz file. The file size is 10GB I want to parse it and create data frame from it in an efficient way. How to do it in R ?
This is how I do it without unpacking the file first:
con <- gzfile("file.json.gz", "rt")
lines <- readLines(con)
close(con)
df <- jsonlite::stream_in(textConnection(lines), verbose = FALSE)
This uses line-by-line processing of JSON, which is the only JSON format I've encountered in the wild so far.

decode base64 to a raster

it is a rather untypical scenario, I am using R Custom visual in PowerBI to plot a raster and the only way to pass data is by using a dataframe.
this what I have done so far,
generate a raster in R
save it to file using SaveRDS
encoded the file as a base64 and save it as a csv.
now using this code I manage to read the csv, load it to a dataframe combine al the rows
my question is how to decode it back to a raster Object ?
here is a reproducible example
# Input load. Please do not change #
`dataset` = read.csv('https://raw.githubusercontent.com/djouallah/keplergl/master/raster.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
library(caTools)
library(readr)
dataset$Value <- as.character(dataset$Value)
dataset <- dataset[order(dataset$Index),]
z <- paste(dataset$Value)
Raster <- base64decode(z,"raw")
here is the result
it turned out the solution is very easy, saveRDS has an option to save with ascii = TRUE
saveRDS(background,'test.rds',ascii = TRUE,compress = FALSE)
now I just read it as humain readbale format (which is easy to load to PowerBI) and it works
fil <- 'https://raw.githubusercontent.com/djouallah/keplergl/master/test.rds'
cony <- gzcon(url(fil))
XXX <- readRDS(cony,refhook = NULL)
plotRGB(XXX)

Base64 encoding a .Rda file

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.
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.

for normalization of microarray data

I want to normalize data using RMA in R package. but there has problem it does not read .txt file. Please tell me, "what I do for normalizing data from .txt file?"
reply please
Basically all normalization methods in Bioconductor are based on the AffyBatch class. Therefore, you have to read your textfile (probably a matrix) and create an AffyBatch manually:
AB <- new("AffyBatch", exprs = exprs, cdfName = cdfname, phenoData = phenoData,...)
RMA needs ExpressionSet structure. After reading the file (read.table()) and cleaning colnames and row.names convert the file to matrix and use:
a<-ExpressionSet(assayData=matrix)
If didnt work, import your *.txt data to flexarray software which can read it and do rma.
This may work.
I use normalizeQuantiles() function from Limma R package:
library(limma)
mydata <- read.table("RDotPsoriasisLogNatTranformedmanuallyTABExport.tab", sep = "\t", header = TRUE) # read from file
b = as.matrix(cbind(mydata[, 2:5], mydata[, 6:11])) # set the numeric data set
m = normalizeQuantiles(b, ties=TRUE) # normilize
mydata_t <- t(as.matrix(m)) # transpose if you need

Resources