在闪亮的server.R文件中设置数据

时间:2014-10-02 13:07:18

标签: r ggplot2 shiny

我正在尝试使用一个简单的Shiny应用程序(这将是更大的事情的开始)。我的问题是,应用程序的sever.R部分中的数据子集似乎对我来说根本不起作用。

错误消息是:

  

eval(expr,envir,enclos)中的错误:找不到对象'localA3'

这是我的server.R文件:

# server.R

library(ggmap)
library(scales)
library(grid)

localA <- read.csv("local7.csv", header=TRUE)

shinyServer(
  function(input, output) {

    output$map <- renderPlot({

      sub <- switch(input$sub, 
                    "a" = "a",
                    "b" = "b",
                    "c" = "c")


      opt <- switch(input$opt, 
                    "data" = "data",
                    "obs" = "obs")


      localx = reactive({
        x <- subset(localA, factor==paste(sub))
        return(x)
      })

      localA3 <- localx()

      testmapA <- qmap("England", zoom = 6, color = "bw", legend = "topleft")

      testmapA +
        geom_point(aes(x = lon, y = lat, colour = localA3$opt, size = localA3$opt),   data = localA3)

    })
  }
)

...这是我的UI.R文件:

# UI.R

shinyUI(fluidPage(
  titlePanel("A Shiny Example"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create maps with random data for UK local authorities."),

      selectInput("sub", 
                  label = "Choose a category to display",
                  choices = c("a", "b", "c"),
                  selected = "a"),

      selectInput("opt", 
                  label = "Choose a variable to display",
                  choices = c("data", "obs"),
                  selected = "data"),


      sliderInput("range", 
                  label = "Range of interest:",
                  min = 0, max = 100, value = c(0, 100))
    ),

    mainPanel(plotOutput("map"))
  )
))

无论出于何种原因,R告诉我它无法找到对象'localA3'。我试图用许多不同的方式编写代码的子集部分 - 从函数的反应式表达式到简单的R代码。在Shiny之外工作的东西在server.R程序中不起作用。

任何人都可以给我任何关于我可能做错的提示吗?

我的基础数据集'local7.csv'的前十个条目如下:

    lon         lat         data        obs         factor
1   -0.1277583  51.5073509  87.828234   20.49259318 a
2   0.1293497   51.5464828  68.79663358 98.0480588  a
3   -0.1997     51.6444     24.35460542 76.77994522 b
4   0.154327    51.439933   71.51349632 28.05491455 a
5   -0.2710568  51.5672808  91.31933313 69.15576621 c
6   0.013156    51.406025   57.98920169 56.12171479 a
7   -0.1588255  51.5517059  45.68928313 29.73514486 a
8   -0.098234   51.376165   29.47027315 96.1460748  b
9   -0.3415002  51.5250257  36.46005588 76.66948508 c
10  -0.0837     51.6516     43.57721438 50.65123884 c

1 个答案:

答案 0 :(得分:0)

您需要将aes来电更改为aes_string,并将所有元素字符串设置为与geom_point保持一致。在使用对象和字符串之前。你最后的小改动对我来说很有用。

testmapA +
          geom_point(aes_string(x = "lon", y = "lat", colour = input$opt, size = input$opt),   data = localA3)