Save output of method in variable instead of file in R - 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)

Related

How to quickly export multiple files from RStudio

I'd like to know, how can I export subsets of a dataframe in R in an automated way?
I am currently using this manual method, where I retype 'a' and 'file_name' values for every file I want to save:
data <- MS[grepl('a', MS$name),]
write.xlsx(data, 'file_path/file_name')
Any help would be very much appreciated.
I would try something like this:
lijst <- c('a','b','c') # list of the values you type for 'a'
for(a in lijst){
filename <- paste0('file_path/',a,'.xlsx')
data <- MS[grepl(a, MS$name),]
write.xlsx(data, filename)
}

How to write gzcon output to a variable in 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!

How can I "generalize" what R uses as my x and y values in a plot

I have written an executable script in R that will simply plot a graph given an input file in a tab delimited format. However, the script I wrote is specific to a single file in terms of what to use as x and y. I want to have this script be able to plot whatever file I give it. All files I will be using for this script will be in the same format: Tab delimited with 4 headers with labels a, b, c, d. Labels b,c, and d have a different name for each file. My x values for the graph will be the values under header b and y values for the graph will be the values under header c. How can I plot a graph that will use whatever is under header b and c?
My script is posted below.
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
data = read.table((args[1]), header=TRUE, fill=TRUE, sep="\t")
attach (data)
jpeg(args[2])
plot (RPMb, RPMc)
dev.off()
Instead of using attach() (which is almost never recommended), use data frame indexing to extract the relevant variables from your data variable.
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly=TRUE)
data = read.table((args[1]), header=TRUE, fill=TRUE, sep="\t")
jpeg(args[2])
x <- names(data)[2]
y <- names(data)[3]
plot (data[[x]], data[[y]],xlab=x,ylab=y)
dev.off()
You could also just use plot(data[,2],data[,3]) ...
A couple of other details/comments:
it's generally best to avoid naming variables for built-in functions such as data. It will usually work, but occasionally it will bite you.
are you sure you want JPEG output? Either PNG or PDF are usually best for line graphs, depending on whether you need a raster or a vector format ...

Loop in R to read sequentially numbered filenames and output accordingly numbered files

I'm sure this is very simple, but I'm new to doing my own programming in R and haven't quite gotten a hang of the syntax for looping.
I have code like this:
mydata1 <- read.table("ph001.txt", header=TRUE)
# ... series of formatting and merging steps
write.table(mydata4, "ph001_anno.txt", row.names=FALSE, quote=FALSE, sep="\t")
png("manhattan_ph001.png"); manhattan(mydata4); dev.off()
png("qq_ph001.png"); qq(mydata4$P); dev.off()
The input file ph001.txt is output from a linear regression algorithm, and from that file, I need to output ph001_anno.txt, manhattan_ph001.png, and qq_ph001.png. The latter two are using the qqman package.
I have a folder that contains ph001 through ph138, and would like a loop function that reads these files individually and creates the corresponding output files for each file. As I said, I'm sure there is an easy way to do this as a loop function, but the part that's tripping me up is modifying the output filenames.
You can use the stringr package to do a lot of the string manipulation you want in order to generate your file names, like so:
f <- function(i) {
num <- str_pad(i, 3, pad = "0")
a <- str_c("ph", num, "_anno.txt")
m <- str_c("manhattan_ph", num, ".png")
q <- str_c("qq_ph", num, ".png")
# Put code to do stuff with these file names here
}
sapply(1:138, f)
In the above block of code, for each number in 1:138 you create the name of three files. You can then use those file names in calls to read.table or ggsave or whatever you want.

How to not overwrite file in R

I am trying to copy and paste tables from R into Excel. Consider the following code from a previous question:
data <- list.files(path=getwd())
n <- length(list)
for (i in 1:n)
{
data1 <- read.csv(data[i])
outline <- data1[,2]
outline <- as.data.frame(table(outline))
print(outline) # this prints all n tables
name <- paste0(i,"X.csv")
write.csv(outline, name)
}
This code writes each table into separate Excel files (i.e. "1X.csv", "2X.csv", etc..). Is there any way of "shifting" each table down some rows instead of rewriting the previous table each time? I have also tried this code:
output <- as.data.frame(output)
wb = loadWorkbook("X.xlsx", create=TRUE)
createSheet(wb, name = "output")
writeWorksheet(wb,output,sheet="output",startRow=1,startCol=1)
writeNamedRegion(wb,output,name="output")
saveWorkbook(wb)
But this does not copy the dataframes exactly into Excel.
I think, as mentioned in the comments, the way to go is to first merge the data frames in R and then writing them into (one) output file:
# get vector of filenames
filenames <- list.files(path=getwd())
# for each filename: load file and create outline
outlines <- lapply(filenames, function(filename) {
data <- read.csv(filename)
outline <- data[,2]
outline <- as.data.frame(table(outline))
outline
})
# merge all outlines into one data frame (by appending them row-wise)
outlines.merged <- do.call(rbind, outlines)
# save merged data frame
write.csv(outlines.merged, "all.csv")
Despite what microsoft would like you to believe, .csv files are not excel files, they are a common file type that can be read by excel and many other programs.
The best approach depends on what you really want to do. Do you want all the tables to read into a single worksheet in excel? If so you could just write to a single file using the append argument to the write.csv or other functions. Or use a connection that you keep open so each new one is appended. You may want to use cat to put a couple of newlines before each new table.
Your second attempt looks like it uses the XLConnect package (but you don't say, so it could be something else). I would think this the best approach, how is the result different from what you are expecting?

Resources