我在使用“带参数编织”功能的RMD文件时,无法命名输出的PDF文件。我的YAML头部大致如下:
---
title: "Discounting"
output: pdf_document
#with more parameters for later
---
我的文件名为“Discounting Script.rmd”
当我运行我的rmd文件时,输出的PDF文件名为“Discounting Script.pdf”。我想将它改成类似于“Discounting ”& Sys.Date() & “.pdf”的东西,但我好像无法做到。如果可以在下面的代码中某个地方保存rmd文件,以获得类似的名称,那么我可以尝试“另存为”rmd文件。由于我必须使用参数编织,所以无法使用rmarkdown::render(我已经尝试过render,但只得到错误)。
我想在输出部分下添加一个部分,使其看起来像这样:
---
title: "Discounting"
output: pdf_document
output_file: "Discounting " & Sys.Date() & ".pdf"
---
我也尝试过这个方法。
---
title: "Discounting"
output: pdf_document
output_file: paste0("Discounting ",Sys.Date(),".pdf")
---
但是这两种方法都没起作用,它们都报错了。这应该是一个很简单的操作,但我找不到任何在线帮助。
这是整个文件,YAML头以下的所有内容都没问题,就只有头部出了问题。
---
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"))
```
上面的代码出现了以下错误 yaml::yaml.load(..., eval.expr = TRUE) 出错: Scanner error: mapping values are not allowed in this context at line 3, column 14。 调用: ... parse_yaml_front_matter -> yaml_load -> 程序已停止执行。
\
r paste0(...)``(例如https://dev59.com/d2Ag5IYBdhLWcg3wjrkZ)。 - r2evansr paste0("IBNR贴现", Sys.Date, ".pdf")
出现以下错误: 错误:在第3行,第14列上下文中不允许映射值的扫描器错误。 调用:<Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous> 执行已停止 - morgSys.Date()
,包括圆括号?) - r2evans