Info boxes

We have already seen how to use icons earlier in this chapter, but shinydashboard makes a nice feature of it by expanding and coloring icons to draw attention to key pieces of information. An info box can be drawn statically as follows:

infoBox(width = 3, "Shiny version", "1.1.0", 
  icon = icon("desktop")) 

As you can see, the width can be set (using the 12 span rule from the standard Bootstrap functions we saw earlier in this chapter) with a title (Shiny version) and value (1.1.0) (although you may often wish to pass a number). This function is placed within dashboardBody() in the ui.R file. For more information on the arguments of this function, type ?infoBox into the console.

Although you may sometimes wish to hardcode info boxes in this way (to show version numbers of an application, as in this case), in the majority of cases, you are going to produce this content dynamically. In this case, you will as always need to do some preparation on server.R first. Here is the code for the first info box:

output$infoYears <- renderInfoBox({
infoBox(
"Years", input$year[2] - input$year[1],
icon = icon("calendar", lib = "font-awesome"),
color = "blue",
fill = ifelse(input$year[2] < 2007,
TRUE, FALSE)
)
})

The first icon is the number of days within the specified range. The first argument gives the icon a title, Years, and the second gives it a value (the number of years, calculated by subtracting the first year from the second). You can also select the color of the box and whether the right-hand portion (which contains the text, as opposed to the icon) is filled (a solid color) or not.

As you can see, here we are deciding the fill of the value portion of the icon dynamically. When the largest year in the range is lower than 2007, the icon will be filled. If it isn't, it won't. See ?ifelse for help with ifelse().

The other dynamic info boxes are set up in the same way, as follows:

output$infoLifeExp <- renderInfoBox({

infoLifeExp = theData() %>%
filter(year == 2007) %>%
group_by(continent) %>%
filter(continent == input$continent) %>%
pull(lifeExp) %>%
mean() %>%
round(0)

infoBox(
"Life Exp. (2007)", infoLifeExp,
icon = icon("user"),
color = "purple",
fill = ifelse(infoLifeExp > 60,
TRUE, FALSE)
)
})

output$infoGdpPercap <- renderInfoBox({

infoGDP = theData() %>%
filter(year == 2007) %>%
group_by(continent) %>%
filter(continent == input$continent) %>%
pull(gdpPercap) %>%
mean() %>%
round(0)

infoBox("GDP per capita",
infoGDP,
icon = icon("usd"),
color = "green",
fill = ifelse(infoGDP > 5000,
TRUE, FALSE)
)
})
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset