2013-10-23 30 views
2

我想開發一個閃亮的應用程序,它將在瀏覽器上安裝基於地圖的熱圖,並允許您更改顯示的變量在熱圖上。地圖是具有GIS形狀文件的地理區域,然後選擇的變量作爲熱圖顯示在地圖上。不幸的是我有一個問題,那就是變量沒有正確傳遞給ggplot(),我的地圖失敗了。以下直連R腳本的server.r代碼可以順利運行,但在適應Shiny時會失敗。閃亮 - FILL值未正確傳遞給閃存服務器中的ggplot - 找不到錯誤對象「輸入」

的問題有以下ggplot()代碼發生在server.r:

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

我得到一個錯誤:

Error in eval(expr, envir, enclos) : object 'input' not found

這與fill = input$var做它不承認input$var這是從ui.r.傳遞input$var是ui.r中選定的變量(var1,var2等)以顯示在熱圖上。這是我知道的input$var未被識別的唯一例子。我在這行之前使用了print(str(input$var)),它清楚地保存了所需的變量名稱。如果我硬編碼它(例如fill=var1),然後ggplot()工作正常,地圖顯示正確。

我也用environment = environment()ggplot()但這產生另一個錯誤:

Error : Discrete value supplied to continuous scale

我解釋爲是指它在尋找由可變描述的數據幀,而是它得到一個單一的值。

我覺得這是簡單的東西我缺少 - 我應該聲明或重新分配的東西。我非常感謝任何見解,指導或反饋人士對此的評論。非常感謝 !!

# 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. 
[email protected]$id = rownames([email protected]) 

# 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, [email protected], 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 <- 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 ... 

回答

1

謝謝你Ram Narasimhan。你指給我一些非常有用的帖子,我能夠在一些挖掘到達解決方案後。這裏是修改部分:

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

上述代碼放置在renderPlot調用之外。 非常感謝!

2

即使您收到此錯誤作爲你嘗試端口的一部分它閃亮,這與ggplot有關。簡而言之,您使用aes的局部變量和函數參數,這比第一次看起來更復雜。

請參閱this SO question進行全面的討論和幾個很好的選擇。

有一點需要記住:對於ggplot,如果您將所需的所有內容都保存爲數據框的一部分,您會發現它更容易。

agstudy's response in this question解釋發生了什麼。

希望有所幫助。

+0

謝謝你Ram Narasimhan。你指給我一些非常有用的帖子,我能夠在一些挖掘到達解決方案後。這裏是修改的部分 - 請參閱下面的答案。 – user2892709

1

其實,有一個更簡單的解決方案。使用ggplot()的renderPlot()函數中像之前,但使用aes_string()而不是AES():

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

aes_string(x=input$x, y=input$y)也適用。只要確保兩個輸入都被視爲反應輸入(例如input$)。 ggplot()不允許其中一個軸是手動的,另一個是反應性的。

+0

這是一個有效的答案。但是,它重複了2個月前的現有答案。 – JasonMArcher