不适用于2个向量?一个闪亮的例子

时间:2020-01-21 16:30:06

标签: r lapply

对于与带有多个参数的lapply有关的帖子这有点多余,我感到很抱歉,但是我仍在努力将这些概念应用到嵌套函数中。如果标题不太符合我的问题并愿意提出建议,我也表示敬意!

我想用一系列数据框在Shiny中创建一系列列表,其中每个列表都有一个对应于数据框名称的标题:

所需的输出

测试

  • A
  • B
  • C

test2

  • D
  • E
  • F

test3

  • G
  • H

我首先创建了all_tests,这是一个包含所有数据框的列表,其名称与该数据框相对应。

我想在一系列功能中使用它:

  • rowBlock创建li元素(A,B,C,D,E,F)等
  • rowPallete为每个数据框创建所有rowBlocks的ul元素,以及数据框的标题
  • rowArea组合给定用户指定的数据帧列表(all_tests)的所有rowPalletes。这是我要在App.R中使用的功能
library(shiny)

# This is a repex where really I'm going to be importing x # of data frames
# I want to use their column names within each list
# And title each list the name of the dataframe
test <- data.frame("A" = NA, "B" = NA, "C" = NA)
test2 <- data.frame("D" = NA, "E" = NA, "F" = NA)
test3 <- data.frame("G" = NA, "H" = NA, "I" = NA)

all_tests <- list(colnames(test), colnames(test2), colnames(test3))
names(all_tests) <- c("test", "test2", "test3")

# each column name should be a li
rowBlock <- function(name) {
  tags$li(
    class = "block", id = name,
    div(name)
  )
}

# each dataframe should be its own list
# and titled with the name of the df
rowPallete <- function(data) {
  div(
    lapply(names(data), h5),
    tags$ul(
      class="all_blocks",
      lapply(data, rowBlock)
    ))
}

# combine the different dataframes into a series of lists
# to be used within app.R
rowArea <- function(bins) {
  column(1, offset = 0, style='padding:0px;',
         lapply(bins, rowPallete)
  )
}

当我不包含ul标题时,我可以使代码工作,但是我正在努力将两个向量应用于同一个lapply函数。我一直在使用column namesnames(all_tests),但似乎仍然缺少一些东西。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

这似乎是您所追求的(rowBlock保持不变)

# each dataframe should be its own list
# and titled with the name of the df
rowPallete <- function(data) {
  Map(function(x, y) 
        div(h5(x), tags$ul(class = 'all_blocks', lapply(colnames(y), rowBlock))),
      names(data),
      data)
}

rowArea <- function(bins) {
  column(1, offset = 0, style='padding:0px;',
         rowPallete(bins)
  )
}

ui <- rowArea(all_tests)
server <- function(input, output) {
}

shinyApp(ui = ui, server = server)