0

I have the following code (Note don't flame me it's not my code I'm fixing a issue that I discovered in modification to this code.)

<!--begin.rcode echo=FALSE, results='asis', message=FALSE
overLevel <- 0
step <- 10

for(i in seq(1, nrow(exposures), by=step)) {
  tryCatch({
    tmp <- exposures[i:(i+step-1),]
    cols <- ifelse( (as.numeric(gsub(",","",gsub(" ","",tmp$ARExcess)))) >  overLevel, 'tomato', 
                 ifelse(as.numeric(gsub(",","",gsub(" ","",tmp$MTMExcess))) > overLevel, 'lightyellow', 
                 ifelse(as.numeric(gsub(",","",gsub(" ","",tmp$VolumeExcess))) > overLevel, 'azure3', 'white')))

    cat(paste0('<h4><u>Exposures</u></h4>'))
    cat(htmlTable(as.matrix(tmp), col.rgroup = cols, rnames = FALSE))
  },error = function(e) {print(e)})
}

end.rcode-->

The problem I'm having is it's taking 10 items at a time(the for line in the code) from the excesses dataset and putting them into a tmp dataset 10 records at a time. When there is less than 10 records it drops and does not add the additional items to the RHTML file either resulting in a incomplete report or no report depending on records.
How would I be able to complete all records no matter if there are 10 or not. It needs to be in 10 chuck size for the readability in email (business rule) so I'm OK with it being 10 then on the last one being shorter but don't know how to do the last part. Does anyone know how I can fix this or lend a helping hand?

Sc-python-leaner
  • 259
  • 1
  • 2
  • 13
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Without all the variables defined, we can't run the code. – MrFlick Jul 02 '18 at 14:26
  • In addition to above comment this is an [XY Problem](https://meta.stackexchange.com/a/66378) where you present to us your *Y* solution but don't tell us the *X* problem. What overall is your problem and goal? Step back from your code and give us background with reproducible sample data. It might be you do not need to go about this per 10 loop approach which is unclear why you choose this strategy. – Parfait Jul 02 '18 at 14:48
  • Sorry, if I presented this as an XY problem. I'm extrememly new to R, the solution would be to have the data come in the html in blocks of 10 and if the tmp table after x loop has less than 10 records to get those records to print. There are NAs in the dataset which I'm believe is the problem and maybe it's waiting on those last 2 records to be something. – Sc-python-leaner Jul 02 '18 at 15:52

1 Answers1

1

In your code you use tmp <- exposures[i:(i+step-1),] but i+step-1 can extend beyond the number of rows. This can give NAs in tmp. For example (using mtcars)

> dim(mtcars)
[1] 32 11
> mtcars[30:34,]
               mpg cyl disp  hp drat   wt qsec vs am gear carb
Ferrari Dino  19.7   6  145 175 3.62 2.77 15.5  0  1    5    6
Maserati Bora 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
Volvo 142E    21.4   4  121 109 4.11 2.78 18.6  1  1    4    2
NA              NA  NA   NA  NA   NA   NA   NA NA NA   NA   NA
NA.1            NA  NA   NA  NA   NA   NA   NA NA NA   NA   NA

Note that since I tried to grab rows beyond the dimensions of the data it just returns NA. This ultimately leads to problems in that you then use this to determine cols which can lead to NAs in cols. htmlTable seems to handle NAs in the x parameter just fine but gives an error when NAs are passed in to the col.rgroup parameter.

The easiest solution would be to make sure that you aren't grabbing anything past the end of the data. There are surely better ways to go about this but a solution that requires the smallest change to your code would be to replace the problem line with

tmp <- exposures[seq(i, min(nrow(exposures), i+step-1)),]

which will make sure you aren't trying to grab rows beyond the end of your data.

As mentioned in the comments there is surely a better way to attack this problem and I think a refactoring could be in order but I still wanted to help understand why this was happening so you can hopefully avoid (or detect) these kinds of traps in the future.

Dason
  • 60,663
  • 9
  • 131
  • 148
  • What would a different solution be? the code above returned a basically blank html page It's like to get some assistance if available.. Any examples I could look at? – Sc-python-leaner Jul 02 '18 at 15:37
  • Sorry, I get the header on the html but I don't get any results from the data list. Also the code above seems to be missing a ) so I put it after exposures so it looked like this now. there were 3 open and 2 closed tmp <- exposures[seq(i, min(nrows(exposures)), i+step-1), ] – Sc-python-leaner Jul 02 '18 at 15:47
  • Tried that so it should look this this. tmp <- exposures[seq(i, min(nrows(exposures), i+step-1)),] if so it still produces an empty page except the info in the html. – Sc-python-leaner Jul 02 '18 at 16:05
  • Dason, thank you for this, Perfect exactly what I wanted. – Sc-python-leaner Jul 02 '18 at 16:16