Skip to content

Commit

Permalink
add reactable_to_df function to convert output to a data frame
Browse files Browse the repository at this point in the history
  • Loading branch information
elong0527 committed Dec 2, 2023
1 parent ed13780 commit a34da09
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions R/reactable2.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,34 @@ reactable2 <- function(data,
tbl
}
}

#' Convert reactable to a data frame
#'
#' @param x A `reactable` HTML widget
#'
#' @return A data frame
#'
reactable_to_df <- function(x){

# table data
tbl1 <- do.call(cbind, jsonlite::fromJSON(x$x$tag$attribs$data))

# table columns
columns <- x$x$tag$attribs$columns
tbl2 <- list()
for(i in seq_along(columns)){
if(! columns[[i]]$id %in% ".details"){
if(is.null(columns[[i]]$show)){
tbl2[[i]] <- unlist(columns[[i]])
}
}
}
tbl2 <- dplyr::bind_rows(tbl2)

# output
tbl <- data.frame(tbl1[, tbl2$id])

attr(tbl, "column_header") <- paste(tbl2$name, collapse = "|")

tbl
}

0 comments on commit a34da09

Please sign in to comment.