Sandhya Ghildiyal
Sandhya Ghildiyal

Reputation: 273

Function after installing the package giving error but not in the global environment in R

I have created a package, one of the function is below:

tab_std <- function(data, var, footer, numdig = 0, sorted = FALSE,masking, ...){
  var1 <- rlang::parse_expr(var)
  expss::var_lab(data[[var]]) <- ""
  if (sorted) {
    if(is.list(lst) & length(lst) == 0) {tab1 <- expss::cro_cpct(data[[var]]) %>% tab_sort_desc
    } else {
      tab1 <- expss::cro_cpct(data[[var]],lst) %>% tab_sort_desc
    }
  } else {
    if(is.list(lst) & length(lst) == 0) {tab1 <- expss::cro_cpct(data[[var1]])
    } else {
      tab1 <- expss::cro_cpct(data[[var]],lst)
    }
  }
  tab1 <- as.data.frame(tab1)
  tab1[which(tab1[,1]=="#Total cases"),1] <- Nstring
  setnames(tab1,"row_labels"," ")
  tab1[is.na(tab1)] <- 0
  tab1 <- tab1 %>%
    mutate(
      across(
        .cols = where(is.numeric),
        .fns = ~ round_half_up(.x,digits = numdig)))
  if (masking == TRUE) {
    mask_indices <- sapply(tab1, function(x) x[length(x)]< freq_mask) %>% which()
    tab1[-nrow(tab1), mask_indices] <- "--"
    tab1[-nrow(tab1), -c(1)] <- sapply(tab1[-nrow(tab1), -c(1)],function(x) ifelse(x=="--","--",paste(format(x,nsmall = numdig),"%",sep ="" )))

  }else{
    tab1[-nrow(tab1), -c(1)] <- sapply(tab1[-nrow(tab1), -c(1)],function(x) paste(format(x,nsmall = numdig),"%",sep ="" ))
  }

  if (Nstring_Position == "Top") {
    i <- match(Nstring, tab1[,1])
    tab1 <- rbind(tab1[i,], tab1[-i,])}
  if(missing(footer)){tab2 <- tab1 %>% flextable() %>% mercer_style()}
  else{tab2 <- tab1 %>% flextable() %>% add_footer_lines(footer) %>% mercer_style()}
  tab2
}

This function after installing package gives below error: Show in New Window Error in rep.default(data, nrows) : attempt to replicate an object of type 'closure'

error is in this step:

expss::cro_cpct(data[[var]], lst)

But if I run this function in the global environment it works fine. This function works well otherwise only when i inlcuded this in the package the error occurs. Is there anything i am missing in the package or in the function?

Upvotes: 1

Views: 107

Answers (1)

Gregory Demin
Gregory Demin

Reputation: 4846

I don't see 'lst' among arguments of your function. And I know that there is 'lst' function in the 'dplyr' package. And error message says that 'cro_cpct' tries to replicate function ("closure"). Perhaps, you forgot to add 'lst' to function arguments.

Upvotes: 1

Related Questions