can't find variable from a dataframe when try to call in addCircleMarker in R shiny leaflet











up vote
0
down vote

favorite












I have a dataset that have the long and lat information, as well as the 'Experiment' info for each long and lat.



I used addCircleMarkers to add markers. And I want to have popup options for each marker. I tried to call "Experiment" at popup, but it gives me errors that it can't find this variable. I am not sure where it went wrong.



Dataset



ui <- fluidPage(
titlePanel("Land Explorer"),

selectInput("year", "Year of G2F", choices = c("2014", "2015", "2016", "All")),

fluidRow(
mainPanel(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map"), width = "100%", height = 1000)
)
)

server <- function(input, output){

pal <- colorFactor("RdYlGn", county_simplified$zone, reverse = T, na.color = "white")

state_popup <- paste0("<strong>State: </strong>",
county_simplified$region,
"<br><strong>County: </strong>",
county_simplified$subregion)

g2f <- reactive ({
if (input$year == "2014"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2014, ]
}
else if (input$year == "2015"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2015, ]
}
else if (input$year == "2016") {
g2f_latlong = g2f_latlong[g2f_latlong$year == 2016, ]
}
else if (input$year == "All"){
g2f_latlong = g2f_latlong
}
else (g2f_latlong = g2f_latlong)
})

output$map <- renderLeaflet({

leaflet() %>%
addTiles() %>%
addPolygons(data = county_simplified, weight = 1,
color = "white",
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(zone),
popup = state_popup) %>%
addPolygons(data = state_boundry, weight = 1, color = "grey", fill = NA) %>%
addLegend(pal = pal, value = county_simplified$zone, position = "topright", title = "Zones") %>%
addCircleMarkers(data = g2f(), lng = ~long, lat = ~lat, popup = ~Experiment, radius = 1)
})

}

shinyApp(ui = ui, server = server)









share|improve this question






















  • The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
    – msr_003
    Nov 8 at 5:17















up vote
0
down vote

favorite












I have a dataset that have the long and lat information, as well as the 'Experiment' info for each long and lat.



I used addCircleMarkers to add markers. And I want to have popup options for each marker. I tried to call "Experiment" at popup, but it gives me errors that it can't find this variable. I am not sure where it went wrong.



Dataset



ui <- fluidPage(
titlePanel("Land Explorer"),

selectInput("year", "Year of G2F", choices = c("2014", "2015", "2016", "All")),

fluidRow(
mainPanel(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map"), width = "100%", height = 1000)
)
)

server <- function(input, output){

pal <- colorFactor("RdYlGn", county_simplified$zone, reverse = T, na.color = "white")

state_popup <- paste0("<strong>State: </strong>",
county_simplified$region,
"<br><strong>County: </strong>",
county_simplified$subregion)

g2f <- reactive ({
if (input$year == "2014"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2014, ]
}
else if (input$year == "2015"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2015, ]
}
else if (input$year == "2016") {
g2f_latlong = g2f_latlong[g2f_latlong$year == 2016, ]
}
else if (input$year == "All"){
g2f_latlong = g2f_latlong
}
else (g2f_latlong = g2f_latlong)
})

output$map <- renderLeaflet({

leaflet() %>%
addTiles() %>%
addPolygons(data = county_simplified, weight = 1,
color = "white",
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(zone),
popup = state_popup) %>%
addPolygons(data = state_boundry, weight = 1, color = "grey", fill = NA) %>%
addLegend(pal = pal, value = county_simplified$zone, position = "topright", title = "Zones") %>%
addCircleMarkers(data = g2f(), lng = ~long, lat = ~lat, popup = ~Experiment, radius = 1)
})

}

shinyApp(ui = ui, server = server)









share|improve this question






















  • The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
    – msr_003
    Nov 8 at 5:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a dataset that have the long and lat information, as well as the 'Experiment' info for each long and lat.



I used addCircleMarkers to add markers. And I want to have popup options for each marker. I tried to call "Experiment" at popup, but it gives me errors that it can't find this variable. I am not sure where it went wrong.



Dataset



ui <- fluidPage(
titlePanel("Land Explorer"),

selectInput("year", "Year of G2F", choices = c("2014", "2015", "2016", "All")),

fluidRow(
mainPanel(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map"), width = "100%", height = 1000)
)
)

server <- function(input, output){

pal <- colorFactor("RdYlGn", county_simplified$zone, reverse = T, na.color = "white")

state_popup <- paste0("<strong>State: </strong>",
county_simplified$region,
"<br><strong>County: </strong>",
county_simplified$subregion)

g2f <- reactive ({
if (input$year == "2014"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2014, ]
}
else if (input$year == "2015"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2015, ]
}
else if (input$year == "2016") {
g2f_latlong = g2f_latlong[g2f_latlong$year == 2016, ]
}
else if (input$year == "All"){
g2f_latlong = g2f_latlong
}
else (g2f_latlong = g2f_latlong)
})

output$map <- renderLeaflet({

leaflet() %>%
addTiles() %>%
addPolygons(data = county_simplified, weight = 1,
color = "white",
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(zone),
popup = state_popup) %>%
addPolygons(data = state_boundry, weight = 1, color = "grey", fill = NA) %>%
addLegend(pal = pal, value = county_simplified$zone, position = "topright", title = "Zones") %>%
addCircleMarkers(data = g2f(), lng = ~long, lat = ~lat, popup = ~Experiment, radius = 1)
})

}

shinyApp(ui = ui, server = server)









share|improve this question













I have a dataset that have the long and lat information, as well as the 'Experiment' info for each long and lat.



I used addCircleMarkers to add markers. And I want to have popup options for each marker. I tried to call "Experiment" at popup, but it gives me errors that it can't find this variable. I am not sure where it went wrong.



Dataset



ui <- fluidPage(
titlePanel("Land Explorer"),

selectInput("year", "Year of G2F", choices = c("2014", "2015", "2016", "All")),

fluidRow(
mainPanel(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("map"), width = "100%", height = 1000)
)
)

server <- function(input, output){

pal <- colorFactor("RdYlGn", county_simplified$zone, reverse = T, na.color = "white")

state_popup <- paste0("<strong>State: </strong>",
county_simplified$region,
"<br><strong>County: </strong>",
county_simplified$subregion)

g2f <- reactive ({
if (input$year == "2014"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2014, ]
}
else if (input$year == "2015"){
g2f_latlong = g2f_latlong[g2f_latlong$year == 2015, ]
}
else if (input$year == "2016") {
g2f_latlong = g2f_latlong[g2f_latlong$year == 2016, ]
}
else if (input$year == "All"){
g2f_latlong = g2f_latlong
}
else (g2f_latlong = g2f_latlong)
})

output$map <- renderLeaflet({

leaflet() %>%
addTiles() %>%
addPolygons(data = county_simplified, weight = 1,
color = "white",
dashArray = '3',
fillOpacity = 0.7,
fillColor = ~pal(zone),
popup = state_popup) %>%
addPolygons(data = state_boundry, weight = 1, color = "grey", fill = NA) %>%
addLegend(pal = pal, value = county_simplified$zone, position = "topright", title = "Zones") %>%
addCircleMarkers(data = g2f(), lng = ~long, lat = ~lat, popup = ~Experiment, radius = 1)
})

}

shinyApp(ui = ui, server = server)






r shiny leaflet






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 18:26









Keru Chen

61




61












  • The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
    – msr_003
    Nov 8 at 5:17


















  • The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
    – msr_003
    Nov 8 at 5:17
















The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
– msr_003
Nov 8 at 5:17




The dataset is not reproducible and unclear. It is hard to do get data from an image. Instead you could provide the output of dput(head(g2f_latlong)) which is more easy to work.
– msr_003
Nov 8 at 5:17












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are not returning the filtered data from reactive environment, so your addCircleMarkers function do not get data to plot on leaflet. Change your reactive environment as shown below and i hope this solves your issue.



g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})





share|improve this answer





















  • With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
    – msr_003
    Nov 8 at 5:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195583%2fcant-find-variable-from-a-dataframe-when-try-to-call-in-addcirclemarker-in-r-sh%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













You are not returning the filtered data from reactive environment, so your addCircleMarkers function do not get data to plot on leaflet. Change your reactive environment as shown below and i hope this solves your issue.



g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})





share|improve this answer





















  • With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
    – msr_003
    Nov 8 at 5:25















up vote
0
down vote













You are not returning the filtered data from reactive environment, so your addCircleMarkers function do not get data to plot on leaflet. Change your reactive environment as shown below and i hope this solves your issue.



g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})





share|improve this answer





















  • With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
    – msr_003
    Nov 8 at 5:25













up vote
0
down vote










up vote
0
down vote









You are not returning the filtered data from reactive environment, so your addCircleMarkers function do not get data to plot on leaflet. Change your reactive environment as shown below and i hope this solves your issue.



g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})





share|improve this answer












You are not returning the filtered data from reactive environment, so your addCircleMarkers function do not get data to plot on leaflet. Change your reactive environment as shown below and i hope this solves your issue.



g2f <- reactive({
if (input$year == "2014") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2014, ]
}else if (input$year == "2015") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2015, ]
}else if (input$year == "2016") {
filtered_data <- g2f_latlong[g2f_latlong$year == 2016, ]
}else if (input$year == "All") {
filtered_data <- g2f_latlong
}else {filtered_data <- g2f_latlong}
return(filtered_data)
})






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 5:10









msr_003

3671315




3671315












  • With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
    – msr_003
    Nov 8 at 5:25


















  • With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
    – msr_003
    Nov 8 at 5:25
















With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
– msr_003
Nov 8 at 5:25




With the limited explanation and data i could only check for addCircleMarkers. But you are using ` county_simplified` & state_boundry data, which are not available to check for addPolygons .
– msr_003
Nov 8 at 5:25


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195583%2fcant-find-variable-from-a-dataframe-when-try-to-call-in-addcirclemarker-in-r-sh%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud