Reputation: 493
i want to create a value box according to the data i have.
suppose if i have 5 data variable consumerdata
like this,
id data number1 number2
1 k4j A 67 53
2 rls B 30 62
3 yv9 C 45 28
4 l6h D 63 47
5 f08 E 96 75
then i need to create 5 value box with 'name' and 'number1' column. I am getting No data displayed and no error.
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Consumer", tabName = "consumerdata")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "consumerdata",
fluidRow(
tabBox(width = 12,
tabPanel("Label",
box(width = 12,
uiOutput("consumer")
)
)
)
)
)
)
)
)
server <- function(input,output) {
output$consumer <- renderUI({
lapply(consumerdata$name, function(i) {
valueBox(i,
consumerdata$number1, #here display number1 one by one like name
width = 4
)
})
})
}
shinyApp(ui = ui, server = server)
Upvotes: 2
Views: 1031
Reputation: 6325
You almost got there, now you can iterate through a sequence of numbers instead of the elements itself and then use those numbers as index.
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Consumer", tabName = "consumerdata")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "consumerdata",
fluidRow(
tabBox(width = 12,
tabPanel("Label",
box(width = 12,
uiOutput("consumer")
)
)
)
)
)
)
)
)
server <- function(input,output) {
output$consumer <- renderUI({
consumerdata <- head(mtcars) #comment this if you already have consumerdata defined
consumerdata$name <- rownames(consumerdata) #comment this if you already have consumerdata defined
consumerdata$number1 <- 1:6 #comment this if you already have consumerdata defined
lapply(1:length(consumerdata$name), function(i) {
valueBox(consumerdata$name[i],
consumerdata$number1[i], #here display number1 one by one like name
width = 4
)
} )
})
}
shinyApp(ui = ui, server = server)
Upvotes: 3