首页 > 解决方案 > 在 R Shiny App 中连接 UI 和 SERVER 端的对象

问题描述

我试图了解 UI 和 SERVER 端如何在 R Shiny App 中连接。这是我在服务器端的代码。这是我在处理矩阵/向量数据时通常会做的事情——我知道这可能不是最好的方法,但它过去对我有用。

我现在正在处理空间数据,并且正在努力了解如何将用户选择的输入连接到我在服务器端创建的对象。

服务器端:

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)
      }
    }  
  })

})

用户界面:

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")
    )
  )
))

运行此代码时,给出的错误是比较 (if(selected_dataset == "Oklahoma")) 对此数据类型无效。在此示例代码中,选定的用户输入将是 Oklahoma,但我相信我没有正确调用该对象。

如何根据用户输入输出俄克拉荷马州的地图?或者更广泛地说,ui.R 和 server.R 如何通信用户输入和输出?

标签: rwebweb-applicationsshinyspatial

解决方案


推荐阅读