我正在尝试在一个 bs4TabItem 中使用fluidRow,它最多有 3 个 bs4UserCard 项目。FluidRows 应该在 for 循环中动态构建,并且最大。3 bs4UserCard 也应该在 for 循环中动态构建。下面是代码片段。我已经根据其他建议尝试了 UI 和服务器部分中的代码,但它仍然不起作用。
# Get number of rows from dataset
records = nrow(candidatesDF)
# Each row will have max. 3 items
numRows = ceiling(nrow(candidatesDF) / 3)
numRows = c(1:numRows)
count = 0
offset = 3
candidates = bs4TabItem(tabName = "candidates",
for (row in numRows) {
fluidRow(
if (records < offset) {
offset = records
}
for (column in 1:offset) {
count = count + 1
# Convert the names to a more legible format
name = explode(candidatesDF[count, "Candidate"], sep = ", ")
name = paste0(name[2], ' ', name[1])
name = capitalizeStrings(name, all.words = TRUE, lower.back = TRUE)
# Convert the names to the img name format
imgName = explode(name, sep = " ")
imgName = tolower(implode(imgName, "_"))
imgUrl = paste0("img/", imgName, ".png")
# Create a user card on each iteration.
bs4UserCard(
title = name,
subtitle = candidatesDF[count, "Party"],
type = NULL,
width = 4,
src = imgUrl,
status = "danger",
closable = TRUE,
elevation = 4,
imageElevation = 4,
fluidRow(
column(
width = 4,
descriptionBlock(header = "District",
text = capitalizeStrings(candidatesDF[count, "District"],
all.words = TRUE, lower.back = TRUE ))
),
column(
width = 4,
descriptionBlock(header = "Votes",
text = candidatesDF[count, "Votes"])
),
column(
width = 4,
descriptionBlock(header = "Result",
text = candidatesDF[count, "Result"], right_border = FALSE)
)
)
)
records = records - 1
}
)
}
)
使用 if 语句,我收到此错误
Possible missing comma at:
87: for (row in fluidRows) {
^
如果我仅出于测试目的删除 if 语句,则会收到此错误
Warning: Error in explode: Assertion on 'x' failed: May not be NA.
我不确定 x in explode 是 NA 因为我在数据集中没有任何 NA 值。当我逐行运行代码测试explode函数时,返回了预期的结果,所以我不明白为什么NA。
尽管如此,我的目标是创建 x 数量的 fluidRows,每行最多包含 3 个项目,并从数据集中动态生成项目的信息。################################################# ##################### 我已更新代码以反映使用 lapply() 的建议。
# Get number of rows from dataset
records = nrow(candidatesDF)
# Each row will have max. 3 items
numRows = ceiling(nrow(candidatesDF) / 3)
numRows = c(1:numRows)
count = 0
offset = 3
checkOffset = function(records, offset) {
if (records < offset) {
offset = records
}
return(offset)
}
candidates = bs4TabItem(tabName = "candidates",
lapply(numRows, function(r) {
fluidRow(
lapply(1:checkOffset(records, offset), function(c) {
count = count + 1
print(count)
# Convert the names to a more legible format
name = explode(candidatesDF[count, "Candidate"], sep = ", ")
name = paste0(name[2], ' ', name[1])
name = capitalizeStrings(name, all.words = TRUE, lower.back = TRUE)
# Convert the names to the img name format
imgName = explode(name, sep = " ")
imgName = tolower(implode(imgName, "_"))
imgUrl = paste0("img/", imgName, ".png")
records = records - 1
# Create a user card on each iteration.
bs4UserCard(
title = name,
subtitle = candidatesDF[count, "Party"],
type = NULL,
width = 4,
src = imgUrl,
status = "primary",
closable = TRUE,
elevation = 4,
imageElevation = 4,
fluidRow(
column(
width = 4,
descriptionBlock(header = "District",
text = capitalizeStrings(candidatesDF[count, "District"],
all.words = TRUE, lower.back = TRUE ))
),
column(
width = 4,
descriptionBlock(header = "Votes",
text = candidatesDF[count, "Votes"])
),
column(
width = 4,
descriptionBlock(header = "Result",
text = candidatesDF[count, "Result"], right_border = FALSE)
)
)
)
})
)
})
虽然这对接近预期结果有很大帮助,但每个网格中的每张卡片都是相同的。这是因为 count 变量不会增加,record 变量也不会增加。
提前致谢。