Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading