shinyで簡単なwebアプリを作ってshinyapp.ioに公開してみた
shinyの練習をしました。以下の記事で作成したコードを流用して、google trendsのdailyの結果をcsv形式で取得するウェブアプリを作ります。
具体的には以下のことができるようにしました。
- 検索ワードを指定
- 期間を指定
- 実行ボタンを押すと、条件に沿ったデータを取得
- 取得したデータをグラフで表示(確認用)
- ダウンロードボタンを押すとcsv形式でデータがダウンロードされる
参考記事
以下の記事が大いに参考になりました。(公式チュートリアルの日本語訳)
RStudio Shiny チュートリアル レッスン1 ようこそ Shiny へ - Qiita
app.Rファイルの作成
# import packages --------------------------------------------------------- library(gtrendsR) library(shiny) library(tidyverse) # ui ---------------------------------------------------------------------- ui <- fluidPage( # title titlePanel("Google Trends downloader"), # Sidebar with a slider input for number of bins sidebarLayout( sidebarPanel( # keyword textInput("keyword", label = h5("Keyword"), value = ""), # start date textInput("start", label = h5("Start date (yyyy-mm-01)"), value = ""), # end date textInput("end", label = h5("End date (yyyy-mm-01)"), value = ""), # action button actionButton("update", "Get data") ), # Show a plot of the generated distribution mainPanel( # plot line plotOutput("linePlot"), # Download Button downloadButton("downloadData", "Download") ) ) ) # server ------------------------------------------------------------------ server <- function(input, output) { df <- eventReactive(input$update, { # prepare arguments for grends() function start_date <- seq(as.Date(input$start), as.Date(input$end), by = "month") end_date <- seq(as.Date(input$start), length = length(start_date), by = "month") period <- cbind(data.frame(s = start_date[-length(start_date)]), data.frame(e = end_date[-1])) %>% mutate(period = str_c(s, " ", e)) %>% select(period) # get google trends data trends <- lapply(period$period, gtrends, keyword = input$keyword, geo = "JP") # extract index data trends_df <- trends %>% map("interest_over_time") %>% bind_rows() %>% mutate(weight_temp = coalesce(ifelse(date == lag(date, 1), lag(hits, 1) / hits , 1), 1), weight = cumprod(weight_temp), hits_adj = hits * weight) %>% distinct(date, .keep_all = TRUE) %>% select(date, hits_adj) # output trends_df }, ignoreNULL = FALSE) output$linePlot <- renderPlot({ req(input$update) df() %>% mutate(date = as.Date(date)) %>% ggplot(aes(x = date, y = hits_adj)) + geom_line() + scale_x_date(date_labels = "%Y%m%d") }) output$downloadData <- downloadHandler( filename = function() { paste(input$keyword, ".csv", sep = "") }, content = function(file) { write_csv(df(), file) } ) } # run --------------------------------------------------------------------- shinyApp(ui = ui, server = server)
アプリの実行
まず、Rstudio上で"Run app"を押してアプリを実行します。
すると、別ウィンドウにアプリが表示されます。
検索ワードと期間をしていして"Get data"を押します。
するとgoogle trendsからデータが取得され、グラフが描画されます。
最後にDownloadボタンを押すと、csvファイルがダウンロードされます。
デプロイ
今回はshinyapp.ioにデプロイすることにしました。
やり方はいくつかあるようですが、Rstudioから簡単にできました。
アプリを実行すると右上にpublishボタンがでてくるので押します。(直前の画像を参照)
押すとshinyapp.ioと繋げるためのダイアログボックスがでてきます。
(なお、事前にshinyapps.ioに登録してtokensを取得しておいてください。)
Nextを押します。
ShinyApps.ioを選択します。
shinyapps.ioのアカウントページで発行されるtokensのスニペットを貼り付けます。
アカウントが紐付きました。publishを押せばあとは自動的にデプロイしてくれます。
デプロイ後はwebで公開されるため、以下のように自由にアクセスできます。
Google Trends downloader
以上