How to input my own "metadata" in "yaml front matter" from a r script ?

Welcome to community.rstudio.com !

I'm beginner in Rstudio IDE.
It's been several weeks now that I research how can I input metadata (to yaml front matter) in rmardown file which from r file or r script.

To resume, I made template.tex with my own pandoc variable then, in r script, I wrote some sqlQuery and in the end I use render("file.Rmd").
To finish my file.Rmd look likes :


online: file.cls
output:
pdf_document:
template : main.tex
my_metadata1 : "r metadata1"

But Rstudio returns warnings :

[WARNING] Could not parse YAML metadata at line 29 column 1: :16:33: Unexpected '
'

or

Could not find data file templates\LaTex\main.tex
Error: pandoc document conversion failed with error 97

I try finding solutions but I haven't had the results what I want.

It looks like you might be looking for rmarkdown parameters. They let you specify (surprisingly) parameters to a report/rmarkdown document, such as a data set/SQL query to use for the rest of the report.

I suspect you have an issue with tabs in the YAML headings - the indentation of the output and template fields is crucial.

To give a concrete example: have a look at my repo https://github.com/jlacko/RPTG

The important files are Report.tex (LaTeX template) and Report.rmd (R Markdown file using the template, known to be working).

Don't worry if you can not understand a word of the text, it is just a placeholder (Czech translation of Cicero's Oratio in Catilinam Prima in Senatu Habita).

Hello jlacko, thanks for your answer.

The indentation of the output in my Rmd are correct It's on RStudio community that this is badly displayed... Otherwise I totally respect the identation.

Thanks you for your link !

Hello jim89, thanks for your answer.

I tryed knit with parameters but it's always the same things. RStudio returns the same warnings... And in parameters RStudio does not apply my R script.
In my global environment I have every correctly data but RStudio returns this warnings :
! LaTeX Error: File `.cls' not found.

! Emergency stop.
<read *>

Error: Failed to compile technical_file.tex. See technical_file.log for more info.

Same if I write in metadata the name of cls file.

About this, you can format text as code by enclosing it between 3 backticks e.g.

With this

```
---
online: file.cls
output:
    pdf_document:
          template : main.tex
my_metadata1 : "`r metadata1`"
---
```

you get this

---
online: file.cls
output:
    pdf_document:
          template : main.tex
my_metadata1 : "`r metadata1`"
---
1 Like

Hello everybody,

So in my R script, I write

data1 <- sqlQuery(con, "SELECT ...........")
data2 <- sqlQuery(con, "SELECT ...........")

own_metadata = list( metadata1 = "data1",
                     metadata2 = "data2")

render(InputFile, params = own_metadata , clean = FALSE ,encoding = "UTF-8")

Then in my RMarkdown file

---
output:
  pdf_document:
      keep_tex: yes
      template: TemplateTex/main.tex
params:
  online: TemplateTex/classe
  metadata1: "`r data1`"
  metadata2: "`r data2`"
---

Knowing that, my main.tex it look likes

\documentclass[a4paper,landscape]{$online$} %macro is in my .cls

\data1{$if(data1)$$data1$$endif$}

\data1{$if(data2)$$data2$$endif$}

\begin{document}

\makefile 

\end{document}

and now I have in my .log file

**./fiche_amenagement_ext.tex
(fiche_amenagement_ext.tex
LaTeX2e <2018-12-01>
! Missing \endcsname inserted.
<to be read again> 
                   \protect 
l.2 

and in my R Markdown window

Error in eval(parse_only(code), envir = envir): objet 'data1' not found Calls: <Anonymous> ... inline_exec -> hook_eval -> withVisible -> eval -> eval Execution stopped

It seems to me that when I make a render() it doesn't detect my variables in the GlobalEnvironment... And I tried to make save(data1, file="data1.RData") and in yaml front matter, metadata1: "` r load("data1.RData")`" but it returns an error message with yaml::yaml.load(...)

You're right that render() won't detect variables in the Global Environment, iirc render() runs the document in a fresh R session, so your current workflow won't work.

I'm not clear on exactly what your main aim is here, but I'd recommend reading through the parameters section of the Rmd book here to get some more details.

What is the purpose of metadata1/metadata2? Are you trying to create the same report for two sets of metadata, or do something else?

My goal is to generate my reports automatically because my basic work is located on a database that I handle every day. So the principle is that after that I don't touch the codes anymore.

I am quite a perfectionist so I have my own Latex template and its presentation is very graphic.

So how can I access my .GlobalEnvir ? I tried with knit(), knit2pdf() and many others, the result remains the same.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.