In an rmarkdown documents I do
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE , comment = NA, message= FALSE, warning = TRUE)
```
```{r include=TRUE, echo=TRUE, eval=TRUE, comment=TRUE, null_prefix = TRUE, message= FALSE}
library(reshape2)
#### t test
library(reshape2)
#head(tips)
t.test(tips$tip, alternative ='two.sided', mu = 2.5) ### reject null of mu = 2.5
```
and the output has "TRUE" listed as a column down next to the ouput of t.test()
How can you remove the "TRUE"?
The problem is your chunk option comment=TRUE. See the chunk options help:
comment: ('##'; character) the prefix to be put before source code
output; default is to comment out the output by ##, which is good for
readers to copy R source code since output is masked in comments (set
comment=NA to disable this feature)
What you are doing is asking every line to begin with TRUE. Since you already set comment = NA in your setup chunk, there is no need to repeat it.
```{r include=TRUE, echo=TRUE, eval=TRUE, null_prefix = TRUE, message= FALSE}
#### t test
library(reshape2)
#head(tips)
t.test(tips$tip, alternative ='two.sided', mu = 2.5) ### reject null of mu = 2.5
```
Related
I am looking to conditionally display a table in a rmarkdown document. Some of the tables that are generated by my code may contain empty cells and I'm looking to exclude those tables and replace them with a message. Something like:
if_else(is.na(table[3,1]) == TRUE,"Data is currently not available",knitr::kable(table))
Is this possible?
we create a TRUE/FALSE variable in a previous R chunk, then depending on a boolean operation, we get a value we can pass to the eval= R chunk options. In this example due to the result, we get a graph, instead of a table. Remember when naming R chunks, each one has to have a unique name.
---
title: "Untitled"
output: html_document
---
```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
if (iris$Sepal.Length[[1]] > 5)
{show_text = TRUE
} else {show_text = FALSE}
```
```{r conditional_block, eval=show_text, echo=FALSE}
head(iris, 5)
```
```{r setup2, echo=FALSE, warning=FALSE, message=FALSE}
if (iris$Sepal.Length[[1]] < 5)
{show_text = TRUE
} else {show_text = FALSE}
```
```{r conditional_block2, eval=show_text, echo=FALSE}
plot(iris$Sepal.Length, iris$Sepal.Width)
```
I have the following rmd script. I've spent a few days trying to get this to work but I am failing miserably. Basically I need help with three things. I am happy to post three separate questions if needed.
The multicolumn options/code are completely ignored. The corporatetable.docx is in landscape and has a typical corporate style. I need to have a full width landscape -> two column landscape -> full width landscape. If I could get the two column landscape setup to work, the remaining style would be inherited by corporatetable.docx. If I could get help with only one - I would need this.
When I run the rmd it generates a word file but none of the corporate styles are in there. It just uses my word's default colors etc. The difference is very clear - no landscape, single column and blue instead of red. How do I correctly pass the officedown::rdocx_document: to reference my word file because it's clearly not picking it up and no warning or error is generated?
If you see in the second chunk I am using flextable to show two pictures (which are passed through params) in the word report and align them with some information. myft works but it prints the (temporary/volatile) path instead of showing the pictures in the report. For reference if I use knitr::include_graphics(c(params$x1,params$x2)) it works fine.
I'm really stuck on these. Any help is welcome.
---
title: "Title"
subtitle:
params:
x1: x1
x1_name: x1_name
x1_email : x1_email
x2: x2
x2_name: x2_name
x2_email : x2_name
output:
officedown::rdocx_document:
reference_docx: corporatetemplate.docx
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(officedown)
library(officer)
library(flextable)
knitr::opts_chunk$set(out.width = '100%', dpi=300)
```
<!---BLOCK_MULTICOL_START--->
This text is on column 1. Please work
```{r somecodechunk, echo=FALSE, out.width="75px", include=TRUE, strip.white=TRUE}
library(flextable)
# this works but prefer to use flextable
# if(all(!is.null(params))) {
# knitr::include_graphics(c(params$x1,params$x2))} else {
# }
myft <- data.frame(
"pic1" = rep("",3),
"details1" = c(params$x1_name,"+X XXX XXX X",params$x1_email),
"pic2" = rep("",3),
"details2" = c(params$x2_name,"+X XXX XXX X",params$x2_email)
)
myft <- flextable(myft)
myft <- merge_at(myft, i = 1:3, j = 1 )
myft <- merge_at(myft, i = 1:3, j = 3 )
myft <- compose(myft,i = 1, j = 1, value = as_paragraph(as_image(params$x1), part = "body"))
myft <- compose(myft,i = 1, j = 3, value = as_paragraph(as_image(params$x2), part = "body"))
autofit(myft)
#Ok this does not work because the pics are not shown
```
`r run_columnbreak()`
This text is on column 2. Please work
This text is on column 2. Please work
`r run_linebreak()`
<!---BLOCK_MULTICOL_STOP{widths: [4,4], space: 0.2, sep: true}--->
\pagebreak
Back to full width with some text
\pagebreak
I am writing an article for a journal that requires Supplementary Material. I was hoping to use a unique label such as SuppMat: instead of the default fig: to send Figures and Tables into this section. If not, I can use the default fig label but I need numbering to restart in the Supplementary Material section of the document.
I am aware of one answer using latex (found here) but I must complete this when exporting to docx. Below is a reproducible example using officedown
---
output:
officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(knitr, officedown, officer)
knitr::opts_chunk$set(echo = FALSE,
eval = TRUE,
fig.cap = TRUE)
```
# Main Text
Please see Supplementary Figure \#ref(fig:appendix-fig1) and Figure \#ref(fig:main-fig1).
```{r fig.id="main-fig1", fig.cap="This should be labelled **Figure 1**"}
barplot(1:5, col=1:5)
```
```{r tab.id="main-tab1", tab.cap="Main Text Table 1"}
head(mtcars)
```
\newpage
# Supplementary Materials {#SuppMat}
```{r fig.id="appendix-fig1", fig.cap="This figure should be labelled **Supplementary Figure 1**"}
barplot(1:5, col=1:5)
```
```{r tab.id="appendix-tab1", tab.cap="Should be labelled **Supplementary Table 1**"}
head(mtcars)
```
After searching multiple forums and hours, I was able to come up with a solution using the officedown package. Hopefully this helps someone else out. For further details check out the run_autonum function.
---
output: officedown::rdocx_document
---
```{r setup, include=FALSE}
pacman::p_load(officedown, officer, knitr)
knitr::opts_chunk$set(echo = FALSE, fig.cap = TRUE)
ft_base <- fp_text(font.family = "Cambria", font.size = 12, bold = TRUE)
ft1 <- update(ft_base, shading.color='#EFEFEF', color = "red")
ft2 <- update(ft_base, color = "#C32900")
srcfile <- file.path( R.home("doc"), "html", "logo.jpg" )
extimg <- external_img(src = srcfile, height = 1.06/5, width = 1.39/5)
```
## References
This is a reference to an supplementary image caption whose number is Supplementary Figure \#ref(fig:faithfuld-plot) and \#ref(fig:supp-bar).
Testing another figure with a barplot in Figure \#ref(fig:plotbar)
```{r fig.id="plotbar", fig.cap = "Main Figure"}
barplot(1:8, col=1:2)
```
## Supplementary Material
```{r fig.id="faithfuld-plot"}
knitr::include_graphics("images/MyImage.png") # Use your own figure here (I needed to test with knitr for my workflow)
block_caption("First Appendix Figure",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'faithfuld-plot',
pre_label = "Supplemental Figure ",
start_at=1))
```
```{r fig.id="supp-bar"}
barplot(1:8, col=1:2)
block_caption("Second Appendix Figure",
style = "Figure",
autonum = run_autonum(seq_id = 'fig',
bkm = 'supp-bar',
pre_label = "Supplemental Figure ",
start_at= NULL))
```
I have a shiny app created with Rmd / Flexdashboard with many charts and tables. It is working well. Recently, I started using shiny_prerendered to improve the UX during startup. Now the page loads up faster but all the sections are empty (this is expected - the html renders but the charts and tables are still waiting for data) until the server code runs.
Is it possible to have some placeholder data during setup that will load the shiny output? Reactive outputs are not recognized in the setup context. Is this what the server-start context is for? I have tried too that without success?
Here is a simple code with a two value boxes
---
title: "ValueBoxTest"
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
theme: united
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, options(scipen = 1, digits = 2))
### load packages
```
# ValueBox Test
===============================================
## Row 1
### Box 1
```{r, context="server"}
output$value1 <- renderValueBox({
### p <- 100 starting value placeholder
### call function to generate "p"
p <- functionToGenerateValue()
valueBox(p, icon = "fa-usd")
})
```
```{r echo = FALSE}
valueBoxOutput("value1")
```
## Row 2
### Box 2
```{r, context="server"}
output$value2 <- renderValueBox({
### q <- 0 starting value placeholder
### call function to generate "q"
q <- functionToGenerateValue()
valueBox(q, icon = "fa-usd")
})
```
```{r echo = FALSE}
valueBoxOutput("value2")
Well, it may be simpler than you might think. For example, why don't you try to change from runtime:shiny to runtime:shiny_prerendered? It may do the trick.
I am trying to publish a table with 1000 separators and I am not having any luck with it. I followed the link here: Set global thousand separator on knitr but am not having much success.
My sample dataset is here: https://goo.gl/G7sZhr
The RMarkdown code is here:
---
title: "Table Example"
author: "Krishnan Viswanathan"
date: "August 4, 2015"
output: html_document
---
Load Data
{r, results='asis', message = FALSE, tidy=TRUE}
load("i75_from_flow.RData")
library(data.table)
{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knitr::kable(i75_from_flow)
However, when I include this chunk of code (knit_hook$set) in the RMarkdown document, i get errors.
```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})
knitr::kable(i75_from_flow)
```
Error:
# object knit_hooks not found.
Any insights on what I am doing wrong and how to fix this is much appreciated.
Thanks,
Krishnan
The easiest is to use the format arguments of the kable() function itself, where you can specify the big number mark like so:
kable(df, format.args = list(big.mark = ","))
So your example would look like:
```{r, results='asis', echo=FALSE,message = FALSE, tidy=TRUE}
i75_from_flow <- i75_from_flow[order(-Tons),]
knitr::kable(i75_from_flow, format.args = list(big.mark = ","))
```
without any need for knitr hooks.
What about using pander with bunch of options to fine-tune your markdown table:
> pander::pander(i75_from_flow, big.mark = ',')
----------------------------
ORIGFIPS TERMFIPS Tons
---------- ---------- ------
12,023 12,117 5,891
12,119 12,105 4,959
12,001 12,057 3,585
12,001 12,113 3,083
12,047 12,047 1,517
----------------------------
The reason that the knit_hooks object is not found is that you either need to load the knitr package or use the knitr:: prefix in order to set the knit_hooks options. For example:
knitr::knit_hooks$set(inline = function(x) {
prettyNum(x, big.mark=",")
})