如何在R块中布局字符值 [英] How to layout character value within R chunk
问题描述
在knitr软件包中,我喜欢kable函数.即使在R代码块中调用它,它也可以很好地布局表和类似对象的数据框架.现在,我想对一个字符值做同样的事情.有没有提供可以格式化的类似kable的输出("kprint")的功能?
knitr::kable() # exists for tables
knitr::kprint() # does a function like this exists for character values?
这就是我现在得到的:
print("character value") # within the R Chunk
生成的报告中的输出:
## [1] "character value"
这就是我想要的,只是:
字符值
EDIT cat("character value")不是我要的解决方案,因为我不再需要R输出,而只是纯文本.
要从R到TEX获取原始"字符串(没有任何格式或其他输出,例如[1]
),有两件事要做:>
- 使用块选项
results = "asis"
指示knitr
不要修改输出. - 使用
cat
而不是print
,因为print
将向量的长度加引号,并引用到输出中.
在这种情况下,使用\Sexpr{}
的内联输出可能有用,因为默认情况下,\Sexpr{}
会按原样打印:\Sexpr{myoutput}
.
由于在注释中存在如何格式化输出的问题,这里有一些选择:
- 将LaTeX添加到传递给
cat
:cat("\\emph{foo}")
的文本中.别忘了通过\
逃脱\
. -
执行与上述相同的操作,但是使用函数来完成肮脏的工作":
makeItNiceR <- function(x) { return(paste("\\fbox{\\texttt{", x, "}}")) } cat(makeItNiceR("foo bar is nice"))
- (请注意,我们可以在
makeItNiceR
内使用cat
来保存某些键入内容,但这会使函数的灵活性降低,因此我们无法再将其与\Sexpr{}
结合使用.)
- (请注意,我们可以在
-
在
\Sexpr{}
周围手动添加LaTeX格式化命令:Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX.
-
组合
makeItNiceR
和\Sexpr{}
以从\Sexpr{}
获得格式正确的输出:\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
以下最少的示例演示了上面所有代码段的用法:
\documentclass{article}
\begin{document}
<<results = "asis">>=
makeItNiceR <- function(x) {
return(paste("\\fbox{\\texttt{", x, "}}"))
}
myoutput <- "slim"
cat("foo")
cat("\\emph{foo}")
cat(makeItNiceR("foo bar is nice"))
@
\paragraph{Outside of chunk:} ~\\
\Sexpr{myoutput} \\
Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX. \\
\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
\end{document}
In the knitr package I like the kable function. It gives a nice layout of tables and data frame like objects even as it is called from within an R code chunk. Now I want to do the same thing with a character value. Is there a function that gives a kable-like output ("kprint") that can be formated?
knitr::kable() # exists for tables
knitr::kprint() # does a function like this exists for character values?
This is what I get now:
print("character value") # within the R Chunk
Output in generated report:
## [1] "character value"
And this is what I want, just:
character value
EDIT cat("character value") is not the solution I am looking for because I don't want an R output anymore, but just a plain text.
There are two things to do to get a "raw" character string (without any formatting or additional output like [1]
) from R to TEX:
- Use the chunk option
results = "asis"
to instructknitr
not to modify the output. - Use
cat
instead ofprint
becauseprint
adds the lenght of the vector and quotes to the output.
In this context, inline output using \Sexpr{}
might be useful because values in \Sexpr{}
are by default printed "as they are": \Sexpr{myoutput}
.
As there was the question of how to format the output in the comments, here some options:
- Add LaTeX to the text you pass to
cat
:cat("\\emph{foo}")
. Don't forget to escape\
by an additional\
. Do the same thing as above, but use a function to do the "dirty work":
makeItNiceR <- function(x) { return(paste("\\fbox{\\texttt{", x, "}}")) } cat(makeItNiceR("foo bar is nice"))
- (Note that we could use
cat
insidemakeItNiceR
to save some typing, but this makes the function less flexible and we cannot use it in combination with\Sexpr{}
anymore.)
- (Note that we could use
Manually add LaTeX formatting commands around
\Sexpr{}
:Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX.
Combine
makeItNiceR
and\Sexpr{}
to get nicely formatted output from\Sexpr{}
:\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
The following minimal examples demonstrates the usage of all code snippets from above:
\documentclass{article}
\begin{document}
<<results = "asis">>=
makeItNiceR <- function(x) {
return(paste("\\fbox{\\texttt{", x, "}}"))
}
myoutput <- "slim"
cat("foo")
cat("\\emph{foo}")
cat(makeItNiceR("foo bar is nice"))
@
\paragraph{Outside of chunk:} ~\\
\Sexpr{myoutput} \\
Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX. \\
\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
\end{document}
这篇关于如何在R块中布局字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!