3
votes

I'm having troubles name my output PDF file from my "Knit with Parameters" RMD file. my YAML header looks something like this:

---
title: "Discounting"
output: pdf_document 
#with more parameters for later
---

My file is called "Discounting Script.rmd"

Now, when I run my rmd file, the output PDF is called "Discounting Script.pdf" I'd like to make it something like "Discounting " & Sys.Date() & ".pdf" but I seem to be unable to. I could "save as" for the rmd file if I'm able to do that somewhere in the code below to get a similar name. I can't use rmarkedown::render because I have to knit with parameters (and I've tried the render, and I only get errors)

I want to add a part under output so it looks something like this:

---
title: "Discounting"
output: pdf_document
     output_file: "Discounting " & Sys.Date() & ".pdf"
---

I've also tried"

 ---
    title: "Discounting"
    output: pdf_document
         output_file: paste0("Discounting ",Sys.Date(),".pdf")
    ---

But neither work and they both throw errors. This should be a simple action to complete, but I can't find anything online that helps me.

Here's the 'whole file' everything under the YAML header works fine, it's just the header I have problems with.

---
title: " Discounting"
output: pdf_document 
  output_file: `r paste0("Discounting ", Sys.Date(), ".pdf")`
params:
  CPI: 
    label: "CPI:"
    value: .02
  Federal_Bonds: 
    input: slider
    label: "Federal Bonds are on rows:"
    min: 6
    max: 12
    value: [7,8]
    step: 1
    round: 1
    dragRange: true
  Provincial_Bonds:  
    label: "Provincial Bonds are on rows:"
    min: 10
    max: 35
    step: 1
    round: 1
    value: [15, 28]
  Corporate_Bonds:  
    label: "Corporate Bonds are on rows:"
    value: [35,86]
    min: 30
    max: 100
    round: 1
    step: 1
---

```{r eval = TRUE, echo= FALSE, warning = FALSE, results = "asis", message = FALSE}
Federal_Start <- params$Federal_Bonds[1]
Federal_End <-params$Federal_Bonds[2]
Provincial_Start <- params$Provincial_Bonds[1]
Provincial_End <- params$Provincial_Bonds[2]
Corporate_Start <- params$Corporate_Bonds[1]
Corporate_End  <- params$Corporate_Bonds[2]
CPI <- params$CPI
```


```{r eval = TRUE, echo= FALSE, warning = FALSE, results = "asis", message = FALSE}
#loading packages
library(plyr)
library(dplyr)
library(kableExtra)
library(scales)
library(ggplot2)
library(RODBC)
library(data.table)
library(DT)
library(treemapify)
library(devtools)
library(digest)
library(plotly)
library(shiny)
library(ggrepel)
library(readxl)
library(tvm)
library(jrvFinance)
library(lubridate)
```

```{r eval = TRUE, echo= FALSE, warning = FALSE, results = "asis", message = FALSE}
printSectionTitle <- function (title) {
 cat("","\n\n")
 template <- title
 cat(sprintf(template),"  \n")
}
```

```{r eval = TRUE, echo= FALSE, warning = FALSE, results = "asis", message = FALSE} 
port_table <- c(Federal_Start, Federal_End, Provincial_Start, Provincial_End, Corporate_Start, Corporate_End, CPI)
```


```{r eval = TRUE, echo= FALSE, warning = FALSE, results = "asis", message = FALSE}
#Formatting the tables and creating a pretty report
printSectionTitle(sprintf("# %s","The Company"))
printSectionTitle(sprintf("# %s","Merging of Bond Portfolio with Other Risk Free Assets"))
printSectionTitle(sprintf("## %s",paste0("as at ", format(as.Date(Start_Date), "%d %B %Y"))))


colnames(port_table) <- c("","")
port_kable <- kable(port_table, align = "r",col.names = NA)
port_kable <- row_spec(port_kable, row = c(4, 7), bold = TRUE, underline = TRUE)
print(kable_styling(port_kable, bootstrap_options = "striped",latex_options = "hold_position", position ="left"))
```

The above code gets this error Error in yaml::yaml.load(..., eval.expr = TRUE) : Scanner error: mapping values are not allowed in this context at line 3, column 14 Calls: ... parse_yaml_front_matter -> yaml_load -> Execution halted

1
Yaml headers don't take Excel code (first example) or raw R code (second example), but you can use `r paste0(...)` (as in stackoverflow.com/q/23449319). - r2evans
While I think that this is a duplicate of that link, your comment "can't use rmarkdown::render" is also an issue: how are you trying to use parameters, and what is happening? Please be specific, including providing a minimal working example or at least the verbatim errors you get (with substantiating code). - r2evans
title: "IBNR Discounting" output: pdf_document output_file: r paste0("IBNR Discounting ", Sys.Date, ".pdf") gets the following error Error in yaml::yaml.load(..., eval.expr = TRUE) : Scanner error: mapping values are not allowed in this context at line 3, column 14 Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous> Execution halted - morg
Comments are horrible at maintaining layout of non-trivial code or data. If it's relevant to the question, please add it to the question. Thanks! (But if that's your literal code, do you instead mean to use Sys.Date(), including the parens?) - r2evans
Updated, with code. - morg

1 Answers

2
votes

One option is to use rmarkdown::render to render your R Markdown output (called discounting.Rmd here), from a separate .R script. You can assign your parameters in the params argument:

rmarkdown::render(
  "test_knit.Rmd",
  params = list(cpi = 10),
  output_file = paste0("Discounting_", Sys.Date(), ".pdf")
)

And your Rmd file would look something like this:

---
params:
  cpi:
output: pdf_document
title: "Discounting"
---

### Content

```{r}
params$cpi * 5
```

This will produce a PDF output named with today's date.