controlling the values that appear in Shiny selectInput's list of choices

reprex for code is the following... and dummy data is given above and stored procedure 1 is saved as sp1 and stored procedure 2 is saved as sp2 in csv format

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(shiny)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
rm(list=ls())

ui <-fluidPage( 
  titlePanel("Reactive select input boxes"),
  
  sidebarPanel( 
    uiOutput("FirstChoice"),
    uiOutput("SecondChoice")
  ),
  
  mainPanel()
)


server = function(input, output) {
  sp1<-read.csv(file = file.choose(),header = TRUE)
  sp1
  sp2<-read.csv(file = file.choose(),header = TRUE)
  sp2
  
  countyData = list("Raj"=c(sp1$DistrictName),"State"=c(sp2$StateName))
  
  output$FirstChoice <- renderUI ({ 
    selectInput(inputId = "FirstChoice",label = "Raj/State",
                choices = c("Raj","State"))
  })
  
  output$SecondChoice <- renderUI ({
    
    if (input$FirstChoice == "Raj"){lab <- "Raj"}
    else if (input$FirstChoice == "State") {lab <- "State"}
    else {lab <- "dependant"}
    
    selectInput(inputId = "SecondChoice", label = lab,
                choices = c(get(input$FirstChoice,countyData)))
  }) 
  
}


shinyApp(ui = ui, server = server)

data on the console panel looks like this...

output-screenshot is given below
23

could anyone tell me why it is printing numbers instead of real value, i have tried a couple of methods like as.factor and as.character but didn't work for me, any sort of help would be great

dummy dataset:

sp1

DID DistrictName BlockName CenterName TotRaj TotOtherState StateCode
101 a null null 564534 564534 null
201 b null null 675645 765645 null
301 c null null 765645 786756 null
401 d null null 987656 764534 null
501 e null null 675645 543423 null
601 f null null 765434 231234 null
701 g null null 564534 763423 null
801 h null null 234565 567876 null
901 i null null 985646 876345 null
102 j null null 876754 453675 null
202 k null null 876756 876754 null

sp2

DID DistrictName BlockName CenterName TotRaj TotOtherState StateName CID
101 a aaa null 564534 564534 sdd null
101 a bbb null 675645 765645 bdd null
101 a ccc null 765645 786756 cdd null
101 a ddd null 987656 764534 eff null
101 a eee null 675645 543423 ghh null
101 a fff null 765434 231234 sdg null
101 a ggg null 564534 763423 ghj null
101 a hhh null 234565 567876 lkj null
101 a iii null 985646 876345 fgh null
101 a jjj null 876754 453675 dfs null
101 a kkk null 876756 876754 fgd null
101 a aaa jkl 4534 6756 ghf null
101 a aaa mnl 5678 5645 sdf null
101 a aaa lkj 6756 4534 mnb null

what i have tried...
countyData = list("Raj"=as.list(paste(as.character(dtd$DistrictName))),"State"=as.list(paste(as.character(dtd$DistrictName)))),but when i click on any of these dropdown list value from select box it will render a chart as per specific id assigned to it,let's take scenario when district one is clicked it would plot a chart according to its id(101) from the database and so on, but when i checked the inspect element for that output what it is passing to database, it was just string values like district1 while i wanna show it as district1 but pass to DB as 101 to plot the chart specific to each scenario.

Screenshot_4
while i wanna display it like this exact way but wanna pass 101 for Alwar, 102 for Banswara and so on ,

just to make it less confusing,i have given screenshots of relevant scenario ,as in the image we can notice ,it is passing 99 to database but print amber on ui
that's how it looks like in ui
122222
that's how it is passed to ms SQL server
133333333
i hope this will clear it up and explain what i am tryna achieve here

it was done using given below code but not can be applied to my case as i am using dynamic vector

selectInput(
label = h4("Delivery Type :"),
      choices =
        list(
          "amber" = 99,
          "binaian" = 1,
          "caster" = 2,
          "delta" = 3
        ),
      selected =  "All Type",
      selectize = FALSE

instead of that ,what i am tryna doing is ...
countyData = list("Raj"=as.list(paste(as.character(dtd$DistrictName))),"State"=as.list(paste(as.character(dtd1$StateName))))

any suggestion to make this work would be great

I can't get your shiny reprex working because it refers to files on your computer. I'd simplify your question such that others can get it up and running quickly, and replicate the issue you're seeing.

The values displayed in the dropdown are defined by the choices argument in the selectInput function.


As an example

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

Values = c(5,25,50)
names(Values) = c("Value - 5", "Value - 25", "Valu - 50")

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput("bins",
                        "Number of bins:",
                        choices = Values,
                        # choices = c("Value5" = 5,"Value25" = 25,"Value50" = 50)
            )
        
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
        plotOutput("distPlot")
    )
)
)

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = as.numeric(input$bins) + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

thanks for your help i got what i was looking for ,your explanation fits my scenario best way possible,thanks for your help again even after i have provided half of the details earlier,i really appreciate your concern to this issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.