Shiny와 leaflet으로 웹 애플리케이션

14403 단어 RleafletShiny
library(shiny)
library(dplyr)
library(lubridate)
library(stringr)
library(leaflet)
library(ggplot2)
library(plotly)

# データの定義
X <- read.csv(file="yockney-et-al-data-for-poned1439135.csv")
head(X)
X <- X %>%
  mutate(NZDT_datetime=dmy_hm(NZDT_datetime)) %>%
  arrange(NZDT_datetime) %>%
  mutate(Latitude=if_else(Latitude>0, -1*Latitude, Latitude)) %>%
  mutate(Animal.code=str_to_upper(Animal.code), Season=str_to_upper(Season))
head(X)
X$popup <- paste("HDOP=", X$HDOP, sep="")
Animal.code <- unique(X$Animal.code) 

# UIの定義
ui <- fluidPage(
  a(titlePanel("possum-foraging"), href="https://data.world/nz-govt/possum-foraging"),
  sidebarLayout(
    sidebarPanel=sidebarPanel(
      selectInput(inputId="Animal.code", label="Animal.code", choices=Animal.code, selected=Animal.code[1])
    ),
    mainPanel=mainPanel(
      leafletOutput(outputId="leaflet"),
      plotlyOutput(outputId="histogram"),
      plotlyOutput(outputId="linepoint")
    )
  )
)

# サーバの定義
server <- function(input, output, session){
  output$leaflet <- renderLeaflet({
    x <- X %>% filter(Animal.code==input$Animal.code)
    leaflet() %>% addTiles() %>%
      addPolylines(lng=x$Longitude, lat=x$Latitude, weight=1, popup=input$Animal.code, color="blue") %>%
      addCircles(lng=x$Longitude, lat=x$Latitude, radius=0.5, popup=x$popup, color="blue")
  })
  output$histogram <- renderPlotly({
    x <- X %>% filter(Animal.code==input$Animal.code)
    p <- ggplot(data=x, mapping=aes(x$HDOP)) + geom_histogram(fill="blue") + xlab(label="HDOP")
    ggplotly(p)
  })
  output$linepoint <- renderPlotly({
    x <- X %>% filter(Animal.code==input$Animal.code)
    p <- ggplot(x, aes(x=NZDT_datetime, y=HDOP)) + geom_line(color="blue") + geom_point(color="blue")
    ggplotly(p)
  })
}

# アプリケーションの起動
shinyApp(ui=ui, server=server)



좋은 웹페이지 즐겨찾기