将`=`变成`<;-`仅使用基本R 问题 将赋值等号转换为赋值箭头 仅使用基本R(无样式器或格式器) 上下文
例子 输入:将`=`变成`<;-`仅使用基本R 问题 将赋值等号转换为赋值箭头 仅使用基本R(无样式器或格式器) 上下文,r,parsing,text-styling,R,Parsing,Text Styling,例子 输入: f = function(x = 1){} 期望输出: f <- function(x = 1){} f发布在问题中,但不妨尝试一些SO pts: library(magrittr) raw_src <- "z = {f('#') # comment x <- 5 y = 'test' }" # so we can have some tasty parse data first <- parse(text = raw_src, keep.
f = function(x = 1){}
期望输出:
f <- function(x = 1){}
f发布在问题中,但不妨尝试一些SO pts:
library(magrittr)
raw_src <- "z = {f('#') # comment
x <- 5
y = 'test'
}"
# so we can have some tasty parse data
first <- parse(text = raw_src, keep.source = TRUE)
# this makes a nice data frame of the tokenized R source including line and column positions of the source bits
src_info <- getParseData(first, TRUE)
# only care about those blasphemous = assignments
elements_with_equals_assignment <- subset(src_info, token == "EQ_ASSIGN")
# take the source and split it into lines
raw_src_lines <- strsplit(raw_src, "\n")[[1]]
# for as many instances in the data frame replace the = with <-
for (idx in 1:nrow(elements_with_equals_assignment)) {
stringi::stri_sub(
raw_src_lines[elements_with_equals_assignment[idx, "line1"]],
elements_with_equals_assignment[idx, "col1"],
elements_with_equals_assignment[idx, "col2"]
) <- "<-"
}
# put the lines back together and do the thing
parse(
text = paste0(raw_src_lines, collapse="\n"),
keep.source = FALSE
)[[1]] %>%
deparse() %>%
cat(sep = "\n")
## z <- {
## f("#")
## x <- 5
## y <- "test"
## }
库(magrittr)
原始的src发布在问题中,但也可以尝试一些SO pts:
library(magrittr)
raw_src <- "z = {f('#') # comment
x <- 5
y = 'test'
}"
# so we can have some tasty parse data
first <- parse(text = raw_src, keep.source = TRUE)
# this makes a nice data frame of the tokenized R source including line and column positions of the source bits
src_info <- getParseData(first, TRUE)
# only care about those blasphemous = assignments
elements_with_equals_assignment <- subset(src_info, token == "EQ_ASSIGN")
# take the source and split it into lines
raw_src_lines <- strsplit(raw_src, "\n")[[1]]
# for as many instances in the data frame replace the = with <-
for (idx in 1:nrow(elements_with_equals_assignment)) {
stringi::stri_sub(
raw_src_lines[elements_with_equals_assignment[idx, "line1"]],
elements_with_equals_assignment[idx, "col1"],
elements_with_equals_assignment[idx, "col2"]
) <- "<-"
}
# put the lines back together and do the thing
parse(
text = paste0(raw_src_lines, collapse="\n"),
keep.source = FALSE
)[[1]] %>%
deparse() %>%
cat(sep = "\n")
## z <- {
## f("#")
## x <- 5
## y <- "test"
## }
库(magrittr)
raw_src可能是sub(“=”,“输入可以是字符向量或语言对象,也可以是好的。sub()
将x=1
替换为x如果您的输入是字符向量,那么sub(“=”,“可能是sub(“=”,“输入可以是字符向量或语言对象,也可以是好的。sub()
将x=1
替换为x如果您的输入是字符向量,则sub(“=”,“+1),但我可以用”=”(zz,yy)将其打断
和zz=yy=3
是的。我在GH杂志上提到了这一点。所以我不能享受所有的乐趣。太棒了,@hrbrmstr!谢谢!@Moody_mudscappper我刚刚在+1发表了你的评论,但我可以用'='(zz,yy)来打断它
和zz=yy=3
是的。我在GH杂志上提到了这一点。所以不能享受所有的乐趣。太棒了,@hrbrmstr!谢谢!@Moody_mudscapper我刚刚在上发表了你的评论。