'Sidebar tabs not recognising tabname argument
I am trying to create a shiny dashboard with three tabs. The first tab is supposed to include a leaflet map, I have then tried to add a second tab to include a page for a dataframe. However, the dataframe and its title is being added to the bottom of the leaflet map page, despite specifying a different tab name.
I am new to shiny so any help is appreciated.
code specific to the question is as follows:
library(shiny)
library(leaflet)
library(RColorBrewer)
library(shinythemes)
library(dplyr)
library(shinydashboard)
library(rgdal)
library(htmltools)
library(ggplot2)
library(DT)
#### UI #############################################################
### header ###
header <- dashboardHeader(
title = "World Happiness Dashboard",
titleWidth = 400
)
### Body content ###
body <- dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "Map",
fluidRow(
column(
width = 12,
box(
title= "Global Levels of Happiness, GDP, Population Density and Covid Stringency",
solidHeader = TRUE,
width = NULL, height = 530,
leafletOutput("map",height=470),
status='primary')
)
)
)
),
# Second tab content
tabItem(tabName = 'data',
fluidPage(
h1('Data Table'),
dataTableOutput("maptable")
))
)
sidebar <- dashboardSidebar(
width = 180,
sidebarMenu(
menuItem("Map", tabName = "Map", icon = icon("globe"), badgeLabel = "Start Here!", badgeColor = "green"),
menuItem("Data", icon = icon("list", lib = "glyphicon"), tabName = "data")
)
)
### ui ###
ui <- dashboardPage(
title = "Happiness Data Visualisation",
skin = 'blue',
header,
sidebar,
body
)
#### Server ###################
server <- function(input, output, session) {
#### output ####
## output: leaflet map
output$map <- renderLeaflet({
map <- worldCountries %>%
leaflet() %>%
addTiles() %>%
setView( lat=10, lng=0 , zoom=2) %>% #sets default map pan
addPolygons(### happiness #########
data = worldCountries,
fillColor = ~mypalettewhi(happiness_score),
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = "World Happiness Index"
) %>%
addPolygons(### GDP #################
data = worldCountries,
fillColor = ~mypalettegdp(gdp),
stroke=TRUE,
fillOpacity = 0.9,
color="black", #black is added as highlight as yellows will blend
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlightgdp,
group = "GDP"
) %>%
addPolygons(### population #################
data = worldCountries,
fillColor = ~mypalettepop(pop_density_log), #log is used to diminish extreme values
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = 'Population Density'
) %>%
addPolygons( ### covid stingency ##############
data = worldCountries,
fillColor = ~mypalettecovid(avg_covid_score),
stroke=TRUE,
fillOpacity = 0.9,
color="white",
weight=0.7,
label = mytext,
labelOptions = label,
highlightOptions = highlight,
group = "Covid Stringency Score"
) %>%
addLayersControl( #controls layers
baseGroups = c("World Happiness Index", "GDP", "Population Density", "Covid Stringency Score"), #base groups indicates these will be toggled between groups
options = layersControlOptions(collapsed = FALSE)
) %>%
addLegend( #happiness legend
values=~happiness_score,
opacity=0.9,
title = "World Happiness<br /> Index Score",
position = "bottomleft",
colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
labels = c("Less Happy", "", "", "", "", "", "More Happy"),
group = "World Happiness Index" #group it belongs to
) %>%
hideGroup(c('World Happiness Index','GDP', 'Population Density', 'Covid Stringency Score')) %>%
showGroup('World Happiness Index')
})
#### update legend when the selected layer group changes ######################
observeEvent(input$map_groups, {
my_map <- leafletProxy("map") %>% clearControls()
if (input$map_groups == 'World Happiness Index'){#### Happiness Legend ####
my_map <- my_map %>%
addLegend(
opacity=0.9,
title = "World Happiness<br /> Index Score",
position = "bottomleft",
colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
labels = c("Less Happy", "", "", "", "", "", "More Happy"),
values = worldCountries$happiness_score
)
}else if (input$map_groups == 'GDP'){ #### GDP Legend ####
my_map <- my_map %>%
addLegend(
opacity=0.9,
title = "GDP (US$)",
position = "bottomleft",
colors = c('#ffffd3', '#d9f0a3', '#addd8e', '#7bce7c', '#41ab5d', '#238443', '#005a32'),
labels = c("Lower GDP", "", "", "", "", "", "Greater GDP"),
values = worldCountries$gdp
)
}else if (input$map_groups == 'Population Density'){
my_map <- my_map %>%
addLegend( #### pop density legend ####
opacity=0.9,
title = "Population Density<br /> (per km\u00B2)",
position = "bottomleft",
colors = c('#f2f0f7', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#4a1486'),
labels = c("Less Dense", "", "", "", "", "", "More Dense"),
values = worldCountries$pop_density_log
)
}else{
my_map <- my_map %>%
addLegend( #code for covid legend
opacity=0.9,
title = "Covid Stringency Score",
position = "bottomleft",
colors = c('#eff3ff', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#084594'),
labels = c("Less Stringent ", "", "", "", "", "", "More Stringent"),
values = worldCountries$avg_covid_score
)
}
})
###### add data table ##################
output$maptable <- renderDataTable(cleantable)
}
#### Run App ####
shinyApp(ui = ui, server = server)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|