我正在使用Map并lapply获取数据框列表,并将它们转换为Shiny应用程序中的一系列列表。
特定地
# list of dataframes
datalist <- list(data.frame(A = c("col_1", "col_2", "col_3"), B = c("val_1", "val_2", "val_3")),
data.frame(X = c("col_4", "col_5", "col_6"), Y = c("val_4", "val_5", "val_6")),
data.frame(A = c("col_7", "col_8", "col_9"), B = c("val_7", "val_8", "val_9")))
# named list
names(datalist) <- c("Group 1", "Group 2", "Group 3")
我想看起来像:
第1组:
第2组:
第3组:
library(shiny)
# Create each list element with text from the first column of the datalist
# And red text from the second column
rowBlock <- function(name, label) {
tags$li(
div(name,
span(label, style="color: red;"))
)
}
# Each dataframe in the datalist is its own list
# Use the name of the dataframe as the list name
# Then use BOTH columns in each dataframe for the black and red text of each block
# Is this an lapply or mapply with two arguments??
rowPallete <- function(data) {
Map(function(x, y)
# each list has the name as the title
div(h5(x),
# then use the data for rowBlock
# but also need to specify label data here?
tags$ul(lapply(y, rowBlock))),
names(data),
data)
}
ui <- rowPallete(datalist)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
我想使用repex简化此操作,但是由于嵌套的lapply和所需的输出而很难,因此,很抱歉,如果可以更简洁的方式呈现它,我也不想排除任何其他方法。
任何帮助表示赞赏!
这里我们可能需要sprintf
或paste
。用list
循环lapply
后>
lapply(datalist, function(x) sprintf("%s[in red: %s]", x[[1]], x[[2]]))
#$`Group 1`
#[1] "col_1[in red: val_1]" "col_2[in red: val_2]" "col_3[in red: val_3]"
#$`Group 2`
#[1] "col_4[in red: val_4]" "col_5[in red: val_5]" "col_6[in red: val_6]"
#$`Group 3`
#[1] "col_7[in red: val_7]" "col_8[in red: val_8]" "col_9[in red: val_9]"