r - 在R代码中使用样式器将T替换为TRUE,F替换为FALSE
问题描述
library("styler")
myugly_code <- "if ( x==T ) print( 'y' )"
style_text(myugly_code)
#> if (x == T) print("y")
由reprex 包(v0.3.0)于 2020 年 1 月 13 日创建
我承认在编写代码时我们应该始终使用TRUE
orFALSE
而不是T
and 。但是,在编辑以前的代码或其他人用and编写的代码时,是否可以使用包替换 all and with and 。在上面的代码中,我想得到:F
R
T
F
styler
T
F
TRUE
FALSE
#> if (x == TRUE) print("y")
解决方案
我不知道 styler,但你可以'T'
用'TRUE'
using替换gsub
。该'\\b'
字符是一个单词边界,因此只有'T'
在每一侧都有一个单词边界的 a 被替换,而不是例如'T'
in 'Test'
。
sub_TF <- function(s){
s <- gsub('\\bT\\b', 'TRUE', s)
gsub('\\bF\\b', 'FALSE', s)
}
sub_TF("list(T, 'Test', F, 'Fox')")
# [1] "list(TRUE, 'Test', FALSE, 'Fox')"
推荐阅读
- gcc - Spin root 找不到 gcc 文件
- connection-pooling - Hikari CP - 持续验证空闲连接
- java - 内存中有数万个 ValueString / ValueLong / ValueDouble 实例。为什么?
- reactjs - 从建议中选择文本时,TextField 背景更改背景颜色
- c++ - 如何一般访问 Boost Graph 捆绑属性?
- lua - 为什么我的动画脚本只能空闲播放?
- android - 究竟IP地址Android系统检查说“没有互联网连接”?
- python - 如何在大字符串中找到链接
- javascript - JavaScript this 关键字在函数中的问题
- r - R Studio 中的函数 system 或 shell 不适用于我在 Windows 10 上的字符串。运行命令的状态为 1