I am trying to create a shiny app in r. However, when I am trying to plot the dygraph and the last graph under their relevant tabs, they are not coming up in the tabs panels like the map and the first graph. I cannot figure out why I have tried numerous things to get them to show. Thank you in advance.
UI code:
library(tidyverse)
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(shinyBS)
library(shinydashboard)
library(xts)
library(dygraphs)
library(countrycode)
library(rworldmap)
ncov <- structure(list(Date = c("2020-05-07", "2020-05-07", "2020-05-07",
"2020-05-07", "2020-05-07", "2020-05-07"), Country = c("Vietnam",
"West Bank and Gaza", "Western Sahara", "Yemen", "Zambia", "Zimbabwe"
), Confirmed = c(288L, 375L, 6L, 25L, 153L, 34L), Recovered = c(233L,
176L, 5L, 1L, 103L, 5L), Deaths = c(0L, 2L, 0L, 5L, 4L, 4L)), row.names = 20004:20009, class = "data.frame")
ui <- fluidPage(
#being able to select the country and get an individual graph
titlePanel("Corona Virus"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "Country",
label = "Country",
choices = ncov$Country
),
),
dashboardPage(skin = "purple",
#title of the dashboard
dashboardHeader(title = "CoronaVirus"),
dashboardSidebar(width = 250,
sidebarMenu(
id = "sidebarmenu",
menuItem("Main", tabName = "Main"),
menuItem("Map", tabName = "Map"),
menuItem("Other Diseases", tabName = "Other Diseases"),
menuItem("Whole World", tabName = "Whole World")
#dashboard body
)),
dashboardBody(
tags$head
(
tags$link(rel = "stylesheet", type = "text/css", href = "custom3.css")
),
#confirmed number of cases
bsPopover (id = "q1", title = "Confirmed",
trigger = "hover",
placement = "top",
options = list(container="body")),
#recovered number of cases
bsPopover (id = "q1", title = "Recovered",
trigger = "hover",
placement = "top",
options = list(container="body")),
#Dead number of cases
bsPopover (id = "q1", title = "Dead",
trigger = "hover",
placement = "top",
options = list(container="body")),
tabItems(
tabItem(tabName = "Main",
fluidRow(
infoBoxOutput("info1"), width=10,
infoBoxOutput("info2"), width=10,
infoBoxOutput("info3"), width=10),
#dygraph of the whole world
plotOutput("plot"),
mainPanel(
)),#end main Panel
tabItem(tabName = "Map",
plotOutput("map"),
mainPanel()),
tabItem(tabName = "Other Diseases",
plotOutput("plott"),
mainPanel()),
tabItem(tabName = "Whole World",
plotOutput("plott"),
mainPanel())
)#end tab Items
)#end dashboard body
)#end dashboard
)#end side bar layout``
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot <- renderPlot({
data <- ncov %>% dplyr::filter(Country == input$Country)
ggplot(data, aes(y = Deaths, x = Confirmed)) + geom_point()
})
output$plott <- renderPlot({
data <- ncov
ggplot(data, aes(y = Deaths, x = Confirmed)) + geom_point()
})
#info box 1
output$info1 <- renderInfoBox({
infoBox("Confirmed",sum(ncov$Confirmed,na.rm = TRUE),
fill = TRUE, color = "yellow", icon("arrow-up"))})
#info box 2
output$info2 <- renderInfoBox({
infoBox("Recovered",sum(ncov$Recovered,na.rm = TRUE),
fill = TRUE, color = "green", icon("arrow-up"))})
#info box 3S
output$info3 <- renderInfoBox({
infoBox("Dead",sum(ncov$Deaths,na.rm = TRUE),
fill = TRUE, color = "red", icon("arrow-up"))})
output$map <- renderPlot({
#map
ans <-ncov %>%
filter(Date=="2020-05-06")%>%
select("Confirmed")
count<-countrycode(factor(ncov$Country), origin = 'country.name', destination = 'iso3c')
# create data frame with iso3 country codes and number of visits
countriesvisited <- data.frame(countries =count, conf = ans)
# inspect data
visitedMap <- joinCountryData2Map(countriesvisited,
joinCode = "ISO3",
nameJoinColumn = "countries")
# get map
worldmap <- getMap(resolution = "coarse")
# plot worldmap
plot(worldmap, col = "lightgrey",
fill = T, border = "darkgray",
xlim = c(-180, 180), ylim = c(-90, 90),
bg = "aliceblue",
asp = 1, wrap=c(-180,180))
# def. map parameters, e.g. def. colors
temp <-mapCountryData(visitedMap,
nameColumnToPlot="Confirmed",
oceanCol = "azure2",
catMethod = "categorical",
missingCountryCol = gray(.8),
colourPalette = heat.colors(171),
addLegend = F,
mapTitle = "",
border = NA)
do.call(addMapLegendBoxes, c(temp,
x = "left",
title = "Corona Cases for the previous day",
horiz = FALSE,
bg = "transparent",
bty = "n"))
})
}#end server
# Run the application
shinyApp(ui, server)
Tab does not like spaces in the tab name. As soon as I took the spaces out, it started to work.