Arguments imply differing number of rows for dynamic vector

library(RODBC)
library(RODBCext)
#> 
#> Attaching package: 'RODBCext'
#> The following objects are masked from 'package:RODBC':
#> 
#>     odbcFetchRows, sqlFetchMore
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(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box
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
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable
library(tidyr)


dbcnd <- odbcDriverConnect('Driver={SQL Server};Server=;Database=;Uid=;Pwd=')

rm(list=ls())

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


server = function(input, output) {
  
  dbcnd <- odbcDriverConnect('Driver={SQL Server};Server=;Database=;Uid=sa;Pwd=')
  
  qrydd<-paste("exec database_name[sp_name]'','','','','0','0','0','','','01/01/2010','31/12/2019'")
  qrydd
  dtd <- sqlQuery(dbcnd,qrydd)
  dtd <- data.frame(dtd)
  dtd
  closeAllConnections()
  
  qrydd1<-paste("exec database_name[sp_name]'','','','','0','0','2','0','','01/01/2017','31/12/2017'")
  qrydd1
  dtd1 <- sqlQuery(dbcnd,qrydd1)
  dtd1 <- data.frame(dtd1)
  dtd1
  closeAllConnections()
  
  
  countyData = data.frame("Raj"=c(dtd$DistrictName),"State"=c(dtd1$StateName),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)

StackOverflow link for the same question

countyData = data.frame("Raj"=c(dtd$DistrictName),"State"=c(dtd1$StateName),stringsAsFactors = FALSE)
throwing an error different number of rows bcoz query1 contains 34 records and query2 contains 33 records,
*does anyone know how to get it right and even though *
i use the same stored procedure(just for testing) to avoid this,
why is this giving numbers like(1,2,3 till 34) in sub-menu instead of names of the 34 district.
could anyone please guide me through what I am doing wrong here.

let's say....

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

and why this prints number instead of real vector values like "Ajmer","Alwar","Banswara"
and so on .how to convert this ino a string format

for simple understanding what i am doing is

inputvalue<-data.frame(a=c("aa","bb","cc","dd","ee","ff","gg"),b=c("aaaa","bbbb","cccc","dddd","eeee"))
  
   countyData =data.frame("Raj"= as.factor(c(inputvalue$a)),"State"=as.factor(c(inputvalue$b)),stringsAsFactors = FALSE)

and it's throwing the error, arguments imply differing number of rows

In your example, column "a" has 7 rows but column "b" only has 5, you can't make a data frame with uneven numbers of rows on its columns.

What to do then if my data schema looks like and since it is stored in ms sql server and used by other, I can't update or modify database, how to make it work, any idea would be great

If you need specific help please provide a proper self-contained reproducible example (including sample data, not an irreproducible call to a stored procedure on a database we don't have access to) illustrating your issue, I'm afraid we are lacking context to understand your problem.

dummy data looks like this..

stored procedure 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

stored procedure 2

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

code is same which is given above, instead of sp we can use this dummy dataset, what I really want to create two labels in select box named raj and other, when I click on raj , it must fetch the districtname from sp1 stored in data frame and ,sp2 for other,second value in data frame. when I try it for the same number of rows, it is working but not for different number of rows, and why is it displaying numbers instead of real names for those case too

i know it's kinda little bit confusing but my real concerns are:

1.) creating two select-box when we click on first it must render the value in second box respectively for it's related data list
2.when i applied it for same dataset for different values,it worked but why is it showing numbers like 1,2,3 and so on for the data frame value ,why doesn't it display real value instead

i hope you will get what i am tryna explain here

Could you please integrate your dummy datsets with minimal code into a proper reproducible example so it is easier to help you? , I think you would understand how to do it by reading section 6.3 on this book.

i have got the solution for that by converting that data frame into a list(solved by Kevin) but it addresses a new issue though, why is it displaying numbers instead of real value (literally numbers) 34 for first label(raj)as there are 34 rows for that and 33 for other,could anyone please tell me why it is so..
link for the solution is the following...

4 posts were split to a new topic: controlling the values that appear in Shiny selectInput's list of choices

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