How to pass an object to a function that expects a file?

Advertisements

Often, I’ll run into a problem where I want to pass an object to an imported function, but that function expects a file. So, I first write the object to a file, then pass the file as a parameter in the function call. Not only does this create a file which I need to remove, but it also interrupts piped workflows. I’d like to cut out the middleman and just pass the object to the function.

An example is ggmsa::readSSfile(), which transforms a string of dots and parentheses into a list that can be interpreted by ggmsa::gghelix().

structure <- "..(.((((...)))).....((..))....)."
write(structure, file = "/user/data/structure.txt")
data <- ggmsa::readSSfile(file = "/user/data/structure.txt", type = "Vienna")
file.remove("/user/data/structure.txt")
ggmsa::gghelix(data)

Is there an argument structure that would allow me to "trick" the function? Something like this:

read_file(file = pseudo_write(object))

>Solution :

I believe you are looking for the function textConnection:

data <- ggmsa::readSSfile(file = textConnection(structure), type = "Vienna")

This will aleviate the writing part and the removing part

Leave a ReplyCancel reply