R:如何将`"NULL"`转换为`NULL`?

问题描述 投票:0回答:1

如何将 NULL (

"NULL"
) 字符串转换为 R 中实际的
NULL
值?

我得到一个包含字符串或

"NULL"
(也是字符串)的环境变量,我的 R 代码正在寻找字符串或
NULL
。这看起来很简单,但例如
stringr::str_replace_all("test", 'NULL', NULL)
不起作用:

> stringr::str_replace_all("test", 'NULL', NULL)
Error in `stringr::str_replace_all()`:
! `replacement` must be a character vector, not `NULL`.
r string null type-conversion
1个回答
0
投票

我设法用一个更简单的 if/else 来实现它:

fun(
base.url = if (Sys.getenv('BASE_URL') == 'NULL') {as.null()} else {Sys.getenv('BASE_URL')}, ...) {
...
}
© www.soinside.com 2019 - 2024. All rights reserved.