Created
          November 11, 2012 09:39 
        
      - 
      
 - 
        
Save haven-jeon/4054309 to your computer and use it in GitHub Desktop.  
    Korea Oil Price plot with Shiny
  
        
  
    
      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(ggplot2) | |
| library(reshape2) | |
| library(scales) | |
| #data load from dropbox | |
| load(url("http://dl.dropbox.com/u/8686172/oil_price.RData")) | |
| shinyServer(function(input, output) { | |
| output$oilPlot <- reactivePlot(function() { | |
| kinds <- c() | |
| if(input$p_gasolin == TRUE){ | |
| kinds <- c(kinds, "premium gasolin") | |
| } | |
| if(input$gasolin== TRUE){ | |
| kinds <- c(kinds, "gasolin") | |
| } | |
| if(input$diesel== TRUE){ | |
| kinds <- c(kinds, "diesel") | |
| } | |
| if(input$kerosene== TRUE){ | |
| kinds <- c(kinds, "kerosene") | |
| } | |
| if(length(kinds) == 0) stop(enc2utf8("최소 하나의 유종을 선택하세요!")) | |
| oil_price_tail <- tail(oil_price, input$obs) | |
| oil_reshpae <- melt(oil_price_tail, id.vars=c("date")) | |
| names(oil_reshpae) <- c("date", "kind", "price") | |
| oil_subset <- subset(oil_reshpae, kind %in% kinds ) | |
| oil_subset$kind <- factor(oil_subset$kind, levels=c("premium gasolin","gasolin", "diesel","kerosene"), | |
| labels=enc2utf8(c("고급휘발유", "보통휘발유", "경유", "등유"))) | |
| p <- ggplot(oil_subset, aes(date,price)) + geom_line(aes(colour=kind)) + | |
| scale_x_date(breaks = date_breaks("3 month")) + | |
| theme(axis.text.x=element_text(angle=45)) + scale_color_discrete(enc2utf8("유종")) + | |
| xlab(enc2utf8("날짜")) + ylab(enc2utf8("가격(원)")) | |
| print(p) | |
| }) | |
| }) | |
  
    
      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) | |
| # bugs on label on checkboxInput to display Korean | |
| # a<-enc2utf8("고급휘발유") | |
| # b<-enc2utf8("보통휘발유") | |
| # c<-enc2utf8("자동차경유") | |
| # d<-enc2utf8("실내등유") | |
| shinyUI(pageWithSidebar( | |
| headerPanel(enc2utf8("유가 시계열 그래프(2008.4.14 ~ 2012.11.08")), | |
| sidebarPanel( | |
| sliderInput("obs", | |
| enc2utf8("관측수(마지막 n개):"), | |
| min = 7, | |
| max = 1670, | |
| value = 670), | |
| wellPanel(p(strong(enc2utf8("유종"))), | |
| checkboxInput(inputId = "p_gasolin", label = "Premium Gasolin", value = FALSE), | |
| checkboxInput(inputId = "gasolin", label = "Gasolin", value = TRUE), | |
| checkboxInput(inputId = "diesel", label = "Diesel", value = TRUE), | |
| checkboxInput(inputId = "kerosene", label = "Kerosene", value = FALSE) | |
| )), | |
| mainPanel( | |
| plotOutput("oilPlot") | |
| ) | |
| )) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
bugs on label on checkboxInput to display Korean