代码之家  ›  专栏  ›  技术社区  ›  arox

闪亮的反应XY绘图:“closure”类型的对象不可子集

  •  2
  • arox  · 技术社区  · 8 年前

    作为一个闪亮的新人,我很难适应 example code 到我的特定数据集。我的数据集是 yelp dataset challenge .

    我的目标 是在当前地图部分创建餐厅“星级”与“评论数”的散点图。

    我得到的: 一个显示餐厅标记的地图,以及一个工作过滤器,用于将控制台中可见的餐厅计数为调试消息。

    我的问题:

    Listening on http://127.0.0.1:4587
    num restaurants: 194 ## <- this number changes when changing the map section##
    Warning: Error in [: object of type 'closure' is not subsettable
    Stack trace (innermost first):
    107: FUN
    106: lapply
    105: concat
    104: lrep
    103: latticeParseFormula
    102: xyplot.formula
    101: xyplot
    100: print
     99: renderPlot [C:/SomePath/server.R#54]
     89: <reactive:plotObj>
     78: plotObj
     77: origRenderFunc
     76: output$scatterStarsReviewCount
      1: runApp
    

    我读过很多关于堆栈溢出的帖子,它们都在处理这个错误消息,但它们似乎都归结为类似的解决方案 this that . 不幸的是,据我所知,这些提示无助于解决我的问题。
    我不理解为什么上面提到的示例代码可以工作,而我的代码会抛出这个错误。它们看起来非常相似。

    当前代码:

    用户界面。r

        library(shiny)
        library(leaflet)
    
    
        navbarPage(
          "Yelp",
          id = "nav",
    
          tabPanel(
            "Interactive map",
            div(
              class = "outer",
    
              tags$head(# Include our custom CSS
                includeCSS("styles.css"),
                includeScript("gomap.js")),
    
              # If not using custom CSS, set height of leafletOutput to a number instead of percent
              leafletOutput("map", width = "100%", height = "100%"),
    
              # Shiny versions prior to 0.11 should use class = "modal" instead.
              absolutePanel(
                id = "controls",
                class = "panel panel-default",
                fixed = TRUE,
                draggable = FALSE,
                top = 60,
                left = "auto",
                right = 20,
                bottom = "auto",
                width = 330,
                height = "auto",
    
                h3('ScatterPlot'),
    
                plotOutput("scatterStarsReviewCount", height = 250)
              )
            )
          )
        )
    

    服务器r

        library(leaflet)
        library(RColorBrewer)
        library(scales)
        library(lattice)
        library(dplyr)
    
        # Read pre-processed data
        bizrates <- read.table("bizrates.dat")
    
        function(input, output, session) {
          ## Interactive Map ###########################################
    
          # Create the map
          output$map <- renderLeaflet({
    
            Sbizrates = bizrates
    
            # generate map
            leaflet() %>%
              addTiles() %>%
              addTiles(urlTemplate = "http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png")  %>%
              mapOptions(zoomToLimits = "always") %>%
    
              addMarkers(
                lat = Sbizrates$biz_dat.latitude,
                lng = Sbizrates$biz_dat.longitude,
                clusterOptions = markerClusterOptions(),
                popup = Sbizrates$biz_dat.name
              )
          })
    
    
          # A reactive expression that returns the set of restaurants that are
          # in bounds right now
          restaurantsInBounds <- reactive({
            if (is.null(input$map_bounds))
              return(bizrates[FALSE,])
            bounds <- input$map_bounds
            latRng <- range(bounds$north, bounds$south)
            lngRng <- range(bounds$east, bounds$west)
    
            subset(bizrates,
                   biz_dat.latitude >= latRng[1] & biz_dat.latitude <= latRng[2] &
                     biz_dat.longitude >= lngRng[1] & biz_dat.longitude <= lngRng[2])
          })
    
          output$scatterStarsReviewCount <- renderPlot({
            # If no restaurants are in view, don't plot
            if (nrow(restaurantsInBounds()) == 0)
             return(NULL)
    
            cat(file=stderr(), "num restaurants:", nrow(restaurantsInBounds()),"\n")
    
            print(xyplot(stars ~ reviewCount, data = restaurantsInBounds()), 
                         xlim = range(bizrates$biz_dat.stars), 
                         ylim = range(bizrates$biz_dat.review_count)
                  )
          })
        }
    

    示例数据集:bizrates。dat公司

    "biz_dat.business_id" "biz_dat.name" "biz_dat.stars" "biz_dat.longitude" "biz_dat.latitude" "biz_dat.state" "biz_dat.attributes..Take.out." "biz_dat.attributes..Takes.Reservations." "biz_dat.attributes..Wi.Fi." "biz_dat.attributes.Caters" "biz_dat.review_count"
    "5" "mVHrayjG3uZ_RLHkLj-AMg" "Emil's Lounge" 4.5 -79.8663507 40.408735 "PA" TRUE FALSE "no" TRUE 11
    "6" "KayYbHCt-RkbGcPdGOThNg" "Alexion's Bar & Grill" 4 -80.067534 40.415517 "PA" TRUE FALSE "free" FALSE 15
    "13" "b9WZJp5L1RZr4F1nxclOoQ" "Gab & Eat" 4.5 -80.0847998 40.3967441 "PA" TRUE FALSE "no" TRUE 38
    "19" "rv7CY8G_XibTx82YhuqQRw" "Long John Silver's" 3.5 -80.0937037 40.3868915 "PA" TRUE FALSE "no" FALSE 3
    "22" "wqu7ILomIOPSduRwoWp4AQ" "Denny's" 4 -80.073426 40.391255 "PA" TRUE FALSE "no" FALSE 7
    "24" "P1fJb2WQ1mXoiudj8UE44w" "Papa J's" 3.5 -80.0854582 40.4082572 "PA" TRUE TRUE "no" TRUE 46
    "26" "PK6aSizckHFWk8i0oxt5DA" "McDonald's" 2 -79.910032 40.412086 "PA" TRUE FALSE "free" FALSE 5
    "30" "6ilJq_05xRgek_8qUp36-g" "Steak 'n Shake" 2 -79.9044562 40.413496 "PA" TRUE FALSE "no" FALSE 36
    "32" "MKyk4F4HSzHF8v-4cYe3Ww" "Sing Sing" 3.5 -79.9151108 40.4094884 "PA" FALSE TRUE "no" FALSE 25
    "37" "McikHxxEqZ2X0joaRNKlaw" "Rock Bottom" 3.5 -79.9151108 40.4094884 "PA" TRUE TRUE "no" FALSE 94
    

    注: 如果有人想执行代码,可以找到丢失的文件(styles.css、gomap.js) here .

    任何帮助和想法都将不胜感激!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Florian    8 年前

    为了检查哪里出了问题,我做了一些 print() 例如,代码中用于绘制xy图的语句 print(colnames(restaurantsInBounds())) . 结果是colnames是

    [1] "biz_dat.latitude" "biz_dat.longitude" "biz_dat.name" "biz_dat.stars" "biz_dat.review_count"
    

    但在plotting函数中,您可以参考其他列:

    print(xyplot(stars ~ reviewCount, data = restaurantsInBounds()),
    

    您可以通过相应地修改函数来修复它:

     output$scatterStarsReviewCount <- renderPlot({
        # If no restaurants are in view, don't plot
        if (nrow(restaurantsInBounds()) == 0)
          return(NULL)
    
        cat(file=stderr(), "num restaurants:", nrow(restaurantsInBounds()),"\n")
    
        print(colnames(restaurantsInBounds()))
        print(xyplot(biz_dat.stars ~ biz_dat.review_count, data = restaurantsInBounds()), 
              xlim = range(bizrates$biz_dat.stars), 
              ylim = range(bizrates$biz_dat.review_count)
        )
      })
    

    结果:

    enter image description here

    希望这有帮助!

    推荐文章