Hi all,
Would you know if there is any way to pass external variables to the bash chunks?
For example, I have python chunk:
{python}
output_dir = "/Users/Santa/results"
Then I would like to execute something like that:
{bash}
./give_gifts_to_chilren -o ((output_dir))/children_addresses.txt
where ((output_dir))
comes from the python environment.
There was a previous thread How to pass arguments to bash chunk defined as variables in R? , but the response is a bit unrelated, because it is not about how to execute bash commands from R, rather than how to have bash commands as separate chunks
Thank you in advance for your help and time,
Andrei
cderv
December 28, 2023, 10:34am
2
bash
chunk as a engine.opts
which allows to pass some options
This book showcases short, practical examples of lesser-known tips and tricks to helps users get the most out of these tools. After reading this book, you will understand how R Markdown documents are transformed from plain text and how you may...
Those advanced engine are not well documented, but you can find there source code at
eng_interpreted = function(options) {
engine = options$engine
code = if (engine %in% c('highlight', 'Rscript', 'sas', 'haskell', 'stata')) {
f = wd_tempfile(engine, switch(engine, sas = '.sas', Rscript = '.R', stata = '.do', '.txt'))
write_utf8(c(switch(
engine,
sas = "OPTIONS NONUMBER NODATE PAGESIZE = MAX FORMCHAR = '|----|+|---+=|-/<>*' FORMDLIM=' ';title;",
NULL
), options$code), f)
on.exit(unlink(f), add = TRUE)
switch(
engine,
haskell = paste('-e', shQuote(paste(':script', f))),
sas = {
logf = sub('[.]sas$', '.lst', f)
on.exit(unlink(c(logf, sub('[.]sas$', '.log', f))), add = TRUE)
f
},
stata = {
logf = sub('[.]do$', '.log', f)
This file has been truncated. show original
Basically a command string is built, and it will be run with system2()
Since knitr there is now a eng_exec
which can be used to create an engine for any command line tool. So you could also use that to create your own bash engine
Hope it helps
system
Closed
February 11, 2024, 10:35am
3
This topic was automatically closed 45 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.