P.Stock
P.Stock

Reputation: 65

How to use a sequential combination of YAML parameters in Rmarkdown

How can I use a sequence of parameters, assigned using YAML in Rmarkdown? I am trying to write a csv file, but I can only use one param.

For instance. I have:

title: "My Title"
params:
  month: "March"
  person: "CEO"
  extention: ".csv"

I want to add all of the assigned parameters as a single continuous word:

write.table(d, file=params$month params$person params$extention, quote = F, fileEncoding = "UTF-8", sep = ";", row.names=FALSE)

However, its not possible like that, it only reads one parameter if put like that.

What is the right way to do that?

Upvotes: 0

Views: 196

Answers (1)

R. Prost
R. Prost

Reputation: 2088

Your file attribute has to be a string so you can use paste to put it all together as one string. (paste0 is the same as paste but doesn't put a separation between the variables by default)

write.table(d, file = paste0(params$month,params$person,params$extention),
             quote = F, fileEncoding = "UTF-8", sep = ";", row.names = FALSE)

will give "MarchCEO.csv"

In your case you only had month for the name of the file, the other variables were not taken into account because of the space, so it thought it was some other attribute..

if you want the line to be more readable you can define the name first like that:

myfilename <- paste0(params$month,params$person,params$extention)

write.table(d, file = myfilename, quote = F, fileEncoding = "UTF-8",
            sep = ";", row.names = FALSE)

Upvotes: 1

Related Questions