我正在尝试将用户提供的字符串和固定的字符串拼接在一个图表标题中。
当然,一个简单的可以工作的例子:
userTitle <- "user title" # Example 1
fullTitle <- paste(userTitle, ": Results", sep = "")
plot(1:10, main = fullTitle)
但是,如果用户的标题包含表达式该怎么办?我尝试过以下几种方法:
# This works, but doesn't include the fixed string # Example 2
userTitle <- expression(italic(Genus)~italic(species)) # EDIT: this was missing
fullTitle <- bquote(.(userTitle))
plot(1:10, main = fullTitle)
尝试添加固定字符串。以下是一些不太有效的方法:
fullTitle <- bquote(.(userTitle)~':'~Results) # Example 3
plot(1:10, main = fullTitle) # title missing .(userTitle)
fullTitle <- bquote(paste("Results:", .(userTitle))) # Example 4
plot(1:10, main = fullTitle) # title missing .(userTitle)
但是这个例子来自这里,完全可以正常工作[编辑:链接是错误的问题]。
x<- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=",.(x))))
我的示例4看起来几乎与上一个完全相同,但行为不同。有如此多的、、组合,我已经查看了很多答案,但可能会错过一些非常小的东西。如果用户字符串包含表达式,您有什么建议可以将用户字符串和固定字符串结合在一起?谢谢。
as.expression
将评估它的参数。 - IRTFMplot(1:10, main = expression(ARL[1] ~ "curve for" ~ S^2))
复制粘贴到控制台中,它就起作用了。 - Bryan Hanson