Fix bug when r not array

Flatten only applies when r is an array.