Created
March 14, 2017 06:10
-
-
Save kkweon/3df2407884bd815427ce9babed301068 to your computer and use it in GitHub Desktop.
Top 포탈 키워드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(gsheet) | |
# Helper Functions | |
read.list <- function(url = "https://docs.google.com/spreadsheets/d/1aJ2Bv8CCR4OhBoVdsQD16OWON89VwuaLYKDDP-OiTG4") { | |
as.data.frame(gsheet::gsheet2tbl(url)) | |
} | |
read.data <- function(url) { | |
data <- as.data.frame(gsheet::gsheet2tbl(url)) | |
if (nrow(data) > 0) { | |
if (!('keyword' %in% colnames(data))) { | |
data <- rbind(colnames(data), data) | |
colnames(data) <- c("datetime", 'source', 'rank', 'keyword') | |
} | |
data | |
} | |
} | |
get.url <- function(name) { | |
tmp <- read.list() | |
subset(tmp, filename == name)$url | |
} | |
# Define UI | |
ui <- fluidPage( | |
# Title | |
titlePanel("포탈3사 검색어 데이터"), | |
# Layout | |
sidebarLayout( | |
# Sidebar | |
sidebarPanel( | |
selectInput("dataset", "데이터셋", choices = read.list()$filename) | |
), | |
# Main Panel | |
mainPanel( | |
tabsetPanel( | |
tabPanel("Top Keyword", dataTableOutput('keyword')), | |
tabPanel("Raw", dataTableOutput('raw')), | |
tabPanel("Download Data", downloadButton("download", label = "download")) | |
) | |
) | |
) | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
dataset <- reactive({ | |
dataurl <- get.url(input$dataset) | |
read.data(dataurl) | |
}) | |
output$keyword <- renderDataTable({ | |
# Tab: Top Keword | |
data <- dataset() | |
if (length(data) > 0) { | |
counts <- sort(table(data$keyword), decreasing = TRUE)[1:20] | |
keywords <- enc2utf8(names(counts)) | |
data.frame(keyword=keywords, count=as.integer(counts)) | |
} | |
}) | |
output$raw <- renderDataTable({ | |
# Tab: Raw Data | |
dataset() | |
}) | |
output$download <- downloadHandler( | |
# Tab: Download Handler | |
filename = function() { paste0(input$dataset, ".csv") }, | |
content = function(file) { | |
write.csv(dataset(), file, fileEncoding = 'utf-8') | |
} | |
) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment