オープンデータとプログラミング

Shinyチュートリアル(レッスン5) Bootstrapウィジェットを追加する


今回から3回にわけて国勢調査のデータを使ったインタラクティブなWebアプリケーションを作成していきます。

なお、この記事は、
http://shiny.rstudio.com/tutorial/lesson5/をもとに作成しています。

1
2
3
4
5
# server.R
 
shinyServer(
  function(input, output) {}
)
1
2
3
4
5
6
7
# ui.R
 
shinyUI(pageWithSidebar(
  headerPanel("censusVis"),
  sidebarPanel(),
  mainPanel()
))

counties.RDS

アメリカの人口統計データをダウンロードします。
rstudioのWebサイトからダウンロードすることができます。

http://www.rstudio.com/shiny/lessons/Lesson-3/data/counties.RDS

ダウンロードしたファイルはプロジェクトディレクトリの直下に”data”ディレクトリを作成して格納します

dir

helpers.R

コロプレスマップ(choropleth maps)を作成するためのRスクリプトをダウンロードします。
shiny学習用に作成されたRスクリプトがrstudioのWebサイトにあるので、これをダウンロードします。

http://shiny.rstudio.com/tutorial/lesson5/census-app/helpers.R

ui.R、server.Rと同じディレクトリに格納しておきます。

helpers.Rは、maps, mapprojパッケージを使っていようなので、
インストールしていない場合はインストールします。

1
2
install.packages("maps")
install.packages("mapproj")

コロプレスマップを表示してみます。
RStudioのconsoleから次のプログラムを順に実行します。

1
2
3
source("helpers.R")
counties <- readRDS("data/counties.RDS")
percent_map(counties$white, "darkgreen", "% white")

RStudioの画面には、こんな感じで表示されます。

map_sample

Comments are closed.