Error: Invalid YAML front matter (ends with ':') - r

The loop in the params not work, and i receive this message:
Error: Invalid YAML front matter (ends with ':')
The YAML is:
---
title: "Growth"
date: "16 de julho de 2019"
output:
html_document:
toc: true
toc_depth: 5
params:
data:
default
---
I see the default thing in other post, but he not work too.
The code is below:
data <- params$data
company <- c("usim5_test.pkl", "vale_test.pkl", "csn_test.pkl", "GGBR4_test.pkl")
for (i in company) {
rmarkdown::render(
input = "C:\\Users\\Rafael\\Documents\\ibov\\gurufocus\\macro\\growth_ibov1.Rmd",
output_file = paste0("Growth_",str_sub(i, 1,5),".html"),
params = list(
data = i
)
)
}
I expect render the reports for all the files

Kinda late (three years later), but i wanted to do the something and had the same problem.
I don't know if there is now easier ways to do this since I'm not a pro on R, still, it works if you first declare data = NULL in the YAML.

Related

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
---
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. Where it does not work is it's not able to provide me the "author" portion with the corresponding name with id value. Do I do a nested for loop?
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
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.
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. By creating them, you should be able to get their values.

R read_yaml() reads a vector as parameter

I would like to read a .yaml file to get yaml parameters for a Rmarkdown report.
Original I have a yaml header to define a vector.
---
params:
ids: !r c(2455, 2490)
---
and it works, where params$ids is a vector.
However, if I put ids: !r c(2455, 2490) into a report_params.yaml file, and read that yaml file by
report_params <- yaml::read_yaml("report_params.yaml")
now report_params$ids is a string 'c(2455, 2490)'.
so what did I miss, and how should I fix this?
The YAML default handler uses !expr rather than !r.
report_params.yaml:
---
params:
ids: !expr c(2455, 2490)
---
yaml::read_yaml("report_params.yaml")
#> $params
#> $params$ids
#> [1] 2455 2490

reticulate python engine - Use r as a name for Python object shared between multiple chunks

I am writing an R Markdown document using the Python engine of {reticulate}. I am quite happy with how it works.
The only thing is, I cannot use r as a Python object name that I'm going to use in multiple chunks.
---
title: "Untitled"
output: html_document
---
## Object name `r`
```{python}
r = 10
print(r) ##> 10
```
```{python}
print(r) ##> <__main__.R object at 0x119ad37d0>
```
I understand r is a good name when we use R objects from within a Python chunk. Since I know I am not going to do that in my project, I would like to use r as a name for my Python object.
Is there any way to change the name, r, for the R object created by reticulate? Or, to tell reticulate not to create r object?
I am aware of the two straightforward workarounds
Don't use r for Python objects.
Write everything in one big chunk and not share r between Python chunks.
but I'd like to have more freedom.
The object name r is special since it is used to communicate between R and python. The same holds for py in R:
---
title: "Untitled"
output: html_document
---
## Object name `r`
```{python}
foo = 10
print(foo) ##> 10
```
```{r}
library(reticulate)
py$foo ##> [1] 10
```
```{r}
foo <- 10
```
```{python}
print(foo) ##> 10
print(r.foo) ##> 10.0
```
Avoiding the use of r as an opject name is therefore the only good possibility I can see.
Still working on the details but I think setting source and output knit hooks may help. My dirty first try looks like this:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
knitr::knit_hooks$set(
source = function(x, options) {
if (!is.null(options$rsub)) x = gsub(options$rsub, 'r', x, fixed = TRUE)
sprintf('<div class="%s"><pre class="knitr %s">%s</pre></div>\n', "source", tolower(options$engine), x)
},
output = function(x, options) {
if (!is.null(options$rsub)) x = gsub(options$rsub, 'r', x, fixed = TRUE)
paste0('<pre><code>', x, '</code></pre>\n')
}
)
```
```{python, rsub='rrr'}
rrr = 10
print(rrr)
```
```{python, rsub='rrr'}
print(rrr)
```

xtable inside of a for loop in Rmarkdown is printing {} in output when floating = FALSE [duplicate]

I'm creating a R Markdown document using knitr and am running into trouble using xtable to create a table. My table is very large and I'm trying to reduce the size using the size command in the print statement. The issue I'm running into is that the command seems to add two extra curly braces which show up in the PDF, one before the table and one after.
Does anyone know a way to fix this?
MWE:
---
output:
pdf_document:
keep_tex: yes
tables: true
---
```{r, results='asis', echo=FALSE}
library(xtable)
my.df <- data.frame(matrix(c(1:18),nrow=2))
glossaryprint <- xtable(my.df, caption="Summary of Terms")
print(glossaryprint,
comment=FALSE,
floating=FALSE,
size="footnotesize"
)
```
Note: The issue that is subject of the question and this answer has been resolved in xtable 1.8-2.
Although the question has been self-answered with a workaround, I think for other users some more details might be helpful.
What happens?
To understand what is happening here, we need to take a close look at the conversion steps the document undergoes on its way from RMD to PDF. The steps are:
RMD --> MD --> TEX --> PDF
Let's look at the files in reversed order:
PDF: (generated from TEX by pdflatex)
TEX: (generated from MD by pandoc)
% …
\{\footnotesize
\begin{tabular}{rrrr}
\hline
& X1 & X2 & X3 \\
\hline
1 & 1 & 3 & 5 \\
2 & 2 & 4 & 6 \\
\hline
\end{tabular}
\}
% …
MD (generated from RMD by knitr)
---
output:
pdf_document:
keep_tex: yes
---
{\footnotesize
\begin{tabular}{rrrr}
\hline
& X1 & X2 & X3 \\
\hline
1 & 1 & 3 & 5 \\
2 & 2 & 4 & 6 \\
\hline
\end{tabular}
}
RMD: (source file)
---
output:
pdf_document:
keep_tex: yes
---
```{r, results='asis', echo=FALSE}
library(xtable)
mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))
print(mytable,
comment = FALSE,
floating = FALSE,
size = "footnotesize"
)
```
Recall: The problem is, that there are visible curly braces in the PDF. Where do they come from?
They are the result of the escaped curly braces in the TEX file (\{ and \}).
These curly braces also exist in the MD file, but there they are not escaped.
So we know two things by now: We see the curly braces because they are escaped and they are escaped by pandoc.
But why do these curly braces exist at all? print.xtable outputs them when a size is specified. The goal is to create a group and to apply size only within that group. (With floating = TRUE, no grouping by curly braces is required because there is a figure environment whithin which the size is set. The curly braces are printed anyways.)
And why does pandoc escape that pair of curly braces but leaves all the other curly braces (e.g. in \begin{tabular}) unescaped? This is because pandoc is supposed to escape special characters that are meant literally but leave raw LaTeX unescaped. However, pandoc does not know that the outer curly braces are LaTeX commands and not text.
(With floating = TRUE the problem does not occur because the curly braces are within a figure environment which is recognized as LaTeX.)
Solutions
After having understood the problem, what can we do about it? One solution has already been posted by the OP: Abstain from spefifying size in print.xtable and insert the footnotesize command manually:
---
output:
pdf_document:
keep_tex: yes
---
```{r, results='asis', echo=FALSE}
library(xtable)
mytable <- xtable(data.frame(matrix(c(1:6), nrow = 2)))
cat("\\begin{footnotesize}")
print(mytable,
comment = FALSE,
floating = FALSE
)
cat("\\end{footnotesize}")
```
However, on the long run it would be nice if xtable (current version: 1.8-0) generated LaTeX code that survives the pandoc conversion. This is quite simple: print.xtable checks if size is set and if so, inserts { before the size specification and } at the end of the table:
if (is.null(size) || !is.character(size)) {
BSIZE <- ""
ESIZE <- ""
}
else {
if (length(grep("^\\\\", size)) == 0) {
size <- paste("\\", size, sep = "")
}
BSIZE <- paste("{", size, "\n", sep = "")
ESIZE <- "}\n"
}
This small modification replaces { with \begingroup and } with \endgroup:
if (is.null(size) || !is.character(size)) {
BSIZE <- ""
ESIZE <- ""
}
else {
if (length(grep("^\\\\", size)) == 0) {
size <- paste("\\", size, sep = "")
}
BSIZE <- paste("\\begingroup", size, "\n", sep = "")
ESIZE <- "\\endgroup\n"
}
For LaTeX, this makes no difference, but as pandoc recognizes \begingroup (as oppsed to {) it should solve the problem. I reported this as a bug in xtable and hopefully the issue will be fixed in future versions.
I was able to fix this by not including the size parameter in the print statement but rather directly before and after the chunk.
\begin{footnotesize}
#chunk
\end{footnotesize}

How to format Gmisc::htmlTable

Below is an rmarkdown document that can be pasted into rstudio.
My problem is that output from htmlTable is prepended/appended with cruft from the htmlTable attributes.
---
title: "SO_question"
author: "AC"
date: "Wednesday, May 28, 2014"
output:
html_document:
theme: readable
---
My heading
============
This is a few tables. Notice that `htmlTable` prints `[1]"` before each table and `" attr(,“class”) [1] “htmlTable” “character” [1] “` after each table. How can I avoid this?
``` {r html_table, results='asis', echo=FALSE, message=FALSE}
library("htmlTable")
library("reshape2")
#Chick weight example
names(ChickWeight) <- tolower(names(ChickWeight))
chick_m <- melt(ChickWeight, id=2:4, na.rm=TRUE)
for (i in unique(chick_m$diet)) {
diet <- subset(chick_m, diet==i)
table_to_print <- dcast(chick_m, time ~ variable, mean)
print(htmlTable(table_to_print, rgroup=c(""), n.rgroup=nrow(table_to_print)))
}
```
Bonus question: How to format the last row in each table as bold text (suited for a 'total' row)?
Rather than using print on your htmTable, use cat to properly render it.
for (i in unique(chick_m$diet)) {
diet <- subset(chick_m, diet==i)
table_to_print <- dcast(chick_m, time ~ variable, mean)
cat(htmlTable(table_to_print, rgroup=c(""), n.rgroup=nrow(table_to_print)))
}
There is a print.htmlTable function that is called when a print is performed on an object from the htmlTable function. It should automatically call the cat, not sure if this was true May '14 but it works today.
In the 1.1 version of the htmlTable-package (the function was separated from the Gmisc-package) there is a total option:
for (i in unique(chick_m$diet)) {
diet <- subset(chick_m, diet==i)
table_to_print <- dcast(chick_m, time ~ variable, mean)
print(htmlTable(table_to_print, total=TRUE))
}
Note: You do not need to specify the rgroup element if you are not using it.
#author

Resources