I add some trivial modifications on the code :
result <- numeric()
for(i in 1 : nrow(test)){
if (test$aa>4 && test$bb>4 && test$cc>4 && test$dd>4)
result<-chisq.test(matrix(unlist(test[i,]),ncol=2,byrow=T),correct=F)$p.value
else
result<-fisher.test(matrix(unlist(test[i,]),ncol=2,byrow=T))$p.value
}
# And result is
> result
[1] 1.785716e-03 5.076277e-07 3.377813e-06 1.534920e-06 6.445038e-14
[6] 2.820388e-03