r
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5KB

  1. #' Is this a reversible document?
  2. #'
  3. #' A function for testing is the file can be de-rendered. If not, un-knitting
  4. #' may be attempted with the `orig_chunkfile` or `orig_docx` files in [dedoc()].
  5. #'
  6. #' @param docx A path to a `.docx` file or an `rdocx` object produced by
  7. #' [officer::read_docx()]
  8. #' @return a logical value
  9. #' @export
  10. #' @examples
  11. #' is_redoc(redoc_example_docx())
  12. is_redoc <- function(docx) {
  13. docx <- to_docx(docx)
  14. codefile <- list.files(file.path(docx$package_dir, "redoc"),
  15. pattern = "\\codelist\\.yml$"
  16. )
  17. return(as.logical(length(codefile)))
  18. }
  19. #' @importFrom officer read_docx
  20. to_docx <- function(docx) {
  21. if (inherits(docx, "rdocx")) {
  22. return(docx)
  23. } else {
  24. return(read_docx(docx))
  25. }
  26. }
  27. assert_redoc <- function(docx) {
  28. if (!is_redoc(docx)) {
  29. stop(deparse(substitute(docx)), " is not a reversible document")
  30. }
  31. }
  32. #' Files for examples and testing
  33. #'
  34. #' @export
  35. #' @rdname redoc_examples
  36. #' @aliases redoc_examples
  37. #' @examples
  38. #' redoc_example_rmd()
  39. #' redoc_example_docx()
  40. #' redoc_example_edited_docx()
  41. redoc_example_rmd <- function() {
  42. system.file("examples", "example.Rmd", package = "redoc")
  43. }
  44. #' @export
  45. #' @rdname redoc_examples
  46. #' @aliases redoc_examples
  47. redoc_example_docx <- function() {
  48. system.file("examples", "example.docx", package = "redoc")
  49. }
  50. #' @export
  51. #' @rdname redoc_examples
  52. #' @aliases redoc_examples
  53. redoc_example_edited_docx <- function() {
  54. system.file("examples", "example-edited.docx", package = "redoc")
  55. }