vendredi 4 mai 2018

Connecting an object in the UI and SERVER sides in an R Shiny App

I am trying to understand how the UI and SERVER sides are connected in an R Shiny App. Here is my code on the Server side. This is what I have typically done when working with matrix/vector data - I know its probably not the best way to do it, but it has worked for me in the past.

I am working with spatial data now and am struggling to understand how to to connect the user selected input to the object that I have created on the server side.

SERVER SIDE:

library(shiny)
library(raster)
library(tiff)
library(rgdal)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  Oklahoma <- brick("Oklahoma.tif")
  Counties <- brick("Counties.tif")
  "Counties" <- Counties
  "Oklahoma" <- Oklahoma
  output$distPlot <- renderPlot({
    {
      Selected_Dataset <- get(input$data)
      if(Selected_Dataset == "Oklahoma")
      {
    plotRGB(Oklahoma)
      }
      if(Selected_Dataset == "Counties"){
        plotRGB(Counties)
      }
    }  
  })

})

UI SIDE:

library(shiny)

shinyUI(fluidPage(

  # Application title
  h3("Oklahoma map"),

  sidebarLayout(




sidebarPanel(tags$head(tags$style("#distPlot{height:85vh !important;}")),
                 selectInput("data","Select what you would like to see", c(




                   "Oklahoma",
                   "Counties",
                  "Livestock Incident",
                  "Land Usagetype",
                   "Sandhills"),
                   selected = "Oklahoma")
                 ,

                 uiOutput("ui"),

                 h6("This application allows users to visualize data about Oklahoma", 
                    a("(CVA)", href ="http://www.Oklahoma.org/", target = "_blank"))

    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      h6("For additional information, contact Oklahoma", a("here", href = "http://www.Oklahoma.org/", target = "_blank")),
      h6("Application created by", a("Ricky Bobby", href = "https://www.linkedin.com", target = "_blank"), " - 2018")
    )
  )
))

Aucun commentaire:

Enregistrer un commentaire