【发布时间】:2020-06-15 07:58:00
【问题描述】:
Lapply with labels = names(x),返回怪异闪亮的 NULL HTML 类; lapply with labels = as.character(names(x)),返回正确的 HTML 类,但不放置标签。任何想法为什么?
代表:
library(shiny)
items <- list(
"A" = "a",
"B", = "b"
)
num_inputs <- function(items){
labels <- names(items)
temp. <- NULL
for(i in 1:length(items)){
temp. <- list(temp., numericInput(inputId = items[i],
label = labels[i],
value = 1,
min = 0,
max = 10,
step = 0.1)
)
}
return(temp.)
}
# doesn't work
num_inputs_fail <- function(items){
lapply(items, FUN = function(x){
numericInput(inputId = x,
label = as.character(names(x)),
value = 1,
min = 0,
max = 10,
step = 0.1)
})
}
attempt1 <- tagList(num_inputs(items))
attempt2 <- tagList(num_inputs_fail(items))
注意这里(对于仅尝试1[1]和尝试[2],标签几乎相同,但缺少实际标签!
<div class="form-group shiny-input-container">
<label class="control-label" for="a">A</label>
<input id="a" type="number" class="form-control" value="1" min="0" max="10" step="0.1"/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" for="a"></label>
<input id="a" type="number" class="form-control" value="1" min="0" max="10" step="0.1"/>
</div>
本页:Create dynamic number of input elements with R/Shiny
包括以下代码:
output$sliders <- renderUI({
members <- as.integer(input$members) # default 2
max_pred <- as.integer(input$max_pred) # default 5000
lapply(1:members, function(i) {
sliderInput(inputId = paste0("ind", i), label = paste("Individual", i),
min = 0, max = max_pred, value = c(0, 500), step = 100)
})
})
在 renderUI 中,链接的闪亮应用似乎正确地具有标签;但即使在我的 lapply 中使用 paste() 函数也不能解决它。
我的 R 版本: "R 版本 3.5.1 (2018-07-02)"
我的闪亮版本: '1.4.0'
【问题讨论】:
-
您可以在
names(items)上使用lapply,例如:lapply(names(items), FUN = function(x){ numericInput(inputId = items[[x]], label = x...这就是您要查找的内容吗? -
这很巧妙。爱它!我试试看。
标签: html r shiny functional-programming numeric-input