R shiny: deselect from selectize









up vote
2
down vote

favorite
1












First of all, my thanks to Shree, who cleaned and made my previous code more performant here



Anyhow, I have some code that enables a user to select one or more species (from selectizeInput). The application shows you the species distribution on the map.



Now, I am puzzled, because I cannot deselect species? Once distributions are plotted, they remain on the map and I cannot remove them anymore...
I have been looking thoroughly, but unable to see it.. I am pretty new to shiny.. so probably an easy mistake?



All code below,
THanks!!!
JOnas



DATAFRAME



df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c("37", "28", "21", "30")),
Y= sample(c("-5", "-16", "-10", "-15"))
)


UI



ui <- (fluidPage(titlePanel("Species Checker"), 
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))


SERVER



server <- function(input, output, session) 
map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(
leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2)
)

map_proxy <- leafletProxy("CountryMap")

observe(
md <- map_data()
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
)



Run the application



shinyApp(ui = ui, server = server)









share|improve this question









New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
    – BigDataScientist
    yesterday














up vote
2
down vote

favorite
1












First of all, my thanks to Shree, who cleaned and made my previous code more performant here



Anyhow, I have some code that enables a user to select one or more species (from selectizeInput). The application shows you the species distribution on the map.



Now, I am puzzled, because I cannot deselect species? Once distributions are plotted, they remain on the map and I cannot remove them anymore...
I have been looking thoroughly, but unable to see it.. I am pretty new to shiny.. so probably an easy mistake?



All code below,
THanks!!!
JOnas



DATAFRAME



df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c("37", "28", "21", "30")),
Y= sample(c("-5", "-16", "-10", "-15"))
)


UI



ui <- (fluidPage(titlePanel("Species Checker"), 
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))


SERVER



server <- function(input, output, session) 
map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(
leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2)
)

map_proxy <- leafletProxy("CountryMap")

observe(
md <- map_data()
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
)



Run the application



shinyApp(ui = ui, server = server)









share|improve this question









New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
    – BigDataScientist
    yesterday












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





First of all, my thanks to Shree, who cleaned and made my previous code more performant here



Anyhow, I have some code that enables a user to select one or more species (from selectizeInput). The application shows you the species distribution on the map.



Now, I am puzzled, because I cannot deselect species? Once distributions are plotted, they remain on the map and I cannot remove them anymore...
I have been looking thoroughly, but unable to see it.. I am pretty new to shiny.. so probably an easy mistake?



All code below,
THanks!!!
JOnas



DATAFRAME



df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c("37", "28", "21", "30")),
Y= sample(c("-5", "-16", "-10", "-15"))
)


UI



ui <- (fluidPage(titlePanel("Species Checker"), 
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))


SERVER



server <- function(input, output, session) 
map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(
leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2)
)

map_proxy <- leafletProxy("CountryMap")

observe(
md <- map_data()
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
)



Run the application



shinyApp(ui = ui, server = server)









share|improve this question









New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











First of all, my thanks to Shree, who cleaned and made my previous code more performant here



Anyhow, I have some code that enables a user to select one or more species (from selectizeInput). The application shows you the species distribution on the map.



Now, I am puzzled, because I cannot deselect species? Once distributions are plotted, they remain on the map and I cannot remove them anymore...
I have been looking thoroughly, but unable to see it.. I am pretty new to shiny.. so probably an easy mistake?



All code below,
THanks!!!
JOnas



DATAFRAME



df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c("37", "28", "21", "30")),
Y= sample(c("-5", "-16", "-10", "-15"))
)


UI



ui <- (fluidPage(titlePanel("Species Checker"), 
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))


SERVER



server <- function(input, output, session) 
map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(
leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2)
)

map_proxy <- leafletProxy("CountryMap")

observe(
md <- map_data()
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
)



Run the application



shinyApp(ui = ui, server = server)






r






share|improve this question









New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday





















New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









jonas

154




154




New contributor




jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






jonas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
    – BigDataScientist
    yesterday
















  • are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
    – BigDataScientist
    yesterday















are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
– BigDataScientist
yesterday




are you sure the Code is working? For me it crashes as the Long/lat values seem to be of type character and not numeric?
– BigDataScientist
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Try the below (I've unquoted some of the variables in df as otherwise the app crashes):



library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
Number_Total = sample(c(5, 6, 1, 3)),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c(37, 28, 21, 30)),
Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))

server <- function(input, output, session)

map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(

leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))

)



shinyApp(ui = ui, server = server)


You need to pull the values directly from the reactive context; I'd also suggest to do this in one statement rather than spreading it around.



P.S. In case your initial dataframe actually does contain all the variables in character format, you can add just in case the mutate_at statement in map_data part, like this:



map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))


Then the script functions also with your initial dataframe provided here without manual changes.






share|improve this answer


















  • 1




    Hello, it works! Thank you for providing feedback! Very much appreciated!
    – jonas
    20 hours ago










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
);



);






jonas is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224887%2fr-shiny-deselect-from-selectize%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Try the below (I've unquoted some of the variables in df as otherwise the app crashes):



library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
Number_Total = sample(c(5, 6, 1, 3)),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c(37, 28, 21, 30)),
Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))

server <- function(input, output, session)

map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(

leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))

)



shinyApp(ui = ui, server = server)


You need to pull the values directly from the reactive context; I'd also suggest to do this in one statement rather than spreading it around.



P.S. In case your initial dataframe actually does contain all the variables in character format, you can add just in case the mutate_at statement in map_data part, like this:



map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))


Then the script functions also with your initial dataframe provided here without manual changes.






share|improve this answer


















  • 1




    Hello, it works! Thank you for providing feedback! Very much appreciated!
    – jonas
    20 hours ago














up vote
0
down vote



accepted










Try the below (I've unquoted some of the variables in df as otherwise the app crashes):



library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
Number_Total = sample(c(5, 6, 1, 3)),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c(37, 28, 21, 30)),
Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))

server <- function(input, output, session)

map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(

leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))

)



shinyApp(ui = ui, server = server)


You need to pull the values directly from the reactive context; I'd also suggest to do this in one statement rather than spreading it around.



P.S. In case your initial dataframe actually does contain all the variables in character format, you can add just in case the mutate_at statement in map_data part, like this:



map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))


Then the script functions also with your initial dataframe provided here without manual changes.






share|improve this answer


















  • 1




    Hello, it works! Thank you for providing feedback! Very much appreciated!
    – jonas
    20 hours ago












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Try the below (I've unquoted some of the variables in df as otherwise the app crashes):



library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
Number_Total = sample(c(5, 6, 1, 3)),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c(37, 28, 21, 30)),
Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))

server <- function(input, output, session)

map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(

leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))

)



shinyApp(ui = ui, server = server)


You need to pull the values directly from the reactive context; I'd also suggest to do this in one statement rather than spreading it around.



P.S. In case your initial dataframe actually does contain all the variables in character format, you can add just in case the mutate_at statement in map_data part, like this:



map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))


Then the script functions also with your initial dataframe provided here without manual changes.






share|improve this answer














Try the below (I've unquoted some of the variables in df as otherwise the app crashes):



library(tidyverse)
library(shiny)
library(leaflet)

df <- data.frame(
Number_Total = sample(c(5, 6, 1, 3)),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c(37, 28, 21, 30)),
Y= sample(c(-5, -16, -10, -15))
)

ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))

server <- function(input, output, session)

map_data <- reactive(
#req(input$species)
df[df$Species %in% input$species, ]
)

output$CountryMap <- renderLeaflet(

leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))

)



shinyApp(ui = ui, server = server)


You need to pull the values directly from the reactive context; I'd also suggest to do this in one statement rather than spreading it around.



P.S. In case your initial dataframe actually does contain all the variables in character format, you can add just in case the mutate_at statement in map_data part, like this:



map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))


Then the script functions also with your initial dataframe provided here without manual changes.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









arg0naut

852212




852212







  • 1




    Hello, it works! Thank you for providing feedback! Very much appreciated!
    – jonas
    20 hours ago












  • 1




    Hello, it works! Thank you for providing feedback! Very much appreciated!
    – jonas
    20 hours ago







1




1




Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago




Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago










jonas is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















jonas is a new contributor. Be nice, and check out our Code of Conduct.












jonas is a new contributor. Be nice, and check out our Code of Conduct.











jonas is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224887%2fr-shiny-deselect-from-selectize%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20