Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

r - Interactively change the selectInput choices

Originally I create this shiny interface that takes in a parameter "company id" and "date", but here we have a problem: most people dont know the companies we work with by their id, only their name, i.e. (McDonalds, Radioshack).

So I want to ideally create a search function like this enter image description here

My current idea is to pass in a table including a list of all our partner companies and their ids to global.R. Then pass in the textInput as the search variables and perform the search on server side. However, I get lost on how to pass searchResults back into the UI on a selectInput panel?

My current code:

ui.R

library(shiny)

shinyUI(pageWithSidebar(


  sidebarPanel(

    textInput("nameSearch", "Or, Search for company name", 'McDonald'),
    selectInput("partnerName", "Select your choice", list( "searchResults" ),
    br(),
    submitButton("Update View"),
    br(),

  ),

server.R

  shinyServer(function(input, output) {

  #subTable
  searchResult<- reactive({
    subset(partners, grepl(input$nameSearch, partners$name))
  })

  output$searchResults <- renderTable({ 
    searchResult[,1]
    })

global.R

partners<- read.csv("partnersList.csv", fill=TRUE)

partnersList is just in this format

    name            id 
 ------------------
    McDonalds        1
    Wendy's          2
    Bestbuy          3 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to make the UI reactive. I haven't tested this (miss data for it too) but should work I think. In server.R add:

output$selectUI <- renderUI({ 
selectInput("partnerName", "Select your choice", searchResult()[,1] ),
})

And in ui.R replace the selectInput with:

htmlOutput("selectUI")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...