Default custom options - r

I would like to create a default chunk option for my documents, so that I can choose which chunks are included in the appendix. Here is my MWE:
```{r setup, include=FALSE, appendix=FALSE}
knitr::opts_chunk$set(echo=FALSE)
knitr::opts_chunk$set(appendix=TRUE)
```
```{r}
# some code included in the appendix
```
```{r appendix=FALSE}
# some code not included in the appendix
```
# Appendix
```{r, ref.label=knitr::all_labels(appendix), echo = T, eval = F}
```
I would like to include all the chunks that do not explicitly say appendix=TRUE. If I add that, it works as expected, but the default knitr::opts_chunk$set(appendix=TRUE) does not seem to work.
I am probably missing something in my setup. Any help is greatly appreciated.

You need to explicitly set:
Appendix <- TRUE in setOptions
you then set -
knitr::opts_chunk$set(appendix = TRUE)
now any chunk without Appendix = FALSE is included in the Appendix.
Note: It is important to include Appendix = FALSE in the Appendix chunk.
I hope the above helps, it is how I do this.
---
title: "47085866-2"
author: "Technophobe1"
date: "11/5/2017"
output:
html_document:
keep_md: yes
---
```{r setOptions, echo = FALSE, Appendix = FALSE}
Appendix <- TRUE
knitr::opts_chunk$set(echo = FALSE, message = FALSE, error = FALSE, warning = FALSE, results = 'hide')
knitr::opts_chunk$set(appendix = TRUE)
```
## Appendix = TRUE
```{r code}
# some code included in the appendix
setClass(
"CStruct",
slots = list(
powerLevel = "numeric",
size = "numeric"
)
)
CStructure <- new("CStruct", powerLevel = 5, size = 10)
CStructure
str(CStructure)
CStructure#powerLevel
CStructure#size
```
## Appendix = FALSE
```{r ref.label='code', Appendix = FALSE}
# some code not included in the appendix
setClass(
"CStruct2",
slots = list(
n = "numeric",
s = "character",
b = "logical"
)
)
CStructure2 <- new("CStruct2", n = c(2, 3, 5),
s = c("aa", "bb"),
b = c(TRUE, FALSE, TRUE) )
str(CStructure2)
CStructure2#n
CStructure2#s
CStructure2#b
```
# Code Appendix
```{r, ref.label=knitr::all_labels(Appendix), Appendix = FALSE, echo=TRUE, eval=FALSE}
## Code Appendix
```
Output:

Related

RMarkdown Change numbering for tables in PDF document

I want to change the numbering of the tables in the RMarkdown document so that all tables in the appendix have an "A-" in front of the number, thus: "Table A-2".
Only in the appendix. Otherwise with normal numbering ("Table 1").
However, I am not really getting anywhere.
Here is my reproducible example:\
---
title: "This is my title"
date: "`r Sys.setlocale(locale = 'English') ; format(Sys.time(), '%B %d, %Y')`"
output: pdf_document
---
```{r echo = F, message = F, warning = F}
library(tidyverse)
library(knitr)
``` #The hash mark must be removed!
# Results
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 2,
"value2", 5
)%>%
kable(booktabs=T, caption = "This is the caption of the first table")
```
# Appendix
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 6,
"value2", 8
)%>%
kable(booktabs=T, caption = "This is the caption of the second table")
```
This is really a LaTeX question, and I found the answer here.
You add these LaTeX lines after your Appendix title:
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}

htmlTable producing row numbers

I'm putting a table into an RMarkdown presentation and it is producing erroneous row numbers in the output I've got this:
library(htmlTable)
d <- data.frame(
x=c('1','2','3'),
y=c('A','B','C')
)
htmlTable(d)
How do I remove these?
Set rnames equals to FALSE.
---
title: "htmlTable"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(htmlTable)
d <- data.frame(
x=c('1','2','3'),
y=c('A','B','C')
)
htmlTable(d, rnames = FALSE)
```
library("xtable")
d <- data.frame(
x=c('1','2','3'),
y=c('A','B','C')
)
print(xtable(d), type="html", include.rownames = FALSE)

Table rendering in pdf document using R markdown

After days trying to find a solution, I give up and ask for help.
I decided to use R Markdown very recently. While I can render plots as I want, I cannot succeed in rendering my tables in a pdf doc properly.
Here the corresponding [EDITED]code:
---
title: "My_title"
output:
pdf_document: default
html_document:
df_print: paged
params:
date: "!r Sys.Date()"
---
```{r library, echo=F, message=F, warning=F, paged.print=FALSE}
suppressMessages(library("knitr"))
suppressMessages(library(reshape2))
suppressMessages(library(splines))
suppressMessages(library(kableExtra))
suppressMessages(library(gplots))
```
```{r, setup, echo = F}
opts_knit$set(root.dir = "my_path")
knitr::opts_chunk$set(echo = F)
```
```{r}
dt <- expand.grid(Region=c("a","b","c"), Country=c("d","e","f"), Cancer= c("All", "CRC", "Breast"),
age.1.1=1:2,
age.1.2=1:2,
age.1.3=1:2)
```
```{r Table_1, INCLUDE = TRUE}
cancer.lab <- c("All", "CRC", "Breast")
for (i in 1:3){
b <- dt[dt$Cancer==cancer.lab[i],]
b <- b[,-3]
t <- kable(b, format = ,caption = "Fig", row.names = F) %>%
kable_paper() %>%
kable_styling(font_size = 9) %>%
add_header_above(c(" " = 2, "1998" = 3))
print(t)
}
```
Again I am new and I surely miss something.
I use Mac if it may explain something.
Thank you for your help.
Sophie.
I think this is the same issue as dealt with here: https://stackoverflow.com/a/53632154/2554330. The problem is that you need to use knit_print to print the tables, but you can't do that in a loop.
So if you change the last code chunk to this, it should work:
```{r Table_1, INCLUDE = TRUE}
results <- c()
cancer.lab <- c("All", "CRC", "Breast")
for (i in 1:3){
b <- dt[dt$Cancer==cancer.lab[i],]
b <- b[,-3]
t <- kable(b, format = ,caption = "Fig", row.names = F) %>%
kable_paper() %>%
kable_styling(font_size = 9) %>%
add_header_above(c(" " = 2, "1998" = 3))
results <- c(results, knit_print(t))
}
asis_output(results)
```

Programmatically create a question_text with multiple answers in r learnr:tutorial

I have the following code with 4 correct answers. I want the students to input all 4 of them. Instead of defining 24 permutations of the answers, I want 4 field boxes that would only accept an answer once.
question_text(
"Input all paths:",
answer("ABEF", correct = TRUE),
answer("ABCDG", correct = TRUE),
answer("ABCDEF",correct = TRUE),
answer("ABDEF", correct = TRUE),
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
EDIT
I tried this approach but I do not think I can set the answer as anything other than a single text:
library(gtools)
pat <- permutations(4, 4, c("ABEF","ABCDG","ABCDEF","ABDEF"))
question_text(
"Input all possible rupture paths:",
answer(pat, correct = TRUE),
allow_retry = TRUE,
trim = TRUE
)
Even if I set pat <- c("ABEF","ABCDG","ABCDEF","ABDEF") it does not run successfully. How can define multiple answers at the same time without writing them out.
I'm not sure about your desired output - however, please check the following.
Referring to:
How can define multiple answers at the same time without writing them
out.
You can use lapply to create the answers and do.call to pass the different arguments to question_text:
library(learnr)
do.call(question_text, c(
list("Input all paths:"),
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
list(
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
))
as *.Rmd file:
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
```
```{r two-plus-two, exercise=FALSE}
do.call(question_text, c(
list("Input all paths:"),
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
list(
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
))
```
Regarding:
I want 4 field boxes that would only accept an answer once
Edit: Added an event handler to access to the answers provided by the user.
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
questions <-
mapply(
FUN = question_text,
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
text = paste("Question", 1:4),
incorrect = paste("Incorrect", 1:4),
MoreArgs = list(allow_retry = TRUE,
trim = TRUE),
SIMPLIFY = FALSE
)
```
```{r q1, echo = FALSE}
do.call(quiz, c(list(caption = "Quiz 1"), questions))
```
```{r context="server-start"}
event_register_handler("question_submission", function(session, event, data) {
# names(data):
# "label" "question" "answer" "correct"
message("event: question_submission: ", data$answer)
})
```

how to make a multiple line table with xtable, longtable

I am trying to to use xtable() and the longtable environment to display a multi=page table.
Currently when you run the code it will show "\scalebox " at the top and then it will cutoff columns of the table.
How do you remove the "\scalebox" at the top and show all the column in a readable manner?
Thank you.
here is the code:
---
title: "test"
output: pdf_document
keep_tex: TRUE
setspace: singlespacing
geometry: margin=1.1cm
header-includes:
- \usepackage{color}
- \usepackage{amsmath}
- \usepackage{xcolor}
- \usepackage{colortbl}
- \usepackage{tabulary}
- \usepackage{tabularx}
- \usepackage{longtable}
- \usepackage{ltxtable}
fig.lp: ('';character)
fig.pos: "H"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE , comment = NA, message= FALSE, warning = FALSE)
```
```{r one, include= FALSE}
n=600
table = data.frame(name = rep(" dakjf aslkj dklsfj aslj ldjs",n),
NUMERICFIELDNUMBERa = rep(10000,n),
NUMERICFIELDNUMBERb = rep(10000,n),
NUMERICFIELDNUMBERc = rep(10000,n),
NUMERICFIELDNUMBERd = rep("1250 + 52.33",n),
NUMERICFIELDNUMBERe = rep("25.6 + 36.25",n),
NUMERICFIELDNUMBERf = rep(10000,n),
NUMERICFIELDNUMBERg = rep(10000,n)
)
head(table)
colnames(table) = c("GROUP NAME",
"Numeric Field Number1",
"Numeric Field Number2",
"Numeric Field Number3",
"Numeric Field Number4",
"Numeric Field Number5",
"Numeric Field Number6",
"Numeric Field Number7"
)
rownames(table) = NULL
```
```{r echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, results='asis'}
library(xtable)
options(xtable.include.rownames=F)
options(xtable.scalebox=.7)
#options(xtable.tabular.environment = "tabulary")
#options(xtable.width = "7in")
options(xtable.comment=F)
options(xtable.floating = F)
t = xtable(table)
#align(t)= "cccccccc"#"cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}cp{2cm}" #rep("p{2cm}C",8)#"p{1cm}cccccccc"
#align(t) = "cp{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}"
print(t, sanitize.text.function = identity)
```

Resources