How to specify column widths in RMarkdown to Word? - r

I've been struggling with this for a while and I was hoping someone might be able to help me out. I'm using Rmarkdown and Knitr in RStudio to produce a word document (.docx), which I am populating with data. The only issue is no matter what I do, I can't seem to create consistent column widths. I've tried using a reference .docx, with no luck, and I've tried using pandoc.table with "split.cells", which changes the result printed to R console, but the formatting goes away when Knitr puts it into word:
panderOptions("table.split.cells",c(10,1,10,1,1))
pandoc.table(function_to_create_table(x))
Any suggestions? I feel like there's an obvious answer that's been eluding me.

Related

Fractions and Superscript in a table while knitting to docx (RMarkdown)

I have been using officedown for some time now to knit to docx. I encountered an issue where a table I had to create required superscripts in the table as shown below
This led me to go over all the methods for creating a table. Rather than typing it all out here I created an Rmd document below which goes through all the issues. I would be interested in knowing if anyone has any comments or suggestions. My goal is simple. We want a table that exports to docx that
Shows up in the list of tables
Has a caption
In-text reference
Shows the fractions correctly
Please find the detailed Rmd with examples HERE
I apologize for the length of the Rmd document but I wanted to showcase in detail what I have attempted to do to facilitate answers and suggestions
Update: Got a little roasted on the officer GitHub but it seems that fractions are not possible at the moment but subscripts are using ftExtra

How to convert what is printed in the RStudio console into a document?

So, as the question says, I was wondering if it was possible with R, to print in a document the result of your code. In other words, the result of sourcing my script that gets printed into the console of RStudio, I'd like to have it in a document. Is this possible? Any idea as how to do it? Thanks!
Definitely RMarkdown was the solution to me. It is wonderful in order to generate documents based on your R code output, amongst other stuff.

Change table of contents colour R markdown to pdf

My problem is very similar to this question - originally I wanted to just separate the table of contents from the title page with R markdown.
I used the latex workaround proposed there to achieve this, but when i add a table of contents to R markdown and knit it as .pdf the table of contents has a bright pink coloured font. All the help I found so far was using something like
<span style="color:green">WORD_WITH_DIFF_COLOUR</span>
or
<font color='green'>WORD_WITH_DIFF_COLOUR</font>
to change the colour of some words, but it doesnt work when do something like
<font color='green'>'\tableofcontents</font>
I guess there is a way to change this, but I wasn't able to find anything till now...
Any help would be appreciated!
EDIT!
Using #Tad Dallas's tip i was able to get this far
See the LaTeX Options section of the R Markdown documentation
All you have to do is add linkcolor: 'black' to the YAML.
In an attempt to apply #mrp's solution, it seems like the current solution is to add toccolor: 'blue'.
Found it in the Pandoc manual, as suggested in the R Markdown documentation.

Edit text/comments of rmarkdown and knitr reports without rerunning code

I love knitr & rmarkdown, but I often find myself in situations where I have a lengthy report that takes some nontrivial amount of time to run. After it's generated, I notice my inevitable typos in text. However, re-knitting everything to just fix a couple typos (just in text, not code) takes a long time and seems avoidable. I was about to start taking a hack at developing my own solution to this, but I'm thinking it's the kind of thing that could already have a mature solution which would likely be more robust than the one I'd build.
I'm wondering if there is solution out there within knitr or third party that would allow me to edit just the text of my reports without rerunning code, generating plots and outputs etc. I know, I can simply edit the generated html text, but then those changes must be replicated in the R/Rmd code that generated it, or they get out of sync. I'm envisioning a function like this:
argument 1: the R/Rmd script with text edits (no code changes)... perhaps a warning is generated when code chunks change
argument 2: the html output file from the last time the R script in argument was knitted without the text edits.
return: the html report (argument 2) updated with the comments in the R/Rmd script (argument 1).
I use the cache option sometimes for large datasets. I toggle eval and echo on and off when developing if I'm just working on the text of my report. However, I'm looking for a function that would take care of all this for me, so one doesn't have to mess with the code and chunk options to make small edits to text.
Here's an interim solution that lets you retain the speed of making changes directly to the rendered text, but you have to do a little work after you're done making changes.
Assuming the following files:
input.knitr is the original Knitr file with text and code integrated.
output.html is the resulting HTML code that has been rendered by Knitr.
Consider making direct text edits to output.html and then running something like Meld visual merge tool:
meld output.html input.knitr
Then manually select the edits in output.html that are new and should be fixed in the original source input.knitr. Tools such as Meld do a pretty good job of aligning the texts so that the chunks and knitted output will appear as large "changes" that, in practice, you would ignore. You would focus on the small changes in the non-chunk sections.

How to format linked R code in Latex so that it can be copied and run directly from the pdf?

I have to submit a programming assignment in pdf format (produced using LaTeX), and the tutor expects to be able to copy and paste the code directly from the pdf into R to run it. I know I can do this by hard-copying the code into the LaTeX document in a \verbatim block, but I usually use the 'listings' package to link my R source file directly to my LaTeX document, and when I do that, the pdf output contains a lot of extra spaces that are picked up when the code is copied back into R. Sometimes the code will still run with the spaces, but with decimal points, underscores etc the inserted space will cause problems. I've copied the same line from the 'verbatim' environment (top) and 'listings' (bottom) to illustrate the difference:
par(mfrow = c(2,1), ps = 10, mar = c(3,3,2,2))
par ( mfrow = c(2 ,1) , ps = 10, mar = c(3 ,3 ,2 ,2))
I've been through the Source Codes documentation and tried removing whitespace and changing the basic style (my default is ttfamily), but this doesn't work, and Googling just brings me variations on the official documentation. Essentially, what I'd like to be able to do is apply the Verbatim font style to my Listings environment so that I can still format my code how I want to - but I suspect it won't be that easy. Any suggestions on how to get my R code into a document without copy-pasting each line, so that the output can be copied back into R, would be greatly appreciated! Thanks in advance...
An easy solution has been mentioned here:
https://tex.stackexchange.com/questions/119218/how-to-copy-paste-from-lstlistings
Add
\lstset{columns=fullflexible}
and you will be able to copy/paste the R code from the pdf document.
+1 to #Roland, he has the right idea with knitr.
I would however also assume with enough configuration of listings in LaTeX you would be able to get rid of the unwanted whitespace. It's been a while since I fiddled with the listings, but I recall them having a lot of customisability, as well as syntax support for R, that should clear most of the conversion issues, but I may be mistaken.

Resources