Render Box Dynamically in Shiny

余生长醉 提交于 2019-12-08 03:52:27

问题


how to render a box in shiny according to the data. data is uploaded by user and it can have more data than this, so i have to create a box dynamically. i am running the below code and i am getting four box created in console not in shiny webpage. please have a look, thankyou.

CODE

   list_data <- list(c("AB","CD","EF","GH"))  #data

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_sentences_tree")
                   )
            ) 
           )  
          )
         )   
        )
       )



server <- function(input, output) {

  output$nlp_sentences_tree <- renderUI({

    for(i in list_data[[1]]){
     print(box(width = 8,
            i
           )
         )
        }
     }) 
    }

  shinyApp(ui = ui, server = server)

回答1:


Have a look here, I've added a button to each just so something is in there

library(shinydashboard)
library(shiny)

list_data <- list(c("AB","CD","EF","GH"))  #data

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_sentences_tree")
                       )
                ) 
              )  
      )
    )   
  )
)



server <- function(input, output) {

  v <- list()
  for (i in 1:length(list_data[[1]])){
    v[[i]] <- box(width = 8, list_data[[1]][i],actionButton(i,i))
  }
  output$nlp_sentences_tree <- renderUI(v)
}

shinyApp(ui = ui, server = server)




回答2:


Or with an lapply and tagList:

server <- function(input, output) {
  output$nlp_sentences_tree <- renderUI({
    a <- lapply(list_data[[1]], function(x) {
      box(width = 8, x)
    })
    tagList(a)
  }) 
}


来源:https://stackoverflow.com/questions/51375520/render-box-dynamically-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!