Home > database >  R language to writing long texts?
R language to writing long texts?

Time:01-20

I'm bothering you here again with a stupid question after unsuccessfully googleing for a while. I need to send A TON (hundreds) of personalized emails. I know it is possible to send emails using R, but all the packages I've checked so far require something like body = "Body of the email", that is, they work with fixed pre-established text as the body text of the email. But, again, I need to personalize them. For example, let's have the following text:

"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.

Bilbo was very rich and very peculiar, and had been the wonder of the Shire for sixty years, ever since his remarkable disappearance and unexpected return. The riches he had brought back from his travels had now become a local legend, and it was popularly believed, whatever the old folk might say, that the Hill at Bag End was full of tunnels stuffed with treasure. And if that was not enough for fame, there was also his prolonged vigour to marvel at. Time wore on, but it seemed to have little effect on Mr. Baggins. At ninety he was much the same as at fifty. At ninety-nine they began to call him well-preserved, but unchanged would have been nearer the mark. There were some that shook their heads and thought this was too much of a good thing; it seemed unfair that anyone should possess (apparently) perpetual youth as well as (reputedly) inexhaustible wealth."

that is, a text with a couple of paragraphs. And let's have the following receivers c("Gandalf", "Saruman", "Sauron"), what I want is to create personalized texts for my emails combining both, headered:

"Dear Gandalf,

When Mr. Bilbo Baggins of Bag End... [all the aforementioned text]"

I have tried stuff like:

txt <- "When  Mr.  Bilbo  Baggins  of  Bag  End... [all the aforementioned text]" 
for(i in 1:length(rs)){
     temp <- paste("Dear", rs[i], txt) 
}

I have also tried calling this text from a .txt file and the idea is to use it then as body = temp. But I only get "unnatural" texts, with continuous lines, symbols, "", etc. Any idea regarding how to solve this situation? Thanks!

CodePudding user response:

Use sprintf, which allows you to have placeholders that are then substituted:

lapply(c("Gandalf", "Saruman", "Sauron"), 
       sprintf, 
       fmt = "Dear %s,

When Mr. Bilbo Baggins of Bag End... [all the aforementioned text]")

#[[1]]
#[1] "Dear Gandalf,\n\nWhen Mr. Bilbo Baggins of Bag End... [all the aforementioned #text]"
#
#[[2]]
#[1] "Dear Saruman,\n\nWhen Mr. Bilbo Baggins of Bag End... [all the aforementioned #text]"
#
#[[3]]
#[1] "Dear Sauron,\n\nWhen Mr. Bilbo Baggins of Bag End... [all the aforementioned #text]"

Use mapply instead of lapply if you need to substitute more then one string. Or use a for loop if you prefer.

  • Related