对于 R Markdown,如何显示来自 R 变量的矩阵 [英] For R Markdown, How do I display a matrix from R variable
问题描述
我有一个 rmd 文档,其中包含以下内容
I have a rmd document where I have the following
```{r code_block, echo=FALSE}
A = matrix(c(1,3,0,1),2,2)
B = matrix(c(5,3,1,4),2,2)
```
$$
egin{bmatrix}
1 & 0 \
3 & 1 \
end{bmatrix}
*
egin{bmatrix}
5 & 1 \
3 & 4 \
end{bmatrix}
$$
现在我不想手动对 LaTeX 部分进行硬编码,而是可以使用来自变量 A 和 B 的矩阵.这怎么可能?
Now I would like to instead of hard coding the LaTeX part manually, I could use the matrix from the variables A and B instead. How could this be done?
谢谢.
推荐答案
这是 knitr::knit_print()
的完美用例.
您将在其专用的小插图中找到有关 knitr::knit_print()
方法的详细信息:
This is a perfect use case for knitr::knit_print()
.
You will find details about the knitr::knit_print()
method in its dedicated vignette:
vignette('knit_print', package = 'knitr')
目标是为 matrix
类的对象提供 knit_print
方法.正如其他答案所建议的那样,定义运算符可能很有用.
The goal is to provide a knit_print
method for objects of class matrix
. As other answers suggested, it could be useful to define operators.
您将在下面找到一个 Rmd
文件,该文件为您的问题提供了解决方案.它还包含对运营商的建议.
You will find below an Rmd
file that provides a solution to your problem. It also contains a proposal for operators.
这个答案的主要特点是你只需要写
The main feature of this answer is that you only have to write
`r A`
以 LaTeX 内联模式输出矩阵 A(无需输入 $
并编写
to output the matrix A in LaTeX inline mode (no $
to type) and write
```{r echo=FALSE}
A
```
以 LaTeX 显示模式书写.
to write in LaTeX display mode.
我还建议您定义一个 %times%
运算符.因此,您只需编写:
I also propose you to define a %times%
operator. Therefore, you only have to write:
`r A %times% B`
这个答案非常通用,您应该能够将其扩展到其他对象.
This answer is quite generic and you should be able to extend it to other objects.
---
title: "R Markdown: Display a Matrix for R Variable"
author: "Romain Lesur"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
# Define a generic method that transforms an object x in a LaTeX string
as_latex = function(x, ...) {
UseMethod('as_latex', x)
}
# Define a class latex for LaTeX expressions
as_latex.character = function(x) {
structure(
paste(x, collapse = ' '),
class = c('latex', 'character')
)
}
# A character string of class latex is rendered in display mode
# Define a knit_print() method for the latex class
knit_print.latex = function(x, ...) {
knitr::asis_output(
paste0('$$', x, '$$')
)
}
# Now, define a method as_latex for matrix
as_latex.matrix = function(x, ...) {
as_latex(c(
'\begin{bmatrix}',
paste(
t(x),
rep(c(rep('&', nrow(x) - 1), '\\'), ncol(x)),
collapse = ''
),
'\end{bmatrix}'
))
}
# Indicate to knitr that matrix are rendered as latex
knit_print.matrix = function(x, ...) {
knitr::knit_print(as_latex(x))
}
# Build a knitr inline hook to display inline latex in inline mode
default_inline_hook = knitr::knit_hooks$get('inline')
knitr::knit_hooks$set(inline = function(x) {
x = paste(gsub('\$\$', '$', x))
default_inline_hook(x)
})
```
```{r}
A = matrix(c(1,3,0,1),2,2)
B = matrix(c(5,3,1,4),2,2)
```
Now, matrix are rendered as LaTeX:
Matrix A in inline mode: `r A`
Matrix A in display mode:
```{r echo=FALSE}
A
```
### Operators
As other answers suggested, it could be useful to define operators.
With the previous class, it is relatively straightforward:
```{r operators, include=FALSE}
`%times%` = function(x, y) {
as_latex(sapply(list(x, '\times', y), as_latex))
}
`%add%` = function(x, y) {
as_latex(sapply(list(x, '+', y), as_latex))
}
```
Example in inline mode: `r A %add% A %times% B`
Display mode:
```{r echo=FALSE}
A %times% B
```
这篇关于对于 R Markdown,如何显示来自 R 变量的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!