shp file in the funtion


#1

Hello,

In my shiny application I call the funtion tab (see an extract below) that uses .shp file.
My issue is with the variable varcode.
In my shiny interface I put the value of the varcode (=toto) and I have the error message below :

Error:'names' attribute [4] must be the same length as the vector [2]

car = .shp file
varcode = interface variable

Extract of my function

tab<-function(car,varcode){

f<- readShapeSpatial(car)
....
colnames(f@data)[4]<-varcode
...
row.names(contig)<-f@data$varcode

but if I put manually the variable toto all things it's ok

Thanks in advance to your help!


#2

Hi @sbl_bah! Welcome!

I'm afraid there's not enough information here to know exactly why you're getting this error. Speaking very generally, the error is happening because varcode has the wrong length (it has length 2, here). I'm not at all sure I understand what varcode is supposed to be here, but it sounds like maybe you're expecting it to hold only a single value, rather than two values?

The next step is to work backwards to the code that calls this function and supplies the varcode argument to see why something unexpected is getting passed in. You might start by taking a look at the debugging resources linked here: Shiny debugging and reprex guide

Beyond that, your best bet for getting more specific help is to construct a small reproducible example that has the same problem — see the guide above for some pointers on how to do that when working with shiny.


#3

Hello.
Thanks to your help!
I found the solution of my issue.
row.names(contig)<-f@data[ ,varcode] insted of row.names(contig)<-f@data$varcode
That's ok
Nice B-)