0

I have successfully uploaded R package in CRAN. Now I want to publish its article. Reviewer 2 returned the paper due to memory usage issues. Now I am confused about this objection, how to solve this. Generally, my package is about density plots and I create S3 class for each style of plot. Almost each function used 2 for loops otherwise code is not too long. But according to Advanced R for loops are slow. So what is best alternative? Chunk of Code

xxx<-function(y,k,h){
n<-length(y)
 x <- seq(min(y) + 0.05, max(y), length =k)
 K <- matrix(rep(0, k * n), ncol = k)
 fhat <- rep(0, k)
###########Loops###########
for(j in 1:k) {
for(i in 1:n) {
####main code###
}
fhat[j] <- 1/n * (sum(K[, j]))
}
results <- list(x=x, y=fhat)

class ( results ) <-c('list', 'XXX')
results
}

k is grid point and defined by user. What is the problem and how to solve this? Additionally, they wrote code writing style is not too good; How I can improve this?

Angel
  • 184
  • 1
  • 14
  • 1
    Memory usage issues is not specific enough. Your code example is not sufficient to diagnose this. – Roland Oct 10 '20 at 06:27
  • @Roland rest of code consists of S3 class n function. For detail see my edit. – Angel Oct 11 '20 at 09:33
  • I don't know the content of the inner loop but `fhat[j] <- 1/n * (sum(K[, j]))` doesn't need a loop. You could use `colSums` instead. However, performance and memory issues are distinct issues. You get into troubles with memory if `k` and `n` get large. – Roland Oct 13 '20 at 07:39
  • @Roland sorry, I didn't get your point. What is problematic for performance if I continuously using fhat[j]? Further, can you please give me tips for making my code better.... – Angel Oct 17 '20 at 04:55
  • Is colSums() is good choice thn fhat[j] specially for R package? – Angel Oct 17 '20 at 05:05

0 Answers0