rmarkdown文档中乳胶内部的r块

发布于 2025-01-30 19:49:10 字数 962 浏览 3 评论 0原文

我正在尝试在以下rmarkDown文档中在乳胶代码中进行R块运行:

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
```{r}
x <- 2+3
x
```
}
\end{enumerate}

输出为:

“在此处intrape

但我想要:

编辑:我希望对R块进行评估(但结果隐藏)及其代码。同时,我找到了这个解决方案,但是也许有一个更简单的解决方案: https://tex.stackexchange.com/questions/questions/210003/how-can-can-i-nest-a-nest-a-code-code-code-chunk-within-an-enumerate-enumerate-environment - 使用R-MarkDow

I am trying to get an R chunk run inside LaTeX code in the following rmarkdown document:

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
```{r}
x <- 2+3
x
```
}
\end{enumerate}

The output is:

enter image description here

But I want:

enter image description here

Could you please help me?

EDIT: I want the R chunk to be both evaluated (but result hidden) and its code shown. I have meanwhile found this solution, but maybe there is a simpler one: https://tex.stackexchange.com/questions/210003/how-can-i-nest-a-code-chunk-within-an-enumerate-environment-when-using-r-markdow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰沐子 2025-02-06 19:49:10

你需要这个吗?请参阅此处参见 https://bookdown.org/yihui/rmark-rmarkdown-cook/raw-cookbook/raw-cookbook/raw-cookbook/raw-cookbook-raw-cook-raw--raw--raw--raw--raw--raw-cook-raw--raw--cook- latex.html

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

```{=latex}

\begin{tabular}{ll}

x <- 2+3\\
x\\

\end{tabular}
```

”在此处输入图像说明”

You need this? See here https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

```{=latex}

\begin{tabular}{ll}

x <- 2+3\\
x\\

\end{tabular}
```

enter image description here

各自安好 2025-02-06 19:49:10

在以下任何示例中,您都可以替换双返回而不是\\

如果您想仅显示这些表达式,则不要将其放在代码块中:

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
x <- 2 + 3\\x
}
\end{enumerate}

这将产生:

< img src =“ https://i.sstatic.net/ajzst.png” alt =“在此处输入图像说明”>


否则,如果您想评估内联表达式,请使用backticks和r :

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
`r x <- 2 + 3; x`
}
\end{enumerate}

生产:

”在此处输入图像描述”


最后,您当然可以结合这两个概念来显示表达式并通过此操作来评估该值:

x <- 2 + 3\\
`r x <- 2 + 3; x`

如果您的表达式更复杂在您的乳胶外进行评估。


更新

为了简单的表达方式,您可以做类似的事情:

```{r, include = F}
exprsn <- "x <- 2 + 3"
```

\begin{enumerate}

\item 
{
`r exprsn`\\
`r eval(parse(text = exprsn)); x`
}
\end{enumerate}

In any of the following examples, you can substitute a double return instead of \\.

If you are wanting to just show those expressions then do not put it in a code chunk:

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
x <- 2 + 3\\x
}
\end{enumerate}

This will produce:

enter image description here


Otherwise, if you want to evaluate an inline expression use backticks and r:

---
title: "Untitled"
output: pdf_document
date: '2022-05-20'
---

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

\begin{enumerate}

\item 
{
`r x <- 2 + 3; x`
}
\end{enumerate}

Which produces:

enter image description here


Lastly, you can of course combine these two concepts to show the expression and evaluate the value by doing this:

x <- 2 + 3\\
`r x <- 2 + 3; x`

If your expression is more complex, I would recommend having the code chunk outside of your LaTeX for evaluation.


Update

For simpler expressions you could do something like:

```{r, include = F}
exprsn <- "x <- 2 + 3"
```

\begin{enumerate}

\item 
{
`r exprsn`\\
`r eval(parse(text = exprsn)); x`
}
\end{enumerate}
旧城空念 2025-02-06 19:49:10

同时,我发现了这一点:使用r markdown?,如何在枚举环境中嵌套代码块?,它启发了以下解决方案:

---
title: "Untitled"
output:
  pdf_document:
    highlight: monochrome
date: '2022-05-20'
header-includes:
- \newcommand{\benum}{\begin{enumerate}}
- \newcommand{\eenum}{\end{enumerate}}
---

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

\benum

\item 

```{r}
x <- 2+3
x
```

\eenum

Meanwhile, I found this: How can I nest a code chunk within an enumerate environment when using R Markdown?, which inspires the following solution:

---
title: "Untitled"
output:
  pdf_document:
    highlight: monochrome
date: '2022-05-20'
header-includes:
- \newcommand{\benum}{\begin{enumerate}}
- \newcommand{\eenum}{\end{enumerate}}
---

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

\benum

\item 

```{r}
x <- 2+3
x
```

\eenum

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文