I am using knitr to generate a PDF writeup. I want to print a series of tables with section headers in between. I am doing this in an R code chunk. Unfortunately though, what happens is that the first header prints, then a figure, then the rest of the headers fit on that page and the rest of the tables come after rather than being interspersed amongst the headers as desired.
After this page there are a series of 5 more tables on their own pages.
This is the code that I am using:
dfList <- list(alc_top, alc_bottom, cpg_home_top, cpg_home_bottom, electronics_top, electronics_bottom)
labels <- c("Premium Liquor Brand - Top Performers", "Premium Liquor Brand- Bottom Performers", "CPG Home - Top Performers", "CPG Home - Bottom Performers", "Electronics - Top Performers", "CPG Home - Bottom Performers")
for (i in 1:length(dfList)) {
df <- dfList[[i]]
product = "test"
cat(paste("\\section{",labels[i],"}", sep=""))
print(xtable(df,size="\\tiny"))
}
I've tried adding a new line, cat("\\newpage") within the loop. this adds a new page for each label, but all of the graphs are after the new section again.
I think I need to specify a positioning value (H or h or something like that in LaTex) for the table, but I am not really sure how to do that with xtable and knitr.
The problem here is not the order in which the elements are written to the TEX file. The "wrong order" in the PDF is due to the fact that the tables are wrapped in floating environments and therefore the position of their TEX code in the source file does not necessarily correspond to the table's position in the PDF.
Here are three options that keep the tables at a fixed position. Each one has its pros and cons:
Option 1: Don't use floats
print.xtable has a floating argument (which defaults to TRUE). Setting this argument to FALSE results in a table that is not wrapped in a floating environment (default: table).
Pro: Simple and effective.
Con: Non-floats are not numbered, have no caption and no label. print.xtable ignores the caption and label arguments on xtable if floating = FALSE.
Option 2: Position "H"
print.xtable has a table.placement argument that can be used to pass a custom float placement specifier to the floating environment. The specifier H "places the float at precisely the location in the LaTeX code" (source: Wikibooks). Note that this requires \usepackage{float}.
Pro: Keep caption, numbering and label.
Con: Requires an additional package (hardly relevant).
Option 3: \FloatBarrier
The LaTeX package placeins offers a \FloatBarrier command which forces that all floats that are not displayed up to this point are printed.
Pros and cons: As option 2.
Besides, it clutters the code a little bit because of the \FloatBarrier commands that need to be inserted after each table – unless (at least in the specific case of this question) the following feature is used:
The package even provides an option to change the definition of \section to automatically include a \FloatBarrier. This can be set by loading the package with the option [section] (\usepackage[section]{placeins}). [source: Wikibooks]
Demo
\documentclass{article}
\usepackage{float}
\usepackage{placeins}
\begin{document}
<<results = "asis", echo = FALSE>>=
library(xtable)
# This table floats.
print(
xtable(head(cars),
caption = "Floating",
label = "tab:floating"), table.placement = "b"
)
# This table won't float but caption and label are ignored.
print(
xtable(head(cars),
caption = "Not floating",
label = "tab:not-floating"),
floating = FALSE)
# Placement "H". (requires "float" package)
print(
xtable(head(cars),
caption = "Non-floating float",
label = "tab:not-actually-floating"),
table.placement = "H")
cat("Text before the barrier. (text 1)")
# Floats won't float beyond this barrier (requires "placeins" package)
cat("\\FloatBarrier\n")
cat("Text after the barrier. (text 2)")
#
Add \texttt{table.placement = "b"} to the first table to see that it will be located at the bottom of page 1 (after `text 1') and `text 2` will come \emph{after} it (on page 2), althogh there would be plenty of space on page 1. This is because the float cannot `pass' the barrier.
\end{document}
Related
I have a Shiny app that writes a webpage and provides both an Excel file download and a PDF, through an .Rmd because I need to parameterize by country. See my previous question for the same project on how to remove empty space in the PDF: https://stackoverflow.com/questions/64210913/how-do-i-remove-empty-space-between-code-chunks-in-rmd-output-to-pdf
The PDF output is in columns: left side for tables, blank mini middle column for white space, right side for charts. I add table titles as text (I did not add them as captions because I wanted the titles formatted a certain way and couldn't figure it out that way as I don't know LaTeX). Some of the titles are long, and wrap mid-word with a hyphen. How do I make the PDF output wrap without breaking up a word?
For example, I'd like a long title to break between "with" and "extralong", without specifically telling it to break there (because my titles are dynamic, each PDF might need to wrap at a different spot(s) in the title), and then again probably between "through" and "hyphenation". Here's the R chunk where I create a long title and the colorize function:
long_title <- paste0("This really long title continues forever with extralong words that shouldn't break through hyphenation")
## https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html
colorize <- function(x, color) {
if (knitr::is_latex_output()) {
sprintf("\\textcolor[HTML]{2A64AB}{%s}", x)
} else if (knitr::is_html_output()) {
sprintf("<span style = 'color: %s;'>%s</span>", color, x)
} else x
}
The call:
**r colorize(long_title, color = "#2A64AB")**
The output, NOT the way I want it:
When using the ReporteRs package, it seems that the only way to get text into the footer of a page involves placing a numbered footnote in the body of the text, and having that footnote appear with the same number in the footer. I'd like to be able to put text in the footer of a page without any numbering in front.
library(ReporteRs)
doc1 <- docx()
doc1 <- addFlexTable(doc1,vanilla.table(head(iris)))
Foot <- Footnote()
Foot <- addParagraph(Foot,"This should not have a number in front of it")
doc <- addParagraph(doc,pot("There should be no number after this",footnote=Foot))
writeDoc(doc1, file = "footnote1.docx")
Alternatively, if it's possible to just put a paragraph at the bottom of the page, that would also solve my problem. This could be done by figuring out how many more lines will fit on the page, but if there was some way to make the vertical alignment the bottom of the page for the last paragraph, that would be ideal.
doc2 <- docx()
doc2 <- addFlexTable(doc2,vanilla.table(head(iris)))
doc2 <- addParagraph(doc2,c(rep("",33),"Text placed by dynamically finding bottom of the page"))
writeDoc(doc2, file = "footnote2.docx")
What you are trying to do doesn't match ReporteRs::Footnote as it was written, as shown in the help:
If in a docx object, footnote will be flagged by a number immediately following the portion of the text the note is in reference to.
However, what you are after is achievable if I understand your question correctly. The note in your table and the text in the footer will not be connected in any way, such as a hyperlink provided by Footnote.
There is also the issue that ReporteRs does not provide a method to place text in the footer without using bookmarks (except Footnote, which we have now discounted). That means we need to use a docx template instead of the empty document generated by the package.
Template creation
Steps:
From MS Word I have opened an empty document
Placed the cursor in the footer area
Insert => Bookmark
Enter Bookmark name, I just used FOOTER, and click Add
Save the document
Document generation with ReporteRs
With our new template, the next steps will seem more familiar.
library(ReporteRs)
doc <- docx(template = "Doc1.docx")
# do the flextable, note that I add your table footer here
ftable <- vanilla.table(head(iris))
ftable <- addFooterRow(
ftable,
value = c("There should be no number after this"),
colspan = 5
)
doc <- addFlexTable(doc, ftable)
# check for the presence of our bookmark
list_bookmarks(doc)
# [1] "FOOTER"
# now add the footer text using the bookmark
doc <- addParagraph(
doc, stylename = "footer", bookmark = "FOOTER",
pot("This should not have a number in front of it")
)
# and finally write the document
writeDoc(doc, file = "doc.docx")
End product
The table, which you can better format to suit, I have not removed the border on the added row.
The footer, in the standard footer style, which again you can modify to suit.
I have a long .Rnw document which consists mostly of text (typeset in LaTeX) with a few chunks here and there. I have also written a chunk which outputs a specific figure. The figure contains a plot, the values for the plot are currently read from a .csv file and some parameters like colors defined manually within the chunk.
Now I want to have the same figure in a different place in the document, but with different values for the plot and a few other parameters different. Ideally, I would like to include the chunk as a child twice, and pass parameters to it somehow, including the name of the .csv to be used for the plot values. I would hate to copy paste the chunk code with hardcoded parameters, as it is complex enough that potential changes will be difficult to synchronize.
How can I do such "parameterized reuse" of chunks?
update
As requested, a small example
This is saved as include-chunk-reuse.Rnw
<<toReuse, echo=FALSE, result='asis'>>=
l <- 25
#
\newlength{\mylength}
\setlength{\mylength}{\Sexpr{l}pt}
%Omitted: a lot of complicated LaTeX commands
\rule{\mylength}{1pt}
This is the document which is supposed to reuse the chunk. It doesn't even compile, as it complains that the same label is used twice: Error in parse_block(g[-1], g[1], params.src) : duplicate label 'toReuse'
\documentclass{article}
\begin{document}
This is some text. And now comes a 25 pt wide line.
<<first-figure, child='include-chunk-reuse.Rnw'>>=
#
This is some text. The next line is also 25 pt wide. But I would like to call the chunk in a way which makes it 50 pt wide instead.
<<second-figure, child='include-chunk-reuse.Rnw'>>=
#
\end{document}
For the knitr part to work simply leave out the chunk-name in the child document, then you don't have the duplicated label and the knitr part works.
Passing Parameters does not really work as far as I know, but you can just set a global variable before including the child. (For example \Sexpr{l <- 200}
You are still redefining \mylength which is why LaTeX will throw an error, so move the first definition of \mylength from the child to the main document.
The example below demonstrates two ways to reuse and parametrize a chunk.
Reusing Chunks
The mechanism is explained here. Basically, the simplest way to reuse a chunk is to add another empty chunk with the same label. Alternatively, the chunk option ref.label lets a chunk inherit another chunks code.
Both approaches of reusing chunks are largely equivalent – with one exception: figures generated in chunks are saved as chunklabel-i.pdf, where i is the figure index counted by chunk. Therefore, if a chunk is reused by repeating its label, figure i from the second use will overwrite figure i from the first use. This is the reason why I use ref.label (and thus distinct chunk labels) in the example below (otherwise, the points on both plots would be green).
In the example below, I used eval = FALSE in order to prevent evaluation of the masterchunk where it is defined. An alternative would be to externalize the chunk and read it by read_chunk().
Parameterizing Chunks
The two most straightforward options to "pass" parameters to a chunk are
chunk options and
global variables
Also when reusing chunks, each use can set different chunk options. The example below exploits this to set different captions.
As all chunks run in the same environment, setting a variable in an early chunk affects subsequent chunks accessing this variable. In the example below, mycolor is modified this way.
\documentclass{article}
\begin{document}
<<masterchunk, eval = FALSE>>=
plot(1:10, col = mycolor)
#
<<config1>>=
mycolor <- "red"
#
<<use1, ref.label = "masterchunk", fig.cap = "Red dots">>=
#
<<config2>>=
mycolor <- "green"
#
<<use2, ref.label = "masterchunk", fig.cap = "Green dots">>=
#
\end{document}
I need to produce a report that is composed of several sections, all sections look similar, with only some differences in data. The number of sections is also dependent on the data. What I ultimately want to have is something like this:
```{r}
section_names = c("A","B","C")
section_data = c(13,14,16)
```
# some looping mechanism here with variable i
This is section `r section_names[i]`
This section's data is `r section_data[i]`
#more things go here for the section
#end of loop should go here
The result should be a single html/document with all the sections one after the other.
Can you point me to a way for producing such an Rmd file with the loop?
Ideally I would have hoped to see something like in PHP:
<$php for(i=0;i<10;i++) { ?>
## some html template + code chunks here
<$php } ?>
This question is similar to that one, although it is LateX/RNW based. Besides, this answer demonstrates how to generate a rmarkdown document dynamically. However, neither of the questions is a exact duplicate of this one.
Basically, there are two mental steps to take:
Figure out the markdown markup needed per section. This could be something along the lines of
## This is section <section_name>
Section data is `<section_data>`.
Additional section text is: <section_text>.
Write R code that generates this markup, replacing the placeholders with the appropriate values.
For step 2, using sprintf is a natural candidate to combine static and dynamic text. Don't forget to use the chunk options results = "asis" to prevent knitr from adding formatting to your output and use cat (instead of print) to prevent R from adding additional stuff like quotes and element numbers.
I changed the input data structure a little bit for the sake of clarity (using a data.frame instead of independent vectors section_names and section_data).
```{r echo = FALSE, results = "asis"}
input <- data.frame(
name = LETTERS[1:4],
data = runif(n = 4),
text = replicate(4, paste(sample(x = LETTERS, size = 100, replace = TRUE), collapse = "")),
stringsAsFactors = FALSE)
template <- "## This is section %s
Section data is `%0.2f`.
Additional section text is: %s.
" # dont't forget the newline
for (i in seq(nrow(input))) {
current <- input[i, ]
cat(sprintf(template, current$name, current$data, current$text))
}
```
Output:
This is section A
Section data is 0.83.
Additional section text is: PUFTZQFCYJFNENMAAUDPTWIKLBSVKWMJWODFHSPRJRROTVDGNEROBVQPLLMVNPOUUHGVGRPMKAOAOMVYXKMGMUHNYWZGPRAWPYLU.
This is section B
Section data is 0.49.
Additional section text is: PFTYCGFSGSMAYSSCZXWLNLDOQEBJYEVSJIYDJPEPSWQBNWJVRUKBTYIUSTOICFKJFEJCWCAYBCQSRTXUDEQLLXCZNPUKNLJIQJXE.
This is section C
Section data is 0.58.
Additional section text is: FCJDDDMNLBUSJMCZVSBPYWCKSFJEARBXXFPAGBTKCWKHPEDGYWYTNGLVGQGJAFZRUMNSDCHKTTMGRFNSUZKFLOUGNWHUBNLVMGDB.
This is section D
Section data is 0.52.
Additional section text is: YQIXHABFVQUAAYZNWTZXJDISSLTZJJAZOLJMJSXEENFTUOFOTYKDNNUMFDXLJSWZEVDLCLSYCTSMEXFLBVQYRTBEVZLCTEBPUGTT.
Just sharing the approach I've used eventually.
I wrote a markdown file for the section. prepared the data for each section in the master document, and looped over all the sections I needed, each time calling to knit_child() with the section Rmd.
I know this is late, but I used this in my code to make numbered sections and it works a treat.
for (k in 1:length(listcsv)){ #Begin Loop at pdf file one and continue until all have been completed
subsection <- paste("5", k, sep = ".")}
this uses the loop number (k) to create the subsection number and then paste it against the section number. This happens to be in section 5, but you could use the same principle to make sections and subsections ad infinitum.
I need to insert the species names in a table created by xtable in my Rnw file and I want to convert the relative column to italics format. Is it possible without any manual intervention?
My call is:
xtable(cklist, caption="Checklist...", align='lllc',label = 'tab:ckzygo')
To typeset a column in italics (or any other non-standard font shape), you should use the >{\cmd} syntax for column specification.
Assigning the column type >{\itshape}l generates a left-justified column in italics.
This is a better solution than iris$Species <- paste0("\\textit{", iris$Species, "}") as suggested in the comments because you neither have to modify your data nor you need to disable text sanitizing.
Small illustration:
\documentclass{article}
\usepackage{array}
\begin{document}
<<xtableItalics, results = "asis">>=
library(xtable)
print(xtable(head(iris), align = c(rep("l", 5), ">{\\itshape}l")))
#
\end{document}
The PDF looks like:
Please note that you need to use the array package for this to work.
EDIT: To show the flexibility of this approach, two more examples:
print(xtable(head(iris), align = c(rep("l", 5), ">{\\textit\\bgroup}l<{\\egroup}")))
print(xtable(head(iris), align = c(rep("l", 5), ">{\\textcolor{red}\\bgroup}l<{\\egroup}")))
The first line uses \textit{} instead of \itshape to typeset the italics. As \textit{} requires the text to modify as an argument, we need a slightly more complex syntax. (It's described in the wikibooks.org article linked above.)
This syntax can also be used to change for example the color of the text. In more complex cases, lrbox is required, as described in the linked article.