R shiny: deselect from selectize
up vote
2
down vote
favorite
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
New contributor
add a comment |
up vote
2
down vote
favorite
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
New contributor
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
New contributor
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
r
New contributor
New contributor
edited yesterday
New contributor
asked 2 days ago
jonas
154
154
New contributor
New contributor
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
add a comment |
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
add a comment |
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.
1
Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago
add a comment |
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.
1
Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago
add a comment |
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.
1
Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago
add a comment |
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.
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.
edited yesterday
answered yesterday
arg0naut
852212
852212
1
Hello, it works! Thank you for providing feedback! Very much appreciated!
– jonas
20 hours ago
add a comment |
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
add a comment |
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.
jonas is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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