是否有更好,更好的方法将混合内容的命名列表转换为数据框?
工作实例:
my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)
my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)
my_df[["value"]] <- unname(my_list)
Run Code Online (Sandbox Code Playgroud)
是否可以一步完成此转换?
我一直在尝试结合mutate_at使用coalesce,以防动态生成列名。
在我的示例中,只有五列,但是在实际数据中,则有更多列(并非所有列都应包含在coalesce步骤中)。
DF示例:
data_example <- data.frame(
aa = c(1, NA, NA),
bb = c(NA, NA, 2),
cc = c(6, 7, 8),
aa_extra = c(2, 2, NA),
bb_extra = c(1, 2, 3)
)
Run Code Online (Sandbox Code Playgroud)
预期产量:
aa bb cc aa_extra bb_extra
1 1 1 6 2 1
2 2 2 7 2 2
3 NA 2 8 NA 3
Run Code Online (Sandbox Code Playgroud)
输出为structure:
structure(list(aa = c(1, 2, NA), bb = c(1, 2, 2), cc = c(6, 7,
8), …Run Code Online (Sandbox Code Playgroud) 在 ShinyconditionalPanel中的conditionarg 中,可以访问 JSinput对象,因此,如果有小部件,则可以通过 JS 表达式numericInput("num", label = h3("Numeric input"), value = 1)访问该小部件的值。conditioninput.num
问题是如何以同样的方式访问input自己的 JS 脚本(在 Shiny 应用程序中启动)中的对象,或者仅从在 Shiny 应用程序页面上打开的浏览器控制台访问对象?
输入数据的简单示例:
dataset <- data.frame("part1" = c("a", "b", "c"),
"part2" = c("x", "y", "z"),
"caption" = c("{part1} {part2}",
"{part2} {part1}",
"{part2} {part1} {part2}"),
stringsAsFactors = F)
Run Code Online (Sandbox Code Playgroud)
预期成绩:
# A tibble: 3 x 3
part1 part2 caption
<chr> <chr> <chr>
1 a x a x
2 b y y b
3 c z z c z
Run Code Online (Sandbox Code Playgroud)
下面的代码不起作用,因为.指的是整个dataset,而不是整行内容的数据:
dataset %>%
rowwise() %>%
mutate("caption" =
glue::glue_data(., caption)
)
Run Code Online (Sandbox Code Playgroud)
问题:如何将行(所有)内容传递给glue?
有效的代码(明确声明的行“内容”)不是我一直在寻找的,因为caption在我的数据集中“模式”中使用了更多的列,因此我想避免手动声明它,只需通过整行内容。
dataset %>%
rowwise() %>%
mutate("caption" = …Run Code Online (Sandbox Code Playgroud)