创建一个依赖于上一个闪亮和r的先前输入选择的输入选择框

时间:2019-11-11 09:23:48

标签: r shiny

我想在其中选择两个选择输入框...

第一个将打印“ raj”和“ other”之类的标签 第二个将在单击raj时打印该区的30个名称,在单击另一个时将打印州的34个名称。 无论是raj还是其他,都只有标签,并且将从MS SQL Server作为存储过程获取此区和状态列表,因此我必须将它们用作dtd $ DistrictName和dtd2 $ StateName都在数据库中,不同的查询是我的原因将它们存储在不同的变量中,例如dtd和dtd2。 任何人都知道如何在其他选择框中单击选择框1 raj中的标签,并单击另一个名为其他标签的标签时打印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)

2 个答案:

答案 0 :(得分:0)

请参见下文!我看到您是发布新手,以后请提供一个可重复的示例...我们无法运行您的查询,因此必须猜测如何设置数据集。

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)

根据OP的评论进行更新

  

如果我们考虑您给定的解决方案,我想怎么办   就像下面给出的场景:   countyData = data.frame(“ Raj” = c(“ District 1”,“ District 2”,“ District 3”,“ District   4“),”状态“ = c(”状态1“,”状态2“,”状态3“),stringsAsFactors = FALSE)   地区的长度为4,州的尺寸为3

如果长度不同,则不能创建data.frame,但是可以创建行为非常相似的list

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 = list("Raj"=c("District 1","District 2", "District 3","District 4"),"State"=c("State 1","State 2","State 3"))


  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)

关于您的评论...

  

inputvalue <-data.frame(a = c(“ Ajmer”,“ Alwar”,“ Banswara”,“ Baran”,“ Barmer”),b = c(“ Chittorgarh”,“ Churu”,“ Dausa” ,“ Dolpur”,“ Dungarpur”))   countyData = data.frame(“ Raj” = c(inputvalue $ a),“ State” = c(inputvalue $ b),stringsAsFactors = FALSE)和   为什么这会打印数字而不是实际的矢量值,例如   “ Ajmer”,“ Alwar”,“ Banswara”等

问题是您的inputvalue未使用stringsAsFactors,这导致所有内容都归类。这就是为什么您看到数字的原因。另外,上面是可以做到的冗余代码。

countyData = data.frame("Raj" = c("Ajmer","Alwar","Banswara","Baran","Barmer"), "State" = c("Chittorgarh","Churu","Dausa","Dholpur","Dungarpur"), stringsAsFactors = FALSE)

答案 1 :(得分:0)

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)

enter image description here

虚拟数据集: 存储过程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

输出屏幕截图: enter image description here

检查元素键值: enter image description here