使用Rstudio我试图制作一个产生传单输出的闪亮应用程序。请注意,闪亮是我之前没有使用的包,因此脚本中可能存在其他错误,而不是我目前遇到的错误。
我正在使用一个数据框,其中包含不同个体的轨迹,我想要对其进行分组,并根据输入选择绘制一只动物的轨迹。
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
的所有行。
在此子集之后,我想仅对先前子集化数据帧中的“long”和“lat”列进行子集化。然后由传单读取该数据帧。
最后一步是在地图上绘制这些“长”和“纬度”位置。
不幸的是我不断收到错误消息:
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部分和其中的子集部分有关。每次都必须更新它以响应从输入选择中选择的选项。不幸的是,我找不到任何解决这个问题的解决办法。
有关如何解决此问题的任何建议?
提前致谢,
ONNO
答案 0 :(得分:2)
我认为您可以完全避开eventReactive
,因为renderLeaflet
会对input
的更改产生反应。这是一个改编的例子,因为我没有你的数据。如果它没有意义,请告诉我。
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)
答案 1 :(得分:0)
有点晚了,但为了完整性并说明如何使用eventReactive。 您必须为其指定一个名称,然后调用eventReactive( df()),否则它将永远不会运行。
此外,如果您将input$whaleID
放在括号中,它将对"input$whaleID"
的数据帧列进行子集化,这不在我想要的数据帧中。因此,您希望它没有括号,因此它从selectInput中获取所选参数。
您也可以将Icon放在服务器之外,因为它不必被动。
require(shiny)
require(leaflet)
require(dplyr)
##Main dataframe I want to subset depending on inputselection
dataframe <- read.csv2("PW_with_speedbearingturn.csv")
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)
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){
df <- eventReactive(input$whaleID,{
daf <- subset(dataframe, WhaleID == input$whaleID)
subset(daf, select = c("long", "lat"))
})
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolylines( lng = df()$long, lat = df()$lat, col = "red", opacity = 1)%>%
addMarkers( lng = first(df()$long), lat = first(df()$lat), icon = Start,
popup = "Start")
})
}
shinyApp(ui = ui, server = server)