gpt4 book ai didi

r - 更新 R 表达式

转载 作者:行者123 更新时间:2023-12-04 00:17:34 25 4
gpt4 key购买 nike

我想换出一些用户编写的代码。这些表达式就像一个列表,但我不知道如何更新它们。我可以 append(),但不能 replace()

orig_code <-
parse(text =
"library(tidyverse)
list_1 <- list(a = 1, b = 2)"
)

new_code <- parse(text = "list_1 <- list(a = 1:3)")


# I can append
append(
x = orig_code,
values = new_code
)
#> expression(
#> library(tidyverse),
#> list_1 <- list(a = 1, b = 2),
#> list_1 <- list(a = 1:3)
#> )


# but not replace
replace(
x = orig_code,
list = 2,
values = new_code
)
#> expression(
#> library(tidyverse),
#> list_1 <- list(a = 1, b = 2)
#> )

# or reassign
orig_code[[2]] <- new_code

orig_code
#> expression(
#> library(tidyverse),
#> list_1 <- list(a = 1, b = 2)
#> )

reprex package 创建于 2020-07-05 (v0.3.0)

最佳答案

一种选择是将其转换为list,然后进行替换,并将其更改为expression

as.expression(c(replace(
x = as.list(orig_code),
list = 2,
values = as.list(new_code
))))
#expression(library(tidyverse), list_1 <- list(a = 1:3))

关于r - 更新 R 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62745946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com