我是R Shiny的新手。 单击地图上的标记后,我可以显示1个折线图。但是,我想在单击地图上的标记时一起显示2个折线图,一个折线图在地图的一侧。 我在下面附上了2条折线图的代码。 任何帮助将不胜感激。谢谢。
library(shiny) # for shiny apps
library(leaflet) # renderLeaflet function
library(readxl)
library(ggplot2)
server = function(input, output) {
Cookedfood_R <- readRDS("~/hawkermaster.rds")
line <- readRDS("~/line.rds")
line2 <- readRDS("~/line2.rds")
#Reshape the data for ggplot
traff2 <- melt(line,id=c("TYPE","newname"),variable.name = "Year")
traff3 <- melt(line2,id=c("TYPE","newname"),variable.name = "Year")
#Remove the X in the Year column and convert it to number
traff2$Year <- as.numeric(gsub(pattern="X",replacement = "",x = as.character(traff2$Year)))
traff3$Year <- as.numeric(gsub(pattern="X",replacement = "",x = as.character(traff3$Year)))
#getColor <- function(Cookedfood_R) {
# sapply(Cookedfood_R$TYPE, function(TYPE) {
# if(TYPE == 1) {"blue"}
# else {"orange"} })
#}
icons <- awesomeIcons(
icon = 'ion-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(Cookedfood_R)
)
output$map = renderLeaflet({
leaflet() %>% addTiles() %>%
addMarkers(data = Cookedfood_R,
lat = ~ LATITUDE,
lng = ~ LONGITUDE,
icon = icons,
layerId =~HAWKER,
popup = paste(Cookedfood_R$HAWKER, "<br>",
"No. of cooked food stalls:", Cookedfood_R$Cook, "<br>",
"No. of Market stalls:", Cookedfood_R$market,"<br>"))})
# generate data in reactive
ggplot_data <- reactive({
site <- input$map_marker_click$id
traff2[traff2$newname %in% site,]
})
ggplot_data2 <- reactive({
site2 <- input$map_marker_click$id2
traff3[traff3$newname %in% site2,]
})
output$plot1 <- renderPlot({
ggplot(data = ggplot_data(), aes(x = Year, y = value, color = TYPE))+
geom_line()+theme_bw()
#geom_point(aes(shape=TYPE, size=1))
})
output$plot2 <- renderPlot({
ggplot(data = ggplot_data2(), aes(x = Year, y = value, color = TYPE))+
geom_line()+theme_bw()
#geom_point(aes(shape=TYPE, size=1))
})
}
ui <- fluidPage(
br(),
column(8,leafletOutput("map", height="700px")),
column(4,br(),br(),br(),br(),plotOutput("plot1", height="300px")),
column(4,br(),br(),br(),br(),plotOutput("plot2", height="300px")),
br()
)
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
没有测试数据很难测试您的代码,但是此行不正确:
site2 <- input$map_marker_click$id2
每个标记单击更新input$map_marker_click$id
-没有id2
。
如果我在那一个标记中正确理解了您的代码,请单击以显示2个折线图,然后更改为:
site2 <- input$map_marker_click$id
可能会工作。如果没有,请告诉我更多详细信息和/或请提供一些虚拟数据供您使用。