gpt4 book ai didi

r - 如何从代码块内部呈现 Markdown ?

转载 作者:行者123 更新时间:2023-12-04 01:16:26 25 4
gpt4 key购买 nike

使用 knitr 在 R markdown 中实现此伪代码的正确方法是什么?假设这些是文件 test.rmd 的内容

---
title: "Test"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown Loop example

```{r cars}
for(i in 1:nrow(cars)){
### Car {i}
Car {i} has a speed of {cars$speed[i]} miles per hour and a stopping distance of {cars$dist[i]} feet.
}
```

显然这不会运行,因为我的代码块不是有效的 R。但是这个 for 循环的目标是产生类似于以下 markdown 的结果,它像正常一样被编译为 pdf 或 html:

## R Markdown Loop example

### Car 1
Car 1 has a speed of 4 miles per hour and a stopping distance of 2 feet.
### Car 2
Car 2 has a speed of 4 miles per hour and a stopping distance of 10 feet.
### Car 3
Car 3 has a speed of 7 miles per hour and a stopping distance of 4 feet.
### Car 4
Car 4 has a speed of 7 miles per hour and a stopping distance of 22 feet.
### Car 5
Car 5 has a speed of 8 miles per hour and a stopping distance of 16 feet.
### Car 6
Car 6 has a speed of 9 miles per hour and a stopping distance of 10 feet.
### Car 7
Car 7 has a speed of 10 miles per hour and a stopping distance of 18 feet.
### Car 8
Car 8 has a speed of 10 miles per hour and a stopping distance of 26 feet.
### Car 9
Car 9 has a speed of 10 miles per hour and a stopping distance of 34 feet.
### Car 10
Car 10 has a speed of 11 miles per hour and a stopping distance of 17 feet.
### Car 11
Car 11 has a speed of 11 miles per hour and a stopping distance of 28 feet.
### Car 12
Car 12 has a speed of 12 miles per hour and a stopping distance of 14 feet.
### Car 13
Car 13 has a speed of 12 miles per hour and a stopping distance of 20 feet.
### Car 14
Car 14 has a speed of 12 miles per hour and a stopping distance of 24 feet.
### Car 15
Car 15 has a speed of 12 miles per hour and a stopping distance of 28 feet.
### Car 16
Car 16 has a speed of 13 miles per hour and a stopping distance of 26 feet.
### Car 17
Car 17 has a speed of 13 miles per hour and a stopping distance of 34 feet.
### Car 18
Car 18 has a speed of 13 miles per hour and a stopping distance of 34 feet.
### Car 19
Car 19 has a speed of 13 miles per hour and a stopping distance of 46 feet.
### Car 20
Car 20 has a speed of 14 miles per hour and a stopping distance of 26 feet.
### Car 21
Car 21 has a speed of 14 miles per hour and a stopping distance of 36 feet.
### Car 22
Car 22 has a speed of 14 miles per hour and a stopping distance of 60 feet.
### Car 23
Car 23 has a speed of 14 miles per hour and a stopping distance of 80 feet.
### Car 24
Car 24 has a speed of 15 miles per hour and a stopping distance of 20 feet.
### Car 25
Car 25 has a speed of 15 miles per hour and a stopping distance of 26 feet.
### Car 26
Car 26 has a speed of 15 miles per hour and a stopping distance of 54 feet.
### Car 27
Car 27 has a speed of 16 miles per hour and a stopping distance of 32 feet.
### Car 28
Car 28 has a speed of 16 miles per hour and a stopping distance of 40 feet.
### Car 29
Car 29 has a speed of 17 miles per hour and a stopping distance of 32 feet.
### Car 30
Car 30 has a speed of 17 miles per hour and a stopping distance of 40 feet.
### Car 31
Car 31 has a speed of 17 miles per hour and a stopping distance of 50 feet.
### Car 32
Car 32 has a speed of 18 miles per hour and a stopping distance of 42 feet.
### Car 33
Car 33 has a speed of 18 miles per hour and a stopping distance of 56 feet.
### Car 34
Car 34 has a speed of 18 miles per hour and a stopping distance of 76 feet.
### Car 35
Car 35 has a speed of 18 miles per hour and a stopping distance of 84 feet.
### Car 36
Car 36 has a speed of 19 miles per hour and a stopping distance of 36 feet.
### Car 37
Car 37 has a speed of 19 miles per hour and a stopping distance of 46 feet.
### Car 38
Car 38 has a speed of 19 miles per hour and a stopping distance of 68 feet.
### Car 39
Car 39 has a speed of 20 miles per hour and a stopping distance of 32 feet.
### Car 40
Car 40 has a speed of 20 miles per hour and a stopping distance of 48 feet.
### Car 41
Car 41 has a speed of 20 miles per hour and a stopping distance of 52 feet.
### Car 42
Car 42 has a speed of 20 miles per hour and a stopping distance of 56 feet.
### Car 43
Car 43 has a speed of 20 miles per hour and a stopping distance of 64 feet.
### Car 44
Car 44 has a speed of 22 miles per hour and a stopping distance of 66 feet.
### Car 45
Car 45 has a speed of 23 miles per hour and a stopping distance of 54 feet.
### Car 46
Car 46 has a speed of 24 miles per hour and a stopping distance of 70 feet.
### Car 47
Car 47 has a speed of 24 miles per hour and a stopping distance of 92 feet.
### Car 48
Car 48 has a speed of 24 miles per hour and a stopping distance of 93 feet.
### Car 49
Car 49 has a speed of 24 miles per hour and a stopping distance of 120 feet.
### Car 50
Car 50 has a speed of 25 miles per hour and a stopping distance of 85 feet.

似乎在代码块中打印字符串不起作用 screen capture of compiled knitr result这只会在输出框中产生丑陋的字符串。我希望将字符串视为编译为 html/pdf 的 Markdown 。

最佳答案

使用 results = 'asis' block 选项并使用 cat() 打印。

```{r cars, results='asis'}

for(i in 1:nrow(cars)){
cat("### Car ", i, "\n\n")
cat("Car", i, "has a speed of", cars$speed[i], "miles per hour and a stopping distance of", cars$dist[i], "feet\n\n", sep = " ")
}

```

enter image description here

关于r - 如何从代码块内部呈现 Markdown ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63274488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com