【问题标题】:Shiny - FILL value not passed to ggplot correctly in shiny server - error object 'input' not found闪亮 - 在闪亮的服务器中未正确传递给 ggplot 的 FILL 值 - 找不到错误对象“输入”
【发布时间】:2013-11-01 03:23:46
【问题描述】:

我正在尝试开发一个闪亮的应用程序,它将在浏览器上安装基于地图的热图,并允许您更改热图上显示的变量。该地图是具有 GIS 形状文件的地理区域,然后选择的变量在地图上显示为热图。不幸的是,我有一个问题,变量没有正确传递给ggplot(),我的地图失败了。下面的 server.r 代码作为一个直接的 R 脚本成功运行,没有问题,但是当适应 Shiny 时它会失败。

该问题发生在 server.r 中的以下 ggplot() 代码中:

myplot1 <- myplot1 + aes(long, lat, group = group, fill = input$var) + ...

我收到一个错误:

eval(expr, envir, enclos) 中的错误:找不到对象“输入”

这与fill = input$var 有关,它无法识别从ui.r 传递的input$varinput$var 是 ui.r 中选择的变量(var1、var2 等)以显示在热图上。这是我在代码中知道的唯一一个无法识别input$var 的实例。我在此行之前使用了print(str(input$var)),它清楚地保存了所需变量的名称。如果我将其硬编码(例如fill=var1),那么ggplot() 工作正常并且地图显示正确。

我还在ggplot() 中使用了environment = environment(),但这会产生另一个错误:

错误:提供给连续刻度的离散值

我的解释是它正在寻找由变量描述的数据框,但它得到的是单个值。

我觉得我缺少一些简单的东西——我应该声明或重新分配的东西。如果人们对此有任何见解、指导或反馈,我将不胜感激。非常感谢!!

# server.R

library(shiny)

library(maps)
library(mapdata)
library(sp)
library(maptools)
library(scales)
library(RColorBrewer)
library(ggplot2)
library(rgeos)
library(plyr)
library(reshape)
library(mapproj)
library(rgdal)
library(grid)
library(gridExtra)

setwd("C:/Shiny")


# Step 1 Read/loading the target shapefile
gregion = readOGR(dsn="C:/Shiny", layer="duid")

# Step 2 Get row numbers from .dbf / explicitly identifies attribute rows by the .dbf offset.
gregion@data$id = rownames(gregion@data)

# Step 3 Makes centroid (point layer) from polygon "FORTIFY"
gregion.points = fortify(gregion, region="id")

# Step 4 Reading in .csv which will be joined to .dbf using "MERGE"
mydata <- read.csv("c:/Shiny/dataset.txt")

# Step 5 Joins the points to their corresponding attributes and finalizes the data preparation 
gregion.df = join(gregion.points, gregion@data, by="id")

# Step 6 Merge makes an inner join of the shapefile's data frame and the .csv on a common item (usually the spatial key)
mygeomdata <- merge(gregion.df, mydata, by.x="UID", by.y="UID")


# Define server logic required to plot various variables as heatmap
# Step 7 Create map
shinyServer(function(input, output) {

  # Compute the forumla text in a reactive expression since it is 
  # shared by the output$caption and output$mapPlot expressions

  formulaText <- reactive({
    paste("Variable:", input$var)
  })

  # Return the formula text for printing as a caption
  output$caption <- renderText({
    formulaText()
  })


  output$mapPlot <- renderPlot({

    myplot1 <- ggplot(mygeomdata)
    myplot1 <- myplot1 + aes(long, lat, group = group, fill = input$var) + labs(x = "Easting", y = "Northing") + scale_fill_gradient(low = "ghostwhite", high = "steelblue")
    myplot1 <- myplot1 + geom_polygon()
    myplot1 <- myplot1 + coord_equal()

    print(myplot1)

  })

})

#ui.R

library(shiny)

shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Mapping"),

  # Sidebar with controls to select the variable to plot
  #

  sidebarPanel(
    selectInput("var", "Variable:",
                list("Variable 1" = "var1",
                     "Variable 2" = "var2"))
  ),

  # Show the caption and plot of the requested variable

  mainPanel(
    h3(textOutput("caption")),

    plotOutput("mapPlot")
  )
))

数据集的一个样本mydata &lt;- read.csv("c:/Shiny/dataset.txt") 是:

UID var1    var2    var3    var4    var5    var6    var7
1   0   0.001   0   0   0   0   0
2   0   0   0   0   1   0   0
3   0   0   0   0   0   0   0
4   0   0   0   0   1   0   0
5   0   0   0   0   1   0   0
6   0   0   0   0   1   0   0
7   0   0   0   0   0   0   0
8   0   0.004   0.026   0   0   0   0
9   0.499   0.014   0   0.499   1   0   0.033
10  0.573   0.002   0.015   0.573   1   0   0.427
11  1   0.003   0.01    1   1   0   0

mygeomdata 具有以下结构:

 $ UID       : int  1 1 1 1 1 1 1 1 1 1 ...
 $ long      : num  393121 392895 392895 392840 392839 ...
 $ lat       : num  5501404 5502275 5502275 5502489 5502494 ...
 $ order     : int  1 2 3 4 5 6 7 8 9 10 ...
 $ hole      : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ piece     : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
 $ group     : Factor w/ 5693 levels "0.1","1.1","10.1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ id        : chr  "0" "0" "0" "0" ...
 $ DUID      : Factor w/ 5656 levels "130023362","130023367",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ PC        : Factor w/ 3617 levels "0","ZZZ0A3","ZZZ0A4",..: 3271 3271 3271 3271 3271 3271 3271 3271 3271 3271 ...
 $ DUIDAREA  : num  21687 21687 21687 21687 21687 ...
 $ ELEV      : num  14.8 14.8 14.8 14.8 14.8 ...
 $ GroupUp   : int  2 2 2 2 2 2 2 2 2 2 ...
 $ GroupUpT  : Factor w/ 2 levels "A","B": 2 2 2 2 2 2 2 2 2 2 ...
 $ var1      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ var2      : num  0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 ...
 $ var3      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ var4      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ var5      : int  0 0 0 0 0 0 0 0 0 0 ...
 $ var6      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ var7      : num  0 0 0 0 0 0 0 0 0 0 ...

【问题讨论】:

    标签: r ggplot2 geospatial heatmap shiny


    【解决方案1】:

    即使您在尝试将其移植到 Shiny 时遇到此错误,这也与 ggplot 有关。简而言之,您正在使用aes 的局部变量和函数参数,这比最初看起来要复杂。

    请参阅this SO question 了解完整的讨论和几个不错的选择。

    要记住的一件事:对于 ggplot,如果您将所需的一切都保留为数据框的一部分,您会发现它要容易得多。

    agstudy's response in this question 解释发生了什么。

    希望对您有所帮助。

    【讨论】:

    • 谢谢拉姆 Narasimhan。您向我指出了一些非常有用的帖子,经过一番挖掘后,我能够找到解决方案。这是修改后的部分 - 请参阅下面的答案。
    【解决方案2】:

    谢谢拉姆·纳拉辛汉。您向我指出了一些非常有用的帖子,经过一番挖掘后,我能够找到解决方案。这是修改后的部分:

    environment<-environment() 
      myplot1 <- ggplot(mygeomdata, aes(long, lat, group = group, fill = get(input$var)), environment = environment) 
    

    上面的代码放在 renderPlot 调用之外。 非常感谢!

    【讨论】:

      【解决方案3】:

      其实有一个更简单的解决方案。像以前一样在 renderPlot() 函数中使用 ggplot(),但使用 aes_string() 而不是 aes():

      output$mapPlot <- renderPlot({
          ggplot(mygeomdata, aes_string(x=long, y=lat, group = group, fill = input$var)) + geom_point()
      })
      

      【讨论】:

        【解决方案4】:

        aes_string(x=input$x, y=input$y) 也可以。只要确保两个输入都被视为反应输入(例如input$)。 ggplot() 不允许其中一个轴为手动轴,而另一个轴为反应轴。

        【讨论】:

        • 这本身就是一个有效的答案。但是,它复制了 2 个月前的现有答案。
        猜你喜欢
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 2015-05-04
        • 2019-12-21
        • 2018-12-12
        • 2020-02-23
        相关资源
        最近更新 更多