0

im trying to make a function that takes two files, say x and y, and returns a xls book, the function would create a number of sheets with the names selected from the x file, x would look like this

sol
   Orden
1   Z169
2   Z170
3   Z175

now, with the sheets created, it would write a data taken from the second (the y) file, the y file looks like this

base   
     Orden Dependencia
1    Z169    TRæNSITO
2    Z170    TRæNSITO
3    Z171    TRæNSITO
4    Z172    TRæNSITO
5    Z173    POLICIA
6    Z174    TRæNSITO
7    Z175    POLICIA

as far as i get, i have this code(in it minimal, complete, verifiable form, or it is not?)

autoform <- function(x,y){

## create wordbook
wb <- createWorkbook()

## create work sheets   
for (i in 1:length(x[,1])){
    if(isTRUE(x[i,1] %in% y[,1])){  
        addWorksheet(wb, x[i,1])

##editing the sheet 
        writeData(wb,  x[i,1], y[i,2], startCol = 8, startRow = 6,rowNames = TRUE )
        }
    }

## save file

saveWorkbook(wb, "marth/javi/supertest.xlsx", overwrite = TRUE)
}

when i try to use the autoform(x,y) i get this error:

> autoform(sol, base)
Error in nchar(sheetName) : 'nchar()' requires a character vector

i was hopping someone can explain me why is this error an how to solve it, tank u for reading

mimus
  • 53
  • 5
  • looks like you're missing a sheet name in writeData function call, so maybe `writedata(wb, "sheetname", x[i,1]...)`. See examples at https://www.rdocumentation.org/packages/openxlsx/versions/4.1.0/topics/writeData – infominer Mar 27 '19 at 18:05
  • but it is on the for cycle, the name should be selected from the x file, did i explain myself? – mimus Mar 27 '19 at 18:22
  • i try this line instead the old writedata "writeData(wb, sheet=x[i,1], y[i,2], startCol = 8, startRow = 6,rowNames = TRUE )" i also try writeData(wb, "sheetname", x[i,1], y[i,2], startCol = 8, startRow = 6,rowNames = TRUE ) all w/ the same effect and error – mimus Mar 27 '19 at 18:37
  • Ok, I see you're asking the same question again! https://stackoverflow.com/questions/55365666/r-and-openxlsx-i-cant-uderstand-why-im-getting-this-error-can-you-helpme?rq=1 sheetname is expected in addworksheet and writedata, look at first example in https://www.rdocumentation.org/packages/openxlsx/versions/4.1.0/topics/writeData. Sheetname can be anything you want to name the sheet. – infominer Mar 27 '19 at 19:04
  • actually i solve it, sorry for double post, im noob here, ill try to make it better – mimus Mar 27 '19 at 19:20

1 Answers1

0

tank all of u for reading, i made it by myself, the problem i think was that the index i is not the same that i need in the y variable, so i needed another for cycle this is the code working

autoform <- function(x,y){

    ## create wordbook
    wb <- createWorkbook()
    x<-as.matrix(x)
     y<-as.matrix(y)
    ## create work sheets   
    for (i in 1:length(x[,1])){
        if(isTRUE(x[i,1] %in% y[,1])){  
            addWorksheet(wb, x[i,1])
            for (j in 1:length(y[,1])){
            if(isTRUE(x[i,1] == y[j,1])){
    ##editing the sheet 
            writeData(wb, x[i,1], y[j,2], startCol = 8, startRow = 6,rowNames = TRUE )
            }
        }
}
}
    ## save file

    saveWorkbook(wb, "marth/javi/supertest.xlsx", overwrite = TRUE)
    }
mimus
  • 53
  • 5