![Sexpr Sexpr]()
使用块选项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 tablesknitr::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 valueEDIT 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 instruct knitr not to modify the output.Use cat instead of print because print 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 inside makeItNiceR to save some typing, but this makes the function less flexible and we cannot use it in combination with \Sexpr{} anymore.)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块中布局字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 13:01