시각화 중 반응형으로 만들기 위한 plotly와 웹에 출력하기 위한 shiny를 연습했다.

기초적인 내용으로 꽤 쉽게 할 수 있었다.

Shiny는 기존의 rJava, Rserve, Rsession보다 훨씬 쉬운 방식이었다.

Shiny를 제대로 사용하기 위해서 나중에 Rstudio를 서버로 사용해보고 싶은 생각도 들었다.



=========================== R ===========================

rm(list=ls())


install.packages("plotly")

library("ggplot2")

library("plotly")

install.packages("devtools")

install.packages("dygraphs")

library("dygraphs")


#ggplot2의 mpg데이터를 가져온다

mpg <- ggplot2::mpg

#mpg데이터에 x에 displ, y에 hwy 각 점을 구분하기 위해 drv를 사용했다.

#산점도를 그리기 위해 geom_point()를 해준다.

p <- ggplot(data = mpg, aes(x = displ, y = hwy, col = drv)) + geom_point()

#마우스에 반응하도록 plotly로 p를 불러온다.

ggplotly(p)


#economics 데이터를 가져온다.

eco <- ggplot2::economics

#시계열 데이터로 바꿔준다. 앞이 데이터 뒤가 어떤 값을 기준으로 정렬할 것이가를 결정

eco <- xts(economics$unemploy, order.by = economics$date)

#구조 확인

class(eco)

#그래프 및 반응형 viewer에서 출력

dygraph(eco)

#범위 selector를 아래에 추가해서 원하는 구간만 크게 볼 수 있게 한다.

dygraph(eco) %>% dyRangeSelector()



rm(list=ls())


library("zoo")

library("xts")

library("dplyr")

library("stringr")

library("lubridate")

library("KoNLP")

library("RColorBrewer")

library("tidyverse")

library("httpuv")

library("shiny")

library("ggplot2")

library("plotly")

library("dygraphs")


options(shiny.port = 7776)

options(shiny.host = "127.0.0.1")


ui <- fluidPage(

  

  plotlyOutput("plotly", width = 400, height = 500)

  

)


server <- function(input, output, session){

 output$plotly <- renderPlotly(

   {

     mpg <- ggplot2::mpg

     ggplot(data = mpg, aes(x = displ, y = hwy, col=drv)) +geom_point()

     

     

   }) 

}




shinyApp(ui = ui, server = server)


=========================== R ===========================

'프로그래밍 > Python, R 프로그래밍' 카테고리의 다른 글

[Python] Python 11일차  (1) 2018.05.29
[Python] Python 10일차  (0) 2018.05.28
[Python] 9일차  (0) 2018.05.23
[R] R 9일차  (0) 2018.05.23
[Python] Python 8일차  (0) 2018.05.21

+ Recent posts