I am currently generating word documents with Rmarkdown. But the formating is a bit difficult compared to an HTML output. One of the technical solution I could use would be to add a macro on my word file and make it execute automaticaly on opening.
The following macro should do the trick for automatic execution :
Sub Auto_Open()
Msgbox "Hello World!"
End Sub
How would one adds this macro to the doc knitted with Rmarkdown ? How do one make sure it executes correctly on opening ?
Related
I have a text file - it's a template that I want to alter and save as a another file (in this case a markdown file, but it doesn't matter):
my_test.txt:
---
title: My report
---
$my_var
As you can see there is a placeholder $my_var - I want to read in the file as a string and then put it the value of the variable my_var. How can I do this?
I already tried several things around this:
using Chain
my_var = "some string"
#chain begin
read("my_test.txt", String) # read the text file as a string
"\"\"\"" * _ * "\"\"\"" # wrap the string with triple quotes
Meta.parse # parse...
eval # ...and eval
write_to_file("reports/jmd_files/org_$(org_id).jmd") # write it to a markdown file
end
It does not work though. I tried a lot of variants and I either get an error saying my_var does not exist or the value that gets inserted is nothing (which is not what it is supposed to be.
So, it really seems to be about the environment in which this is executed, but I can't figure out what the problem is.
It shouldn't be important, but just to be sure - eventually want this to run in a loop or in a (to-be-broadcasted) function. So, hard-coding for the single example would not really be a solution.
It seems you're looking for a text templating engine like Mustache.jl. Eg:
using Mustache
function createscript()
vars = Dict("my_var" => "some string")
# Doesn't have to be a Dict - see the docs for other
# options eg. a module name can be passed
# to grab variables from it
open("data/rendered.jmd", "w") do out
render(out, read("data/reporttemplate.mustache", String), vars)
end
end
with reporttemplate.mustache containing:
---
title: My report
---
{{my_var}}
Using a text-templating engine is a lot safer and less error prone than an eval.
Not actually an answer to your question but.. are you sure you want to do it ? What you are describing looks like creating a dynamic document, but there are already lots of packages for that.
For example, Documenter.jl and Weave.jl both take markdown documents with embedded Julia scripts and are designed to run the document and compile it in HTML or PDF pages showing the script or its outputs (you choose).
I prefer going a step further and I start from a (valid) Julia script where the markdown structure is embedded with an extra comment layer (e.g. # ## This would be an H2 header) and then use Literate.jl to transform it to a markdown with embedded Julia and Documenter.jl to create a navigable web site.
These packages have plenty of options and integrate easily with CI like GitHub actions.
RStudio renders mynotebook.nb.html file every time R Markdown document mynotebook.Rmd is saved. This process does not involve running code in chunks, so it is much faster than knitting a notebook into mynotebook.html. However, for large .Rmd documents, saving nb.html files can take a long time, and, unfortunately, it is required to wait for it to finish before one can start using the notebook again and run code in chunks.
Is there a way to configure RStudio to not to create nb.html files on save of an R Markdown document?
I've found out you can delete relevant output entry from the top section of your file. In my case it looks like:
---
title: "Document"
output:
html_notebook: default
---
Which causes creation of .nb.html on every save. If you remove the output tag, file is no longer created automatically. You can still knit to any output file from the Knit menu on the top (or press Ctrl+Shift+K for default. This will run all the chunks again, which may take a while.
You may want to consult this guide book for more information on how YAML tags work. I'm just beginning with them!
Another reason (and solution) might be, that you clicked incidentally on the "Knit on Save" button located just under the Save-to-Disk-Symbol in RStudio. This was in my case the problem. Usually you should be able to save a rmd-file without triggering the knitting process.
So - given this scenario - just uncheck the "Knit on Save" button.
I have a report made of several Rmd files (several layers of child documents).
I always end up doing a copy paste and forget to change the chunk names, it's annoying because the report takes minutes to knit before the error pops up.
I use the chunk names to debug, and in the end they're often the ones that make my code crash, there has to be a better way.
How can I check programmatically that everything's clear before attempting to knit ?
Apparently there exists this option that allows you to keep duplicate chunk labels and still knit:
options(knitr.duplicate.label = 'allow')
I am using .. [2] / [2]_ style footnotes in restructuredText. I would like them to be displayed on the end of the page where they occur rather than at the end of the document. How can I go about this? I need a solution that works with rst2latex.
/please refrain from classifying this as of topic. restructuredText is part of the PYTHON programming language.
The LaTeX writer in docutils uses \footnotetext to specify the page at the bottom of which the footnote text should appear. If you want the text to appear at the same page as its mark, you can try to put them close together.
If you want to directly use the \footnote command in LaTeX, you will have to create a new LaTeX writer, probably by extending the LaTeXTranslator class in docutils.writers.latex2e and overload the visit_footnote_reference function. There, search for the footnote content in self.document.footnotes, and write a \footnote command to the output. Also overload visit_footnote and do nothing there (raise nodes.SkipNode).
Alternatively, you can redefine some TeX command to generate the text at the correct position, probably in two passes to get the footnotes marks and contents at first.
I have a .Rnw document in which I include childs. The childs produce tables via the 'latex' command of the Hmisc library in R.
When I make changes in the child documents, these changes do not anymore change the pdf document. My first guess was to use the chunk option 'eval=TRUE', but this does not change anything. Then, I saw, that the tables are actually saved to a .tex file with same name as the .Rnw document. I deleted this file and after compilation with knitr I got an error:
Error: Latexmk: Could not find file documentname.tex.
I assume, this is not the way to do it. Now I am out of ideas what to do. I appreciate some help on my problem.
Best
Simon
Allright, when trying to construct a simple example, I actually found out, that neither the packages I included nor the nesting of child documents interfere with the compilation via knitr. The reason was a simple error in the low-level .Rnw document, where a Hmisc latex table had a label, that missed a closing speech mark.
This causes then the output pdf not to change - I assume, that in this case the already constructed .tex file is included instead of letting knitr recompile the .Rnw documents and this hasn't changed since the last compilation?
What I wonder about is the different format of the landscape ctable in the document. Using a simple knitr document just with \documentclass{article} produces well placed tables. In my document using a template for the JFE, I get a table that extends over the whole page and even in footnotesize it is far away from the great appearance in the simple document. There is only a margin of less than half a cm on the right and the left. Page size is the same: both US letter... Can I probably control that via knitr or only via resizebox?