Passing labels to selectize and returning values for server functions

SamanthaDS

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())
    })
  }
)
tospig

I can get the parameter codes to display, and multiple selections by either:

  • changing the parameter to a character (in stead of a factor), and using %in% rather than ==, or
  • by using [ 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

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing and returning functions in R

Functions and returning values

Returning values to other functions

Understanding returning values functions C

Returning values from functions in Python

trig functions returning incorrect values

returning multiple values from functions

python functions not returning their values properly?

Passing structs to functions and returning structs from functions in Zig

returning values from decorated functions in python

Error returning values in multiple callback functions with electron

Returning values from functions when efficiency matters

Returning values from functions in structures in Visual Basic

Returning values from nested functions - JavaScript

Two matrix summation functions returning different values

R: passing values to eval in nested functions

calling functions without objects or passing values

Passing values inbetween functions within an object

Passing values in to ajax loaded functions in javascript

Passing values to functions in React Native from Array

Passing values between functions in an R package

C++ passing values to functions using class

passing values across functions yields no output

Passing and returning structs to C functions on stack from Common Lisp with CFFI

Passing chords to and returning music with Lilypond music substitution functions

Returning dates / server timestamps in cloud callable functions

ChartJS - Returning labels and data from server on different calls

Loading values into Selectize.js

Pass values to database with selectize multiselect