0

I recently asked the question "Can I use group_map or group_walk to iteratively export results?", and the code was working perfectly, but as of today, it no longer works. I tried updating my packages and RStudio, and this did not work. The data are:

MiniType MiniAssay StandardAssay Freq
Assay1 neg neg 19
Assay1 neg pos 5
Assay1 pos neg 8
Assay1 pos pos 141
Assay2 neg neg 25
Assay2 neg pos 6
Assay2 pos neg 17
Assay2 pos pos 33
Assay3 neg neg 99
Assay3 neg pos 20
Assay3 pos neg 5
Assay3 pos pos 105

The code is:

check_create_dir <- function(the_dir) {
  if (!dir.exists(the_dir)) {
    dir.create(the_dir, recursive = TRUE) } #Creates a directory if it doesn't already exist
}
the_dir_ex <- "data_generated/epidata" #Name the new desired directory


check_create_dir(the_dir_ex) #Make the directory if it doesn't already exist

#Make function for the series of analyses
epi_analysis <- function(a_csv, the_dir){
  #Clean data
  dat2 <- a_csv  %>%
    select(c(MiniAssay, StandardAssay, Freq)) %>%
    pivot_wider(MiniAssay, names_from = StandardAssay, values_from = Freq) %>%
    remove_rownames %>% 
    column_to_rownames(var = "MiniAssay") %>% 
    as.matrix() 
  
  #Run tests
  rval <- epi.tests(dat2, conf.level = 0.95)
  rkappa <- epi.kappa(dat2)
  gwet <- gwet.ac1.table(dat2)
  gwet_ci <- gwet$coeff.ci #CI is presented as (###,###); needs to be cleaned to separate lwr and upr vals
  gwet_ci <- gsub('[\\(]', '', gwet_ci)#Removes ( from string
  gwet_ci <- gsub('[\\)]', '', gwet_ci)#Removes ) from string
  gwet_ci <- strsplit(gwet_ci, ",")#Splits string at , so that it becomes a list of 1 with 2 different characters
  kappa2 <- kappa2.table(dat2)
  kappa_ci <- kappa2$coeff.ci #CI is presented as (###,###); needs to be cleaned to separate lwr and upr vals
  kappa_ci <- gsub('[\\(]', '', kappa_ci)#Removes ( from string
  kappa_ci <- gsub('[\\)]', '', kappa_ci)#Removes ) from string
  kappa_ci <- strsplit(kappa_ci, ",")#Splits string at , so that it becomes a list of 1 with 2 different characters 
  
  #Export results
  hd <- c('sensitivity', 'specificity','ppv','npv', 'pfp', 'pfn', 'kappa', 'gwet', 'pabak')
  
  ests <- c(round(rval$elements$sensitivity$est, digits = 3), 
            round(rval$elements$specificity$est, digits = 3),
            round(rval$elements$ppv, digits = 3), 
            round(rval$elements$npv, digits = 3), 
            round(rval$element$pfp$est, digits = 3), 
            round(rval$element$pfn$est, digits = 3), 
            round(kappa2$coeff.val, digits = 3), 
            round(gwet$coeff.val, digits = 3), 
            round(rkappa$pabak$est, digits = 3))
  
  ci_lwr <- c(round(rval$elements$sensitivity$lower, digits = 3), 
              round(rval$elements$specificity$lower, digits = 3),
              round(rval$elements$ppv.low , digits = 3), 
              round(rval$elements$npv.low, digits = 3), 
              round(rval$element$pfp$lower, digits = 3),  
              round(rval$element$pfn$lower, digits = 3), 
              kappa_ci[[1]][1], 
              gwet_ci[[1]][1],
              round(rkappa$pabak$lower, digits = 3))
  
  ci_upr <- c(round(rval$elements$sensitivity$upper, digits = 3), 
              round(rval$elements$specificity$upper, digits = 3),
              round(rval$elements$ppv.up , digits = 3), 
              round(rval$elements$npv.up, digits = 3), 
              round(rval$element$pfp$upper, digits = 3),  
              round(rval$element$pfn$upper, digits = 3), 
              kappa_ci[[1]][2], 
              gwet_ci[[1]][2], 
              round(rkappa$pabak$upper, digits = 3))
  
  write.csv(df, 
            file = sprintf('%s/%s.csv', the_dir, a_csv$MiniType[1]),
            na = "999.99", 
            row.names = FALSE)
  
}

When I try to execute the functions:

#Execute functions
data <- read_csv("data_raw/EpiData.csv") %>%
  group_by(MiniType)%>%
  group_map(~ epi_analysis(., the_dir_ex), .keep = TRUE)

I get:

 Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"function"’ to a data.frame 
16.
stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])), 
    domain = NA) 
15.
as.data.frame.default(x[[i]], optional = TRUE) 
14.
as.data.frame(x[[i]], optional = TRUE) 
13.
data.frame(x) 
12.
write.table(df, file = sprintf("%s/%s.csv", the_dir, a_csv$MiniType[1]), 
    na = "999.99", row.names = FALSE, col.names = TRUE, sep = ",", 
    dec = ".", qmethod = "double") 
11.
eval(expr, p) 
10.
eval(expr, p) 
9.
eval.parent(Call) 
8.
write.csv(df, file = sprintf("%s/%s.csv", the_dir, a_csv$MiniType[1]), 
    na = "999.99", row.names = FALSE) 
7.
epi_analysis(., the_dir_ex) 
6.
(structure(function (..., .x = ..1, .y = ..2, . = ..1) 
epi_analysis(., the_dir_ex), class = c("rlang_lambda_function", 
"function")))(dots[[1L]][[1L]], dots[[2L]][[1L]]) 
5.
mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE) 
4.
map2(chunks, group_keys, .f, ...) 
3.
group_map.data.frame(., ~epi_analysis(., the_dir_ex), .keep = TRUE) 
2.
group_map(., ~epi_analysis(., the_dir_ex), .keep = TRUE) 
1.
read_csv("data_raw/EpiData.csv") %>% group_by(MiniType) %>% group_map(~epi_analysis(., 
    the_dir_ex), .keep = TRUE) 

If I try to execute the functions with:

#ALTERNATE EXECUTE FUNCTION (AS AN EXAMPLE)
read_csv("data_raw/EpiData.csv") %>%
  group_split(MiniType) %>%
  map(~ epi_analysis(.x, the_dir_ex))

This had also worked previosly just fine, but now I get:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"function"’ to a data.frame 
13.
stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])), 
    domain = NA) 
12.
as.data.frame.default(x[[i]], optional = TRUE) 
11.
as.data.frame(x[[i]], optional = TRUE) 
10.
data.frame(x) 
9.
write.table(df, file = sprintf("%s/%s.csv", the_dir, a_csv$MiniType[1]), 
    na = "999.99", row.names = FALSE, col.names = TRUE, sep = ",", 
    dec = ".", qmethod = "double") 
8.
eval(expr, p) 
7.
eval(expr, p) 
6.
eval.parent(Call) 
5.
write.csv(df, file = sprintf("%s/%s.csv", the_dir, a_csv$MiniType[1]), 
    na = "999.99", row.names = FALSE) 
4.
epi_analysis(.x, the_dir_ex) 
3.
.f(.x[[i]], ...) 
2.
map(., ~epi_analysis(.x, the_dir_ex)) 
1.
read_csv("data_raw/EpiData.csv") %>% group_split(MiniType) %>% 
    map(~epi_analysis(.x, the_dir_ex)) 

I am operating a PC with Windows 10, and R v4.0.3 and RStudio v1.4.1717

I need to get this code working again, any help is appreciated.

MBell
  • 57
  • 1
  • 8

1 Answers1

2

The function is missing dataframe creation line. Try this function -

epi_analysis <- function(a_csv, the_dir){
  #....
  #....
  #....
  #....
  hd <- c(....)
  ests <- c(....)
  ci_lwr <- c(....)
  ci_upr <- c(....)
  
  
  #Add this line
  df <- data.frame(hd, ests, ci_lwr, ci_upr)
  
  write.csv(df, 
            file = sprintf('%s/%s.csv', the_dir, a_csv$MiniType[1]),
            na = "999.99", 
            row.names = FALSE)
  
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213