r - 在 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 如何通信用户输入和输出?
解决方案
推荐阅读
- javascript - 在jquery中添加和删除div不起作用
- kaggle - 使用 Julia 创建内核
- node.js - Mongoose 中的批量更新
- html - 带有标签的 CSS 开关切换转换
- python - Tensorflow:获得预测
- javascript - 如何从 wordpress 上的 .json 文件中提取信息
- javascript - 尝试使用 JavaScript 触发 ASP.NET C# 控制器方法会插入“未定义”作为参数
- javascript - ui.table.Table 在滚动时不会重新渲染行
- javascript - 使用 lodash 获取重复值索引
- javascript - 从 div 的中心开始滚动