如何根据闪亮的inputselect子集数据框并使用传单进行绘图 [英] How to subset a dataframe and plot with leaflet depending on inputselect in shiny

查看:59
本文介绍了如何根据闪亮的inputselect子集数据框并使用传单进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rstudio,我正在尝试制作一个能够生成传单输出的闪亮应用程序.请注意,Shiny是我以前从未使用过的软件包,因此脚本中可能有除我当前遇到的错误以外的其他错误.

Using Rstudio I am trying to make a shiny app that produces leaflet outputs. Note that shiny is a package I have not used before so it could be that there are other mistakes in the script than the one I am currently encountering.

我正在使用一个数据框,其中包含不同个体的轨迹,我想根据输入选择对其中的一只动物的轨迹进行子集化并绘制轨迹.

I am using one dataframe with tracks of different individuals from which I want to subset and plot the track of one animal in response to inputselection.

Sample:

WhaleID     lat       long
gm08_150c   68,4276   16,5192
gm08_150c   68,4337   16,5263
gm08_150c   68,4327   16,5198
gm08_154d   68,4295   16,5243
gm08_154d   68,4313   16,5314
gm08_154d   68,4281   16,5191

输入选择中的选择是.csv文件中列WhaleID中使用的确切名称,因此我希望从主数据框中将所有具有该WhaleID的行作为子集.

The choices in the input selection are the exact names used in the .csv file, in the column WhaleID, so I would like the subset all rows with that WhaleID from the main dataframe.

在此子集之后,我只想对先前子集的数据帧中的"long"和"lat"列进行子集化.然后,通过传单读取此数据帧.

After this subset I want to subset only the "long" and "lat" columns from the previously subsetted dataframe. This dataframe is then read by leaflet.

最后一步是在地图上绘制这些长"和纬"位置.

The final step is plotting these "long"and "lat" positions on a map.

不幸的是,我不断收到错误消息:

Unfortunately I keep getting an error message:

Warning: Error in $: object of type 'closure' is not subsettable
Stack trace (innermost first):
    82: inherits
    81: resolveFormula
    80: derivePolygons
    79: addPolylines
    78: function_list[[i]]
    77: freduce
    76: _fseq
    75: eval
    74: eval
    73: withVisible
    72: %>%
    71: func [#15]
    70: output$map
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

我正在使用的脚本:

    require(shiny)
    require(leaflet)

    ##Main dataframe I want to subset depending on inputselection    
    dataframe <-read.csv2("PW_with_speedbearingturn.csv") 


    ui <- bootstrapPage( 
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
      absolutePanel(top = 10, right = 10,
      selectInput(inputId = "whaleID", 
                  label = "Select a whale", 
                  choices = c("gm08_150c","gm08_154d"))),
      leafletOutput(outputId = "map", width = "100%", height = "700")
    ) 

    server <- function(input, output, session){ 

      #?observeEvent  
      #observeEvent(input$whaleID, )
      Start <- makeIcon(
        iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Dark_Green_Arrow_Down.svg/480px-Dark_Green_Arrow_Down.svg.png",
        iconWidth = 22, iconHeight = 20,
        iconAnchorX = 11, iconAnchorY = 20)

      eventReactive(input$whaleID,{
                    df<-subset(dataframe, WhaleID == "input$whaleID") ## "input$whaleID" should become the WhaleID that is selected from the inputselect.

                    df<-subset(df, select = c("long", "lat")) 
      })                
      output$map <- renderLeaflet({
      leaflet() %>%
      addTiles() %>%
      addPolylines(df, lng = df$long, lat = df$lat, col = "grey", opacity = 1)%>%
      addMarkers(df, lng = first(df$long), lat = first(df$lat), icon = Start, popup = "Start")
      })
    } 

    shinyApp(ui = ui, server = server)

我认为这与eventreactive部分和其中的子集部分有关.每次响应于从输入选择中选择了什么选择,都必须对其进行更新.不幸的是,我找不到这个问题的任何现有解决方案.

I believe it has to do with the eventreactive section and the subsetting part within. It has to be updated each time in response to what choice is selected from the inputselection. Unfortunately I could not find any exsisting solutions to this question.

有关如何解决此问题的任何建议?

Any advice on how to solve this issue?

预先感谢

Onno

推荐答案

我认为您可以完全避免使用eventReactive,因为renderLeaflet会对input的更改做出反应.这是一个经过修改的示例,因为我没有您的数据.让我知道这是否没有道理.

I think you can avoid the eventReactive entirely since the renderLeaflet will be reactive on changes to input. Here is an adapted example since I don't have your data. Let me know if it does not make sense.

require(shiny)
require(leaflet)

##Main dataframe I want to subset depending on inputselection    
dataframe <- data.frame(
  abb = state.abb,
  x = state.center$x,
  y = state.center$y,
  stringsAsFactors = FALSE
)


ui <- bootstrapPage( 
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  absolutePanel(top = 10, right = 10,
                selectInput(inputId = "whaleID", 
                            label = "Select a state", 
                            choices = dataframe[["abb"]])),
  leafletOutput(outputId = "map", width = "100%", height = "700")
)

server <- function(input, output, session){ 

  Start <- makeIcon(
    iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Dark_Green_Arrow_Down.svg/480px-Dark_Green_Arrow_Down.svg.png",
    iconWidth = 22, iconHeight = 20,
    iconAnchorX = 11, iconAnchorY = 20)


  output$map <- renderLeaflet({
    df <- subset(dataframe, abb == input$whaleID)
    leaflet() %>%
      addTiles() %>%
      #addPolylines(df, lng = df$long, lat = df$lat, col = "grey", opacity = 1)%>%
      addMarkers(data = df, lng = ~x, lat = ~y, icon = Start, popup = "Start")
  })
} 

shinyApp(ui = ui, server = server)

这篇关于如何根据闪亮的inputselect子集数据框并使用传单进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆