英文:
How to get \% in paste0()?
问题
以下是翻译的部分:
我正在尝试使用R的paste0()
生成一些Latex代码。为了打印百分号,我需要在它前面加一个单反斜杠,即结果应该类似于$95\%$
。然而,paste0()
不允许我添加一个单反斜杠。
paste0(95, "%")
- 得到"95%"
,这对Latex来说当然是无用的。paste0(95, "\%")
- 会引发错误:Error: '\%' is an unrecognized escape in character string starting ""\%"
paste0(95, "\\%")
- 会输出两个反斜杠,即"95\\%"
。但我只想要一个。
在这篇帖子中提到,使用\\%
的paste0()
会输出\%
,但对我来说并非如此。有什么想法?
我正在使用R版本4.2.3(2023-03-15 ucrt)。
编辑:在控制台中使用cat()
的解决方法有效,但在R Markdown/Quarto文档中不会显示,例如在使用以下内联代码时:
``` r cat("95\\%")
提前感谢!
英文:
I'm trying to generate some Latex code using R's paste0()
. In order to print a percent sign, I need a single backslash in front of it, i.e., the result should be something like $95\%$
. However, paste0()
will not let me add a single backslash.
paste0(95, "%")
- Gives"95%"
, which is of course useless for Latex.paste0(95, "\%")
- Will throwError: '\%' is an unrecognized escape in character string starting ""\%"
paste0(95, "\\%")
- Will output BOTH backslashes, i.e.,"95\\%"
. But I only want one.
In this post, it says paste0()
with \\%
will output \%
, but that is not the case for me. Any idea what's going on?
I'm using R version 4.2.3 (2023-03-15 ucrt).
EDIT: Solutions using cat()
work in the console, but are not displayed in an R Markdown/Quarto document, e.g. when using the following inline code:
`r cat("95\\%")`
Thanks in advance!
答案1
得分: 1
I use glue
library(glue)
glue("95\\%")
95\%
We can also generate the strings dinamically:
value <- 95
glue("{value}\\%")
95\%
英文:
I use glue
library(glue)
glue("95\\%")
95\%
We can also generate the strings dinamically:
value <- 95
glue("{value}\\%")
95\%
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论