Using R variable to determine date in blogdown talk - r

this may be trivial or simply impossible, but I would like to allow the date of a blogdown "talk" to be determined by a R expression. The way I thought about it something like that
# Some R code to define a variable 't0', which is a string to contain the start date in
# a format that `blogdown` will like for the yaml field 'date'
---
title: My title here
date: '`r t0`'
### Other yaml variables
---
I assumed that that would work, but instead, I get an error
Quitting from lines X-XX ()
Error in eval(parse_only(code), envir = envir) : object 't0' not found
Looks like the R code is not "ported" onto the yaml? (I have managed in blogdown to use the !expr SOME_R_INLINE_EXPRESSION to add a variable to the yaml, but in this case it just doesn't seem to work...).
Am I missing something obvious?
Thanks
Gianluca

Related

How to pass args to makeindex in tinytex?

In this previous question: how to use zhmakeindex instead of makeindex in bookdown, I see there is a way to pass arguments to makeindex in tinytex, for example setting
options(tinytex.makeindex.args = c('-z', 'pinyin')).
I'm currently trying to use the nomencl package, which requires passing arguments to makeindex, as described here: https://abhigupta.io/2021/11/08/nomenclature-in-latex.html, but can't get them to work in tinytex.
MWE (this is test.tex:
\documentclass{article}
\usepackage{nomencl}
\makenomenclature
\begin{document}
Here is an example:
\nomenclature{\(c\)}{Speed of light in a vacuum}
\nomenclature{\(h\)}{Planck constant}
\printnomenclature
\end{document}
Then, in my R terminal I would set
options(tinytex.makeindex.args = c('test.nlo', '-s', 'nomencl.ist', '-o', 'test.nls'))
When I run tinytex::pdflatex('test.tex'), I get a PDF that says "Here is an example:", but no nomenclature.
I'm not sure if I'm passing the arguments correctly or if there's something else I'm missing, but any help is appreciated.

Unexplained R failure: Error: attempt to use zero-length variable name

I am trying to specify the size of a plot dynamically in RMarkdown. Because it is a facetted plot with a dynamic number of plots, the aspect ratio needs to change.
I'm using this code:
...
jobs_levels <- 9
```
#### Figure 3.1.1. Effect of Inidivdual Interventions in Living Rooms
```{r figure_3_3_1, fig.height=jobs_levels}
jobs_levels
print(figure_3_3_1)
```
For the sake of simplicity, I've hard coded the jobs_levels (the number of levels in the facetting factor). As you can see, I set the value very clearly,and then use it in the very next code chuck. I can see the value clear as day in the environment. It is 9. But I get this:
Error in eval(ele) : object 'jobs_levels' not found
> ```{r figure_3_3_1, fig.height=jobs_levels}
Error: attempt to use zero-length variable name
When I run this in batch mode, it crashes. Any idea what is going on with this?
I even put in the extra lines:
jobs_levels
to debug. EAch time I run one of those lines with cotrl enter, it evaluates right, but I see the error message again too...
The error message makes it look as though you are trying to evaluate the chunk header as R code. You had
```{r figure_3_3_1, fig.height=jobs_levels}
The first two backticks would be parsed as a zero length variable name, as the error message states.
You can't run R Markdown code as R code, you need to use rmarkdown::render or a related function to run it.

R package rcites: bulk enquiry using functions "spp_taxonconcept" and "map"

I have a csv file with a list of bird species (heading=Sci.Name) which I would like to bulk enquire their CITES appendix listing using the "spp_taxonconcept" function in the package rcites.
After setting the token, loading the csv file and all the packages needed I use the following code to generate the result in the column named "results":
bird.cites<-mutate(bird.cites,results=map(Sci.Name,spp_taxonconcept(taxonomy="CITES")))
which returns the following error msg:
x argument "query_taxon" is missing, with no default
Elements of the object "Sci.Name" was not passed to spp_taxaonconcept as the first argument, which should be query_taxon = "sci names of individual species" using the map function.
Any help with this would be greatly appreciated!
I think there are some syntax issues Try. -
bird.cites <- mutate(bird.cites,results=map(Sci.Name,spp_taxonconcept, taxonomy="CITES"))
which can also be written as -
bird.cites <- mutate(bird.cites, results = map(Sci.Name,~spp_taxonconcept(.x, taxonomy="CITES")))

R Markdown error in code when knit to HTML

I am trying to run code chunks in my markdown document. I have an R script that runs all the code that I need without any issues. Then, when I copy and paste the code into the markdown document, the code will run within the chunk, but will fail when trying to knit into an output document (html/pdf).
I had to create a safe.ifelse function to prevent r from converting my dates to a numeric format as discussed here.
The error appears to be with the code:
safe.ifelse = function(cond, yes, no){structure(ifelse(cond, yes, no), class = class(yes))
}
The error message I get is:
Line 121 Error in structure(ifelse(cond,yes,no), class = class(yes)) : could not find function "days" Calls: ... transform.data.frame ->eval->eval-> safe.ifelse-> structure Execution halted
The line of code following my safe.ifelse function is
seminoma1 = transform(seminoma1, recur.date = safe.ifelse(salvage.tx=="Yes",
date.diagnosis + days(pmax(time.rad, time.chemo, na.rm=TRUE)), NA))
Any help would be appreciated. Thanks.
I'm still too new to comment, but the only time I get an error like that is when I forget to define a function/variable or forget to source a package.
Since days() isn't part of R's base package, I think you need to add:
```{r echo = FALSE}
library("lubridate")
```

Knitr Markdown - Sourced Script returns empty lists + ugly source file output

I'm trying on a simple knitr markdown "exercise". What I want to do is to simply source my R script file and output some of the data, e.g. the mean of a vector.
My code
```{r, echo = FALSE}
summary(cars)
source("GETPrices.R")
x <- BooliReq()
mean(x$sold$listPrice)
```
Firstly, the source ("GETPrices.R") generates an ugly
Attaching package: 'jsonlite'
The following object is masked from 'package:utils':
View
in the output file. How can this be prevented?
Secondly, when I e.g. try to output the mean value (see code above), all I get is
Warning in mean.default(x$sold$listPrice): argument is not numeric or
logical: returning NA
Outputting the mean in this way works fine though in console. So what is the issue?
For the first error, use the options:
{r, echo = FALSE, message=FALSE,warning=FALSE}

Resources