将元素从嵌套列表传递到renderUI

时间:2018-07-19 09:38:11

标签: r shiny shinydashboard

我想将每个列表的元素呈现到一个valuebox。
我能够像下面的示例一样显示单个列表的元素(运行ex的代码),但不能嵌套列表。
我想要的是让valuebox包含所有列表的元素。
请运行代码以了解想法。谢谢

#this should be the result:

1stvaluebox    2ndvaluebox  3rdvaluebox   4thvaluebox
A                 C             E             H
Kim               John          Satish        Kevin
1                 2             3             4 


#Data and code

list_data <- list(letters = c("A","C","E","H"),names = c("Kim","John","Satish","Kevin"),numbers = 1:4)

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Text Mining"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("NLP Tree", tabName = "NLP")
    )
  ),

  dashboardBody(
    tabItems(
      tabItem(tabName = "NLP",
          fluidRow(
            tabBox(width = 12,height="500",
                   tabPanel("Sentences",
                            uiOutput("nlp_entities")
                   )
            ) 
          )  
        )
      )     
    ) 
  )



 server <- function(input, output) {
    output$nlp_entities <- renderUI({
      a <- lapply(list_data[[1]], function(x) {
         valueBox(x,"names")
     })
    tagList(a)
  }) 
 }
   shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:3)

您可以从1迭代到子列表的长度,并在每次迭代中提取所需信息。

server <- function(input, output) {
    output$nlp_entities <- renderUI({
        a <- list()
        for(i in seq_len(lengths(list_data)[1])) {
            a[[i]] <- valueBox(lapply(list_data[c(1, 3)], "[[", i), 
                               list_data[[2]][i])
        }
        tagList(a)
    }) 
}

enter image description here