Home > Mobile >  Applying Dynamic Function Based on Logical Values in R
Applying Dynamic Function Based on Logical Values in R

Time:10-05

I'm using openxlsx and I get to this point:

ABC = c(TRUE,TRUE,TRUE,FALSE,...,FALSE)

if (ABC) {writeData(Worksheet, Sheet = "Sheet1","Yes Please",
        startCol = 4,
        startRow = 3)}

I need an Excel file to say "Yes Please" when the value is TRUE. Second value (TRUE) would correspond to column 5, row 3. Forth value (FALSE) would correspond to column 7, row 3 and etc.

I thought about using loop to change the column number but not sure how to loop within argument for writeData.

CodePudding user response:

Based on the condition, only the column number is incremented while the row is still the same. We can then use a simple hack using lapply.

lapply(1:length(ABC), function(x){
    if(ABC[x]) writeData(Worksheet, sheet = "Sheet1", "Yes Please", 
    startCol = 4   (x - 1), startRow = 3)
})

we use x to increment the column as well as an index for the vector ABC.

  • Related