Shinyアプリデプロイしました

NBA Team Record ←ようやくできました。
とりあえず最初なので、できはこのくらいでもお許し下さい。

ちょっと頑張ったことについて、技術的に挑戦したことを書いてみたいと思います。
データ呼び起こし編
データを参照にしたのは下のリンクからです。
www.basketball-reference.com

library(shiny)
library(ggplot2)
library(DT)
library(tidyverse)
library(rvest)
library(magrittr)

自分でも思いますが、本当に、使うパッケージが王道ものしか使っていないなぁと...
これからの当分の目標はtidyverseあたりをさらにマスターして新たなことにどんどん挑戦していきたいです。

NBA_team_datasets <- read_html("https://www.basketball-reference.com/teams/")
nt <- 
  NBA_team_datasets %>% 
  html_table(header = TRUE) %>% 
  extract2(1) %>% 
  filter(To == 2018) %>% 
  distinct(Franchise, .keep_all=TRUE)

今回挑戦したのは、スクレイピングに挑戦したことです。感想は思っていたよりもとてもハードルが低くやりやすかったので今後ともどしどし使っていきたい。

UI編
特にひねったことはしてないです。

    sidebarPanel(
      selectInput(inputId = "y", 
                  label = "Y-axis:",
                  choices = c("Titles"            = "Champ", 
                              "Plyoffs made"      = "Plyfs", 
                              "Win %"              = "PCT", 
                              "Divsion Titles"    = "Div",
                              "Conference Titles" = "Conf",
                              "Years"             = "Yrs"), 
                  selected = "Champ")

sidebarなど次は凝ったことをしてみたいと思います。

    mainPanel(
      tabsetPanel(type ="tabs",
                  tabPanel(
                    title = "Plot",
                    plotOutput(outputId = "scatterplot")
                  ),
                  tabPanel(
                    title = "Glossary",
                    tags$div(
                      tags$li("Lg -- League"),
                      tags$li("From -- First year"),
                      tags$li("To -- Last year"),
                      tags$li("G -- Games"),
                      tags$li("W -- Wins"),
                      tags$li("L -- Losses"),
                      tags$li("PCT -- Win-Loss Percentage"),
                      tags$li(" Plyfs -- Years team made the playoffs"),
                      tags$li("Div -- Years team finished first (or tied for first) in the division"),
                      tags$li("Conf -- Years team won the conference championship"),
                      tags$li("Champ -- Years team won the league championship")
                    )
                  ),
                  DT::dataTableOutput(outputId = "teamtable")
      )

美しくないできでね...でも今回の目標はあくまでも見た目重視というより、「Shinyでとりあえずアプリをつくる」ということだったので良しとしましょう。
サーバー編

function(input, output) {
    output$scatterplot <- renderPlot({
      nt$Franchise<- reorder(nt$Franchise, -nt[,input$y])
      
      nt %>% 
        ggplot(aes_string(x = nt$Franchise, y = input$y)) + 
        geom_point(colour="red", size=2) +
        theme(axis.text.x = element_text(size = 13,angle = 70, hjust = 1)) + 
        geom_segment(aes(xend=nt$Franchise, yend=0), colour = "pink", alpha =1.0) + 
        labs(x=NULL)
    })
    
    output$teamtable <- DT::renderDataTable({
      DT::datatable(data = nt, 
                    rownames = FALSE,
                    options = list(
                      lengthMenu = list(c(5,10,15,-1), c("5","10","15","ALL")),
                      pagelength = 5)
      )
    })
  }

結構ggplot2の使い方は理解してきたなぁと思いますが、今後はより気持ちのよい動かし方がしたい。

とまぁかなりはしょりましたが大枠はこんな感じでした。

f:id:kokiando:20180218105026g:plain
http://g.recordit.co/NQLhmHKqYf.gif

感想
今までと違い、自分の作ったものがローカル環境以外でも動いているのをみるとなんだか形容し難い嬉しさ見たいのが感じられました笑
大学の新学期がそろそろ始まろうとしていますが、その場でもじゃんじゃんRを活用していけるように、今のうちにインプットとアウトプットをしっかりしていいきたいと思います。