geekzeus
geekzeus

Reputation: 895

Render Box Dynamically in Shiny

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)

Upvotes: 1

Views: 2521

Answers (2)

SeGa
SeGa

Reputation: 9809

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)
  }) 
}

Upvotes: 2

Pork Chop
Pork Chop

Reputation: 29387

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)

enter image description here

Upvotes: 2

Related Questions