简体   繁体   English

为什么在 R 中使用 deparse 时,在 output 中会出现 \?

[英]Why do I get \ in output when using deparse in R?

Why and how do I get rid of the \ in deparse:为什么以及如何在 deparse 中摆脱 \:

variable <- "Only output text"
deparse(variable)

Output: [1] "\"Only output text\"" Output: [1] "\"Only output text\""

I would just like to get "Only text" as output.我只想获得“仅文本”为 output。

For example this does not work:例如,这不起作用:

 result_description <- paste("result =", cat(deparse(variable)))
result_description

The \ is simply telling you that the double quote following it is part of the string itself. \只是告诉您它后面的双引号是字符串本身的一部分。 So it is not a \ that is the undesired character in your text, it is the double quotes.因此,文本中不需要的字符不是\ ,而是双引号。 You can remove these with a gsub command very easily:您可以使用gsub命令非常轻松地删除它们:

variable <- "Only output text"
gsub("\"", "", deparse(variable))
#> [1] "Only output text"

However, it's really not clear why you would do this.但是,目前还不清楚您为什么要这样做。 The line gsub("\"", "", deparse(variable)) is identical to variable :gsub("\"", "", deparse(variable))variable相同:

gsub("\"", "", deparse(variable)) == variable
#> [1] TRUE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在矩阵上的R中使用diff时出现错误 - Why do I get error when using diff in R on matrix 使用`deparse(substitute))(或替代)时如何处理空格? - How can I handle whitespace when using `deparse(substitute))` (or an alternative)? 为什么与excel和手动计算相比,使用R获得单向方差分析输出的方式有所不同? - Why do I get different one way ANOVA output using R compared to excel and manual calculation? 为什么用For循环进行R模式匹配时为什么没有输出 - Why there is no output when I do R Pattern Matching with For loop 在R中使用for循环时,如何从稀有命令获得多条输出 - How do I get multiple pieces of output from rarefy command when using for loops in R 在R中,我如何定义一个等效于`deparse(substitute(x))`的函数? - In R, how do I define a function which is equivalent to `deparse(substitute(x))`? R:当函数调用为3点时,无法使用Deparse和Replace完全捕获所有参数 - R: cannot fully capture all arguments using deparse and substitute when function call is 3 dots 使用R调用此api时,为什么我得到的地址必须是字符错误? - Why do I get address must be a character error when I call this api using R? 为什么在尝试使用 R Shiny 和 networkD3 制作桑基图时会出现“缺少参数”输出,没有默认值”? - Why do I get "argument "output" is missing, with no default" when trying to make a sankey plot with R shiny and networkD3? 为什么在r中使用group_by和summarise()时会出现“找不到对象”错误? - Why do I get an “Object not found” error when using group_by and summarise() in r?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM