How to save text from a list to separately named pdf files using for loop

Hi everyone. I am struggling with a for loop. I have 10 separate rows of text in a list file called text_objects. I have another object called title_text which contains the titles for each pdf I want to create. Here is latest version fo the for loop I have tried:

output_dir <-- "~/Downloads/"

for (i in 1:length(text_objects)) {
pdf(file = paste0(output_dir, title_text[i], ".pdf"))
print(text_objects[[i]])
dev.off()
}

I actually get all the named pdf files, but they are all only 4 kb each and non of them will open (damaged file). Sorry, but I can't seem to see what I have wrong, despite much searching. Any suggestions are greatly appreciated.

Hello @kjd

pdf opens a graphical device, so you need graphical statements to send data to it.
See https://stackoverflow.com/questions/56809402/how-to-print-or-cat-text-into-pdf-in-r-without-markdown for an example.

Alternatives are using RMarkdown (with a template that will not show a title , author and date) or directly pandoc.

Hi @HanOostdijk,

Thanks for the reply. It filled in some ideas for me, and now I have all the scraped pages saved as individual PDFs (went the markdown route). I also saved them as plain text files too, because, why not. Now I can check frequencies, associations, topics and others across a nice sized corpus. Thanks again for the suggestion.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.