To remind you of the server.R file, and to show you which bits we have retained, let's look at the server.R file first, shown in the following code:
library(tidyverse)
library(gapminder)
load("geocodedData.Rdata")
function(input, output) {
theData = reactive({
mapData %>%
filter(year >= input$year[1])
})
output$trend = renderPlot({
thePlot = theData() %>%
group_by(continent, year) %>%
summarise(meanLife = mean(lifeExp)) %>%
ggplot(aes(x = year, y = meanLife, group = continent,
colour = continent)) + geom_line() +
ggtitle("Graph to show life expectancy by continent over time")
if(input$linear){
thePlot = thePlot + geom_smooth(method = "lm")
}
print(thePlot)
})
}
There are no surprises in this code, really; it is just a cut-down version of the code from the previous chapter. You can see a reactive function to bring back the data (theData()), and a call to renderPlot(), which produces a line graph using ggplot().