官术网_书友最值得收藏!

Data processing

As you write more and more complex programs, it's the server.R file that will become the largest because this is where all the data processing and output goes, and is even where some of the functions that handle advanced UI features live. Let's look at the chunks in order and talk about the work carried out in each section.

The first chunk of code looks like the following:

library(tidyverse)
library(gapminder)
library(leaflet)
library(ggmap)

You can see the packages that we need being loaded at the top. We load the tidyverse for access to things such as dplyr and ggplot, for munging data and plotting, respectively. The gapminder package is used to load a subset of the gapminder data. It has been very kindly munged and placed online in the form of a CRAN package by Jenny Bryan. We will talk more about the data that it makes available later. The leaflet package—which, as we mentioned before, is used for drawing maps—is loaded next. Finally, the ggmap package is loaded. This will be used to convert country names to latitude and longitude for plotting by leaflet. 

The next thing that we do is munge or, if we already munged it, load the data, as shown in the following code:

if(!file.exists("geocodedData.Rdata")){

mapData = gapminder %>%
mutate(country2 = as.character(country)) %>%
group_by(country) %>%
slice(1) %>%
mutate_geocode(country2, source = "dsk") %>%
select(-country2)

mapData = left_join(gapminder, mapData) %>%
group_by(country) %>%
fill(lon) %>%
fill(lat)

save(mapData, file = "geocodedData.Rdata")

} else {

load("geocodedData.Rdata")
}

I'm not going to distract from Shiny by talking too much about this code. Essentially, it checks to see whether the data is already in the relevant directory (which it will be after you've run it once). If it is, it just loads the data. If not, it prepares it by producing a smaller dataset with one instance of each country and geocoding it (that is, turning it into latitude and longitude), then combining it with the whole dataset (to put all the years for each country back in), and filling in the resulting missing geocodings using the incredibly useful fill() function of dplyr. Having done all that, it saves the data for next time (since querying the API for all 142 countries takes quite a long time). Don't worry too much if you can't follow this code at the moment; it's really there just to get the data onto your computer in a simple way.

It's worth noting that data instructions—and indeed any other code—that appear above function(input, output) {} are executed once when the application starts and then serve all instances of the Shiny application. This makes no difference when you're developing on a local machine, since the code will start fresh every time you run it, but once you move your code to a server, it can make a big difference, since the code is only run once for all the users and application instances you have (until you restart the Shiny server, which manages connections to your applications on a server, of course). Therefore, any intensive data or processing calls are best kept in this section to avoid your users having to wait a long time for their application to load.

主站蜘蛛池模板: 馆陶县| 鄯善县| 衡阳县| 通化县| 延津县| 镇江市| 伊金霍洛旗| 故城县| 蛟河市| 平昌县| 墨竹工卡县| 上杭县| 涡阳县| 上杭县| 满洲里市| 十堰市| 康定县| 益阳市| 东山县| 桐庐县| 青岛市| 陇西县| 五河县| 南和县| 大埔县| 东宁县| 毕节市| 山阳县| 京山县| 临江市| 西昌市| 株洲市| 阳东县| 青海省| 察雅县| 札达县| 青海省| 贺兰县| 高雄县| 福鼎市| 乌恰县|