Use texreg::wordreg within whole markdown knitted Word document - r

I want to use texreg::wordreg within a Markdown file with several other calculations that I want to finally knit to an MS Word file. However, the function demands that I enter a file name and hence export each table separately. Is there a workaround that I could use so I don't need extra files for each table? I already tried "file = NULL" but it doesn't work (Error: "'file' must be a valid file path.")

That's the expected behaviour of wordreg.
Try
texreg::knitreg(your_list_of_models, center = FALSE)
It should automatically adapt your texreg table based on the type of document that you are knitting.

Related

file.remove() with dynamic input in R

I create dynamic LaTex reports in R markdown and want to delete the .tex file after the .pdf has been created as part of the overall function get_report. The name of the .pdf file in dynamic. I tried
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
where DATA$report_filename is the name of the .pdf that is being created. So if i want to remove the file of a report called "hello", the path would be /sang/pect/apps/office/admin/hello.tex
When I call the function mentioned above, I however get an error: 'No such file or directory'
The reason is that R tries to remove the file /sang/pect/apps/office/admin/.tex
It just leaves out the dynamic part
But when i run
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
in the console, all works well.
So what is my mistake?
If paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex") returns "/sang/pect/apps/office/admin/.tex", I would inspect the DATA$report_filename vector by e.g. using print() function.

script to add links to all images in markdown file

I have a workflow where I'm downloading Google Docs as docx, then processing them to markdown for export to other formats in the R environment.
When I convert to markdown with pandoc_convert, I get embedded images, but they're not linked.
I want to add link syntax similar to this post,
i.e. I want this (not linked):
![m'lady](https://i.imgur.com/v8IVDka.jpg)
to be (linked):
[![m'lady](https://i.imgur.com/v8IVDka.jpg)](https://i.imgur.com/v8IVDka.jpg)
for every image in a document. How to do?
After much toil, I put this function together, which solves my issue:
addLinksToPhotos<-function(mdfile){
d<-readLines(mdfile)
s<-sapply(d,function(x) {
replacementPattern="[\\1(\\2)](\\2)\\3"
gsub('.*?(?<firstpart>!\\[[^\\]]*?\\])\\((?<filename>.*)\\)(?<potentialHTML>\\{.*?\\})?.*?',x=x,replacement=replacementPattern,perl=T)
},USE.NAMES = F)
writeLines(s,mdfile)
}
#usage
addLinksToPhotos("myRMarkdownDoc.rmd")
It will read every line in the markdown file, looking for the photo ![]() pattern, including where it has style coding in braces ![](){}. It will modify each instance to have a linked image [![]()](){}.

Render R markdown from a function

I need to automate some report generation. I would like to create an rmarkdown report from a function, something like
make_report <- function(file_path = "data_file.txt", outfile){
# get data from external file
object <- parse_text(file_path)
# ?pass it into report template?
rmarkdown::render("report_template.Rmd", outfile)
}
Where report_template.Rmd prints tables and figures based on the information in the data_frame object. I've seen ways to make rmarkdown templates, but I don't know if there is a way to pass anything into them.
You cannot directly pass objects to render function, but you still can save objects into RDS/csv and load them in your rmarkdown file at the beginning in the hidden section, so it will not be printed to your output document.

Converting Tex file generated by Knitr into .doc

Why is it important? My collaborators would like to be able to modify my .pdf reports and they are not able to modify the actual .tex code. They are sooo used to word !
How I write my reports? I use knitr. What a great and useful tool; thanks Yihui :)
What I'm looking for? convert the generated pdf report into .doc (my collaborators favorite !)
a solution might help: Pandoc (http://johnmacfarlane.net/pandoc/). As it's explained in the introduction of page of Pandoc, it can be used to convert LaTex to .docx and to markup. I then thought converting the .tex file of each of my reports (a .tex file is generated when I run my knitr .Rnw file) and convert it to .docx using pandoc:
pandoc -s myTexRepot.tex -o aDocReport.docx
issue: 1) Figures are missing in my .docx file. 2) I generate all my tables in my reports using xtable(), none of them can be converted! 3) Also, I lose all table of contents, section numbering.
Markup to Word: Issue (1) above is still an issue in converting Markup code to .doc
Is there any better idea to approach the issue of converting latex to doc. If Pandoc is the best, would you know how I can solve the issues above?
I really appreciate your help.
since you are already using knitr, you can call the rmarkdown:render() function to knit the document.
there is a parameter in render() called output_format[1]. the parameter along with the YAML header in your knitr document will allow you do generate the pdf and word document simultaneously
[1] output_format
R Markdown output format to convert to. Pass "all" to render all formats defined within the file. Pass the name of a format (e.g. "html_document") to render a single format or pass a vector of format names to render multiple formats. Alternatively you can pass an output format object; e.g. html_document(). If NULL is passed then the output format is the first one defined in the YAML metadata of the input file (defaulting to HTML if none is specified).

knitr: Document does not change anymore

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?

Resources