创建一个输入选择框,该框依赖于shiny和r中以前的输入选择

创建一个输入选择框,该框依赖于shiny和r中以前的输入选择,r,shiny,R,Shiny,我想取两个选择输入框,其中 第一个将打印标签,如“raj”和“other” 第二个将在单击raj时打印30个地区名称,在单击其他名称时打印34个州名称。 raj和other以及除标签之外的其他任何东西以及这个地区和州列表都将作为存储过程从ms SQL server中获取,因此我必须使用它们,因为dtd$DistrictName和dtd2$StateName都在数据库中,不同的查询,这就是为什么我将它们存储在不同的变量(如dtd和dtd2)中。 当在其他selectbox中单击“选择框1”中的标签

我想取两个选择输入框,其中

第一个将打印标签,如“raj”和“other” 第二个将在单击raj时打印30个地区名称,在单击其他名称时打印34个州名称。 raj和other以及除标签之外的其他任何东西以及这个地区和州列表都将作为存储过程从ms SQL server中获取,因此我必须使用它们,因为dtd$DistrictName和dtd2$StateName都在数据库中,不同的查询,这就是为什么我将它们存储在不同的变量(如dtd和dtd2)中。 当在其他selectbox中单击“选择框1”中的标签时,任何人都知道如何打印dtd$DistrictName,对于另一个名为“其他”的标签,也知道如何打印dtd$DistrictName

我不知道如何解决这个问题,我已经尝试了一些来自网络的代码,这是其中之一,但对我来说不起作用

library(RODBC)
library(RODBCext)
library(plotly)
library(shinydashboard)
library(tidyverse)
library(readr)
library(RODBC)
library(RODBCext)
library(shiny)
library(psych) 

dbcnd <- odbcDriverConnect("Driver={SQL Server};Server=;Database=;Uid=;Pwd=''")
qrydd<<-paste("exec db..[sp_name] '0','0','','0','','01/01/2017','31/12/2017'")
    qrydd
    dtd <<- sqlQuery(dbcnd,qrydd)
    dtd <<- data.frame(dtd)
    dtd

qrydd2<<-paste("exec db..[sp_name]'','','','','0','0','2','0','','01/01/2017','31/12/2017'")
    qrydd2
    dtd2 <<- sqlQuery(dbcnd,qrydd2)
    dtd2 <<- data.frame(dtd2)
    dtd2

countyData = data.frame("raj"=dtd$DistrictName,"other_state"=dtd2$StateName,stringsAsFactors = FALSE)

 ui = shinyUI(
 fluidPage( 
 titlePanel("Reactive select input boxes"),

 sidebarPanel( 
 htmlOutput("state"),
 htmlOutput("district")
 ),

mainPanel(
 plotOutput("plot1") 
           )
       ) )



 server = shinyServer(function(input, output) {


output$state = renderUI({ 
 selectInput(inputId = "raj", 
 label = "raj:", 
 choices = as.character(unique(countyData$raj))) 
 })
 output$district = renderUI({

data_available = countyData[countyData$raj == input$raj, "other_state"]


selectInput(inputId = "other_state", 
 label = "other_state:", 
 choices = unique(data_available), 
 selected = unique(data_available)[1])
 })

# output$plot1 = renderPlot({
#  map('county', region = input$state)
# 
#  map('county', region =paste(input$state,input$county, sep=','),
#  add = T, fill = T, col = 'red')
#  
#  })
 })


 shinyApp(ui = ui, server = server)
库(RODBC)
图书馆(RODBEXT)
图书馆(绘本)
图书馆(shinydashboard)
图书馆(tidyverse)
图书馆(readr)
图书馆(RODBC)
图书馆(RODBEXT)
图书馆(闪亮)
图书馆(心理学)

dbcnd见下文!我看到你是新的张贴,在未来请提供一个可复制的例子。。。我们无法运行您的查询,因此必须猜测数据集是如何设置的

library(shinydashboard)
library(shiny)
library(dplyr)


rm(list=ls())

 ui = shinyUI(
 fluidPage( 
  titlePanel("Reactive select input boxes"),

   sidebarPanel( 
     uiOutput("FirstChoice"),
     uiOutput("SecondChoice")
   ),

    mainPanel()
  )
 )

 server = shinyServer(function(input, output) {

   countyData = data.frame("Raj"=c("District 1","District 2", "District 3"),"State"=c("State 1","State 2","State 3"),stringsAsFactors = FALSE)

   output$FirstChoice <- renderUI ({ 
      selectInput(inputId = "FirstChoice",label = "Raj/State",
                  choices = c("Raj","State"))
   })

   output$SecondChoice <- renderUI ({

     sub <- select(countyData, input$FirstChoice)

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

 })


 shinyApp(ui = ui, server = server)
至于你的评论

输入值

虚拟数据集: 存储过程1

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
存储过程2:

DID DistrictName    BlockName   StateCode TotRaj    TotOtherState   StateName   
101      a            aaa         1111  564534  564534  sdd          null
101      a            bbb         2222  675645  765645  bdd          null
101      a            ccc         3333  765645  786756  cdd          null
101      a            ddd         4444  987656  764534  eff          null
101      a            eee         5555  675645  543423  ghh          null
101      a            fff         6666  765434  231234  sdg          null
101      a            ggg         7777  564534  763423  ghj          null
101      a            hhh         8888  234565  567876  lkj          null
101      a            iii         9999  985646  876345  fgh          null
101      a            jjj         1011  876754  453675  dfs          null
101      a            kkk         2011  876756  876754  fgd          null
101      a            aaa         3011  5678    5645    sdf          null
101      a            aaa         4011  6756    4534    mnb          null
输出屏幕截图:

检查元素键值:

感谢您的帮助和建议,下次我会注意CountyData=data.frame(“Raj”=c(dtd$DistrictName),“State”=c(dtd1$StateName),“stringsAsFactors=FALSE),抛出错误不同行数bcoz query1包含34条记录,query2包含33条记录,您知道如何正确使用它吗?尽管我使用相同的存储过程(仅用于测试)来避免这种情况,但为什么在子菜单中给出类似(1,2,3到34)的数字,而不是34个区域的名称。你能告诉我我做了什么错事吗?HelLists说如果我们考虑你所给的解决方案,如果我想做如下给定的场景:-----------CordyDATA=DATA。帧(“RaJ”=C(“区域1”,“区域2”,“区域3”,“区域4”),“状态”=C(“状态1”,“状态2”,“状态3”),StrugsasFealths= FALSE)。其中,地区长度为4,州大小为3,第二个相关问题的代码段为:-------------inputvalue
Values = c(dtd$DID)
   names(Values) = as.list(as.character(dtd$DistrictName))

   values1=c(dtd1$StateCode)
   names(values1)=as.list(as.character(dtd1$StateName))

  countyData = list("Raj"=Values,"State"=values1)
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
DID DistrictName    BlockName   StateCode TotRaj    TotOtherState   StateName   
101      a            aaa         1111  564534  564534  sdd          null
101      a            bbb         2222  675645  765645  bdd          null
101      a            ccc         3333  765645  786756  cdd          null
101      a            ddd         4444  987656  764534  eff          null
101      a            eee         5555  675645  543423  ghh          null
101      a            fff         6666  765434  231234  sdg          null
101      a            ggg         7777  564534  763423  ghj          null
101      a            hhh         8888  234565  567876  lkj          null
101      a            iii         9999  985646  876345  fgh          null
101      a            jjj         1011  876754  453675  dfs          null
101      a            kkk         2011  876756  876754  fgd          null
101      a            aaa         3011  5678    5645    sdf          null
101      a            aaa         4011  6756    4534    mnb          null