I'm trying to split a document in multiple .jmd files. Something similar to the answer to this question.
Basically what I'm trying to find out is what is the equivalent to R Markdown's child cells in Weave.jl.
An example of what I'm trying to obtain is a pdf obtained from combining three (or more) .jmd files:
notes.jmd
---
title: ...
author: ...
date: ...
---
```julia, child="chapter01.jmd"
```
```julia, child="chapter02.jmd"
```
chapter01.jmd
# Chapter 1 title
```julia
#= code here =#
```
chapter02.jmd
# Chapter 2 title
```julia
#= code here =#
```
Related
I have a code in R, which is done in rMarkdown, it needs to run several times but with different parameter values.
How can I solve it? I have put an example of simplified problems.
Suppose I need to print different values in a loops, in order to make it simple I used simple loops.
For each combination of these two loops I want to make one html file, for example means 200 *400 different html report files.
---
title: "file1"
author: "user"
date: "25 1 2021"
output: html_document
---
# The first loop should be done, from 1:100, 101:200 ... to ... 20001:20100
```{r}
i=getfirst()
j=getsecond()
```
```{r}
for (i in i:j) print(i)
```
# The second loop should be done, from 1:50, 51:100 ... to ... 20001:20050
```{r}
for (i in i-50:j-50) print(i)
```
suppose each time we may have different i and j which should pass into markdown file.
I'd simply create a wrapper script (i.e. a separate e.g. .R) file, where you specify:
R file
for (i in 1:10){
j = i+50
if(!dir.exists(paste0("directory_",i))){dir.create(paste0("directory_",i))}
knitr::knit(input = "markdown.Rmd",output = paste0("directory_",i,"/output",i,"_",j,".html"))
}
And then your Rmd file, which you have saved as markdown.Rmd in this example looks like this:
RMarkdown file
---
title: "file1"
author: "user"
date: "25 1 2021"
output: html_document
---
# The first loop should be done, from 1:100, 101:200 ... to ... 20001:20100
```{r}
for (i in i:j) print(i)
```
# The second loop should be done, from 1:50, 51:100 ... to ... 20001:20050
```{r}
for (i in i-50:j-50) print(i)
```
There is a dedicated chapter in the official documentation, "Knitting with parameters", that outlines how to proceed in your use-case.
I have been running some small R tutorials / workshops for which I keep my 'challenge scripts' in Rmarkdown documents. These contain free text and R-code blocks. Some of the code blocks are prefilled (eg, to set up datasets for later use), whereas some are there for the attendees to fill-in code during the workshop.
For each challenge script, I have a solution script. The latter contains all the free text of the former, but any challenge-blocks have been filled in (there's an example of a solutions workbook here).
I don't really want to keep two closely related copies of the same file (the challenge and the solutions workbook). So I was wondering if there's an easy way to construct my challenge scripts from my solutions scripts (or the solutions script from a challenge-script and an R-script containing just the solution blocks).
For example, is there an easy way to replace all named code-blocks in one Rmarkdown file with the correspondingly-named code block from another rmarkdown file?
That is, if I have
challenge.Rmd
HEADER
INTRODUCTION
Today we're going to learn about sampling pseudo-random numbers in R
```{r challenge_1}
# Challenge 1: Make a histogram of 100 randomly-sampled
# normally-distributed values
```
BLAH BLAH
END_OF_FILE
solutions.Rmd
HEADER
```{r challenge_1}
# Challenge 1: Make a histogram of 100 randomly-sampled
# normally-distributed values
hist(rnorm(100))
```
END_OF_FILE
How do I replace challenge_1 from challenge.Rmd with challenge_1 from solutions.Rmd?
All the best
This is one approach:
challenge.Rmd
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
show_solution <- FALSE
```
```{r child="solution.Rmd", eval=show_solution}
```
Today we're going to learn about sampling pseudo-random numbers in R
```{r challenge_1}
# Challenge 1: Make a histogram of 100 randomly-sampled
# normally-distributed values
```
```{r challenge_1_s, eval=show_solution, echo=show_solution}
```
```{r challenge_2}
# Challenge 2: Make a histogram of 100 randomly-sampled
# uniform-distributed values
```
```{r challenge_2_s, eval=show_solution, echo=show_solution}
```
solution.Rmd
```{r challenge_1_s, eval=FALSE, echo=FALSE}
# Challenge 1: Make a histogram of 100 randomly-sampled
# normally-distributed values
hist(rnorm(100))
```
```{r challenge_2_s, eval=FALSE, echo=FALSE}
# Challenge 2: Make a histogram of 100 randomly-sampled
# uniform-distributed values
hist(runif(100))
```
With the show_solution parameter you can include or exclude the solution from you rmarkdown. The participants are not able to compile the document for show_solution = TRUE unless they have the solution.Rmd. For show_solution = FALSE there's no problem and it compiles nicely.
Revised title to clarify focus.
We notice an anomaly that R markdown and data.table interact in a surprising way. Same does not happen when knitting LaTeX. Commands which not have a return within the R session do cause a return within the knitted markdown output. I trace the problem back to commands like the following, which do not produce an output in R,
````{r}
poolballs[ , weight2:=2 * weight]
```
but inside Rmarkdown, the output includes the full print of the poolballs DT. Same does not happen if we knit an equivalent chunk in LaTeX.
This produced some funny HTML output because I wrote chunks like this, intending to display only the first 5 lines
```{r}
poolballs[ , weight2:=2 * weight]
head(poolballs)
```
Markdown parses that as the equivalent of two chunks,
> poolballs[ , weight2:=2 * weight]
> poolballs
> head(poolballs)
Here's the markdown file to demonstrate
---
title: "Data Table Guide"
author:
- name: Paul Johnson
affiliation: Center for Research Methods and Data Analysis, University of Kansas
email: pauljohn#ku.edu
date: "`r format(Sys.time(), '%Y %B %d')`"
output:
html_document:
theme: united
highlight: haddock
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE, comment=NA)
options(width = 70)
```
```{r make_pb_dt}
set.seed(234234)
library(data.table)
poolballs <- data.table(
number = 1:15,
weight = rnorm(15, 45.7, 0.8),
diameter = c(3, 2.9, 3.1) #shows recyling
)
poolballs
```
I want the following to show only head in line 2
```{r}
poolballs[ , weight2:=2 * weight]
head(poolballs)
```
Compare the HTML output:
http://pj.freefaculty.org/scraps/mre-dt.html
I'm sorry if this is a known feature of markdown. I've coded around this wrinkle by hiding chunks, but it seems somewhat inconvenient. Today i'm curious enough to ask you about it. I wrote the same chunks in a LaTeX file and the funny DT output problem does not happen. I put link to PDF from LaTeX in http:/pj.freefaculty.org/scraps/mre-dt-3.pdf
In your final chunk, knitr sees that you have two objects that its attempting to print and you're getting the output for both. This isn't a feature, and has been addressed in a previous question.
If you want to only print the head of the first object in that chunk, your code should be head(poolballs[, weight2:=2 * weight])
I am trying to output a large number that is stored in a variable in a RMD file. I would like the number to print something like
Large number: 4123125.2
however when I knit the rmd it always comes out as
Large number: 4.123125210^{6}
I would like to do away with the exponent notation to make it easier to read.
---
output: html_document
---
```{r}
large.number <- 4.1231252*10^6
```
Large number: `r large.number`.
```{r}
large.number <- 4123125.2
```
Large number: `r large.number`.
use this syntax:
sprintf("%f", large.number)
you can then define how many figures you want to be printed:
sprintf("%.2f", large.number)
I have a standard piece of analysis to perform on multiple datasets and want to present them in one report using a template.
The analysis per dataset could look like this:
child.Rmd
## Name of dataset
```{r calculate_stats}
summary(ds)
nrows <- nrow(ds)
```
The number of rows in the dataset is `r nrows`
The full report has this structure:
parent.Rmd
# Report
```{r import_all_datasets}
...import all datasets form csv...
ds.list <- c(ds1, ds2, ds3, ...)
```
for ds in ds.list
run child.Rmd with ds as a parameter
An additional requirement is that I can run the child.Rmd report alone with a specified parameter. The linked answer in comments below uses double curly braces ({{i}}) and knit_expand replaces it with i in the parent environment. This is unsatisfactory as it makes it a faff to call child.Rmd on its own.
Is it possible for the child to be a parametrised report and for the parent to pass the child the list of parameters.
I'm just attempting to do this now by trying:
child.Rmd
---
output: pdf_document
params:
ds: !r cars
name: "cars"
---
`r params$name`
=====
```{r}
summary(params$ds)
nrows <- nrow(params$ds)
```
The number of rows in the dataset is `r nrows`
And passing params to child within parent.Rmd