I want to pass distinct labels to selectizeInput
in Shiny. I then want the user input from selectize to pass an encoded parameter value to a function. I have the parameter codes and labels stored in a data frame. So, I should be able to access the parameter field in the data frame using a logical matching statement on the labels. However, I only seem to get the row number as output - not the actual parameter code. Also, multiple selections are not displaying.
Please see example below:
library(shiny)
library(dplyr)
dropdown_A<-as.data.frame( cbind(labels = c("red", "white", "blue"), parameter = c(800, 72, 9048)))
dropdown_B<-as.data.frame( cbind(labels = c("green", "purple", "orange"), parameter = c("xyz","def","abc")))
shinyApp(
ui = fluidPage(
fluidRow(
wellPanel(
selectizeInput("A", label = p("Select a color"), choices = as.character(dropdown_A$labels), multiple = TRUE),
selectizeInput("B", label = p("Select another color"), choices = as.character(dropdown_B$labels), multiple = TRUE))),
fluidRow(verbatimTextOutput("Value_A")),
fluidRow(verbatimTextOutput("Value_B"))),
server = function(input, output, session){
A<-reactive({
if (is.null(input$A))
return ("Please select a color")
else (dropdown_A %>% filter(labels == input$A)%>% select(parameter))
})
B<-reactive({
if (is.null(input$B))
return ("Please select another color")
else (dropdown_B %>% filter(labels == input$B)%>% select(parameter))
})
output$Value_A<-renderText({
as.character(A())
})
output$Value_B<-renderText({
as.character(B())
})
}
)
I can get the parameter codes to display, and multiple selections by either:
%in%
rather than ==
, or[
rather than %>%
.In your code, I've changed A()
to use the character value from dropdown_A
, and B()
uses [
.
library(shiny)
library(dplyr)
dropdown_A<-as.data.frame( cbind(labels = c("red", "white", "blue"), parameter = c(800, 72, 9048)))
dropdown_B<-as.data.frame( cbind(labels = c("green", "purple", "orange"), parameter = c("xyz","def","abc")))
dropdown_A$parameter <- as.character(dropdown_A$parameter)
shinyApp(
ui = fluidPage(
fluidRow(
wellPanel(
selectizeInput("A", label = p("Select a color"), choices = as.character(dropdown_A$labels), multiple = TRUE),
selectizeInput("B", label = p("Select another color"), choices = as.character(dropdown_B$labels), multiple = TRUE))),
fluidRow(verbatimTextOutput("Value_A")),
fluidRow(verbatimTextOutput("Value_B"))),
server = function(input, output, session){
A<-reactive({
if (is.null(input$A))
return ("Please select a color")
else((dropdown_A %>% filter(labels %in% input$A) %>% select(parameter)))
})
B<-reactive({
if (is.null(input$B))
return ("Please select another color")
else (dropdown_B[dropdown_B$labels %in% input$B, "parameter"])
})
output$Value_A<-renderText({
as.character(A())
})
output$Value_B<-renderText({
as.character(B())
})
}
)
Here is a screenshot of the output
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments