简体   繁体   English

R markdown output pdf 如何根据多极柱的值动态调整文本并设置为参数

[英]How do I dynamically adjust for text based on values of multipole columns and set as parameters in R markdown output pdf

Note the YAML. I have the title and author refer to set parameters注意YAML,我有标题和作者参考设置参数

---
title: "`r params$ID_NUM`"
classoption: landscape 
author: "`r params$first` `r params$last`"
date: "8/11/2021"
output:
  pdf_document: 
    latex_engine: xelatex
    html_document: default
 params:
  ID_NUM: 1
  first: MICHAEL
  last: DOUGLAS
---

The script below is able to output the pdf and provide me with the id number as a title.下面的脚本能够 output pdf 并向我提供 ID 号作为标题。 Where it does not work is it's not able to provide me the "author" portion with the corresponding name with id value.它不起作用的地方是它无法为我提供具有相应名称和 id 值的“作者”部分。 Do I do a nested for loop?我做一个嵌套的for循环吗?

for (i in unique(df$id)) {

rmarkdown::render("Document.Rmd", 
                params = list(id_NUM = i),
                output_file=paste0(i, ".pdf"))
}

if the dataset looks like this如果数据集看起来像这样

id   first      last 
 1   Michael    Douglas
 2   Sean       Penn
 3   Kevin      Bacon

I would like the first pdf to show我想要第一个 pdf 显示

ID1
Michael Douglas

the second to show第二个显示

ID2 
Sean  Penn  

and last.最后。

ID3
Kevin Bacon

I can get the ID portion but I can't get the names.我可以获取 ID 部分,但无法获取名称。

Update::更新::

purrr::walk(df,function(x){
 i<-df$id
 p<-df$first
 z<-df$last
 rmarkdown::render(
    "Document.Rmd",
    output_file=paste0(i, ".pdf"),
    params = list(id=i,first=p,last=z)
  )
 })

Tried with this script but did not work as well.尝试使用此脚本但效果不佳。

I believe that the following will work.我相信以下会奏效。

for (i in unique(df$id)) {
first <- df$first[df$id == i]
last <- df$last[df$id == i]

rmarkdown::render("Document.Rmd", 
                params = list(id_NUM = i, first = first, last = last),
                output_file=paste0(i, ".pdf"))
}

The params values for first and last do not exist. firstlastparams值不存在。 By creating them, you should be able to get their values.通过创建它们,您应该能够获得它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM