inst/shiny-app/guessex/app.R

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(ggplot2)
library(gridExtra)
library(maps)
library(PBSmapping)
library(readr)
library(dplyr)

ui <- fluidPage(title = "LPJ-GUESS @ Amazon",
                fluidRow(
                  column(6,
                         fileInput("file1", "Upload *.out file", accept = "out")),
                  column(6,
                         fileInput("file2", "Upload *.out file", accept = "out"))
                ),
                fluidRow(
                  column(6,
                         uiOutput('filepath1')),
                  column(6,
                         uiOutput('filepath2'))
                ),
                fluidRow(
                  column(6,
                         plotOutput("map1")),
                  column(6,
                         plotOutput("map2"))),
                fluidRow(
                  column(6,
                         uiOutput("select1")),
                  column(6,
                         uiOutput("select2"))
                ))

server <- function(input, output) {

  options(shiny.maxRequestSize = 500*1024^2)

  file1 <- reactive({
    file1 <- input$file1
    if (is.null(file1)) { return() }
    read_table(file = file1$datapath)
  })

  file2 <- reactive({
    file2 <- input$file2
    if (is.null(file2)) { return() }
    read_table(file = file2$datapath)
  })

  output$select1 <- renderUI({
    selectInput("param1", "Parameter", choices = names(file1()[-c(1:3)]))
  })

  output$select2 <- renderUI({
    selectInput("param2", "Parameter", choices = names(file2()[-c(1:3)]))
  })

  output$map1 <- renderPlot({

    if (!is.null(file1())) {

      data1 <- file1() %>%
        group_by(Lon, Lat) %>%
        summarise_each(funs(mean))
      
      xlim <- range(data1$Lon) + c(-1, 1)
      ylim <- range(data1$Lat) + c(-1, 1)
      
      world <- map_data("world")
      data.table::setnames(world, c("X","Y","PID","POS","region","subregion"))
      world <- clipPolys(world, xlim = xlim, ylim = ylim, keepExtra = TRUE)

      p1 <- ggplot(data1, aes_string(x = "Lon", y = "Lat", fill = input$param1)) +
        geom_tile() +
        coord_quickmap() +
        geom_polygon(data = world, aes(x = X,  y = Y, group = PID),
                     colour = "black", fill = "white", alpha = 0) +
        theme_minimal() +
        scale_fill_gradient2(low = "blue", mid = "yellow", high = "red")

      p1
    }
  })

  output$map2 <- renderPlot({

    if (!is.null(file2())) {
      
      data2 <- file2() %>%
        group_by(Lon, Lat) %>%
        summarise_each(funs(mean))
      
      xlim <- range(data2$Lon) + c(-1, 1)
      ylim <- range(data2$Lat) + c(-1, 1)
      
      world <- map_data("world")
      data.table::setnames(world, c("X","Y","PID","POS","region","subregion"))
      world <- clipPolys(world, xlim = xlim, ylim = ylim, keepExtra = TRUE)

      p2 <- ggplot(data2, aes_string(x = "Lon", y = "Lat", fill = input$param2)) +
        geom_tile() +
        coord_quickmap() +
        geom_polygon(data = world, aes(x = X,  y = Y, group = PID),
                     colour = "black", fill = "white", alpha = 0) +
        theme_minimal() +
        scale_fill_gradient2(low = "blue", mid = "yellow", high = "red")

      p2
    }
  })
}

shinyApp(ui = ui, server = server)
cszang/guessex documentation built on May 14, 2019, 12:26 p.m.