以交互方式更改 selectInput 选项 [英] Interactively change the selectInput choices

查看:32
本文介绍了以交互方式更改 selectInput 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我创建了这个闪亮的界面,它接受参数company id"和date",但这里我们有一个问题:大多数人不知道我们合作的公司的 id,只知道他们的名字,即(麦当劳, 窝棚电台).

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

我目前的想法是将包含我们所有合作伙伴公司及其 ID 的列表传递给 global.R.然后传入 textInput 作为搜索变量并在服务器端执行搜索.但是,我不知道如何将 searchResults 传递回 selectInput 面板上的 UI?

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?

我当前的代码:

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]
    })

全局.R

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

partnersList 就是这种格式

partnersList is just in this format

    name            id 
 ------------------
    McDonalds        1
    Wendy's          2
    Bestbuy          3 

推荐答案

您需要使 UI 具有反应性.我还没有测试过这个(也没有测试过它的数据),但我认为应该可以.在 server.R 添加:

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] ),
})

并在 ui.R 中将 selectInput 替换为:

And in ui.R replace the selectInput with:

htmlOutput("selectUI")

这篇关于以交互方式更改 selectInput 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆