R 在Swave中储存和以后使用乳胶标题的最佳方法?

R 在Swave中储存和以后使用乳胶标题的最佳方法?,r,text,latex,sweave,R,Text,Latex,Sweave,我想在swave的R模式下编写图形标题,然后将标题添加到列表中,然后在图形标题中使用它们,例如: caption <- list() myresult <- data.frame(name = c('fee', 'fi'), x = c(1,2)) caption[['fig1']] <- "$\text{\Sexpr{myresult$name[1]}}\Sexpr{myresult$x[1]$" caption[['fig2']] <- "$\text{\Sexpr

我想在swave的R模式下编写图形标题,然后将标题添加到列表中,然后在图形标题中使用它们,例如:

caption <- list()
myresult <- data.frame(name = c('fee', 'fi'), x = c(1,2))
caption[['fig1']] <- "$\text{\Sexpr{myresult$name[1]}}\Sexpr{myresult$x[1]$" 
caption[['fig2']] <- "$\text{\Sexpr{myresult$name[2]}}\Sexpr{myresult$x[2]$"

有没有一种方法可以将这样的字符串存储在列表中,或者更好的方法?

双转义
\
字符。你不需要双方括号

caption <- list()
myresult <- data.frame(name = c('fee', 'fi'), x = c(1,2))
caption['fig1'] <- "$\\text{\\Sexpr{myresult$name[1]}}\\Sexpr{myresult$x[1]$" 
caption['fig2'] <- "$\\text{\\Sexpr{myresult$name[2]}}\\Sexpr{myresult$x[2]$"

这回答了我的问题。。。我专注于一种方法来实现这一点,因为我已经写出了所有这些字符串,并希望将它们封装在一个函数中。。。但除了在特定于任务的函数中包装
gsub()
之外,我找不到其他函数。
caption <- list()
myresult <- data.frame(name = c('fee', 'fi'), x = c(1,2))
caption['fig1'] <- "$\\text{\\Sexpr{myresult$name[1]}}\\Sexpr{myresult$x[1]$" 
caption['fig2'] <- "$\\text{\\Sexpr{myresult$name[2]}}\\Sexpr{myresult$x[2]$"
genCaption <- function(name, value){
    sprintf("$\\text{%s}%.3f$", name, value)
}
> genCaption("pi", pi)
[1] "$\text{pi}3.142$"