如何将 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`.
我设法用一个更简单的 if/else 来实现它:
fun(
base.url = if (Sys.getenv('BASE_URL') == 'NULL') {as.null()} else {Sys.getenv('BASE_URL')}, ...) {
...
}