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.

89 lines
2.9KB

  1. #' RStudio Addin Functions
  2. #'
  3. #' @description
  4. #' These functions act on the RStudio text editor and can be used via the
  5. #' Addins menu or the R console. `rountrip_active_file()` ("Render and Update")
  6. #' knits the current file using [redoc()] replaces the text with text that has
  7. #' been round-tripped to Word and back. This helps eliminate small formatting
  8. #' differences.
  9. #'
  10. #' The `dedoc_*` functions de-render Word documents (using [dedoc()]),
  11. #' and place the results in the current or new file. By default, they will
  12. #' also display a diff (generated via [redoc_diff()]) of edits in the word file
  13. #' compared to its original version a compilation time. If called without
  14. #' an input file, they will prompt for file selection via the RStudio GUI.
  15. #'
  16. #'
  17. #' @param docx The input Word file, originally generated by [redoc()]
  18. #' @param showdiff Display a diff of the current version of the document against
  19. #' the rendered version?
  20. #' @aliases addins redoc_addins
  21. #' @rdname addins
  22. #' @export
  23. roundtrip_active_file <- function() {
  24. if (!requireNamespace("rstudioapi")) {
  25. stop("The 'rstudioapi' package is required for this function")
  26. }
  27. active_file <- rstudioapi::getSourceEditorContext()
  28. cursor_position <- active_file$selection[[1]]$range$start
  29. rstudioapi::documentSave(active_file$id)
  30. docfile <- rmarkdown::render(
  31. normalizePath(active_file$path),
  32. output_format = redoc(roundtrip = TRUE),
  33. quiet = TRUE,
  34. clean = TRUE
  35. )
  36. rfile <- redoc_extract_rmd(docfile,
  37. type = "roundtrip", dir = tempdir(),
  38. overwrite = TRUE
  39. )
  40. rstudioapi::setDocumentContents(readfile(rfile), active_file$id)
  41. rstudioapi::setCursorPosition(cursor_position, active_file$id)
  42. }
  43. #' @export
  44. #' @rdname addins
  45. dedoc_to_active_file <- function(docx = NULL, showdiff = TRUE) {
  46. if (!requireNamespace("rstudioapi")) {
  47. stop("The 'rstudioapi' package is required for this function")
  48. }
  49. active_file <- rstudioapi::getSourceEditorContext()
  50. cursor_position <- active_file$selection[[1]]$range$start
  51. if (is.null(docx)) {
  52. docx <- rstudioapi::selectFile(
  53. caption = "Select Word file to dedoc",
  54. filter = "Word Files (*.docx)"
  55. )
  56. }
  57. tmprmd <- dedoc(docx, dir = tempdir(), overwrite = TRUE)
  58. rstudioapi::setDocumentContents(readfile(tmprmd), active_file$id)
  59. rstudioapi::setCursorPosition(cursor_position, active_file$id)
  60. if (showdiff) {
  61. print(
  62. redoc_diff(docx)
  63. )
  64. }
  65. }
  66. #' @export
  67. #' @rdname addins
  68. dedoc_to_new_file <- function(docx = NULL, showdiff = TRUE) {
  69. if (!requireNamespace("rstudioapi")) {
  70. stop("The 'rstudioapi' package is required for this function")
  71. }
  72. if (is.null(docx)) {
  73. docx <- rstudioapi::selectFile(
  74. caption = "Select Word file to dedoc",
  75. filter = "Word Files (*.docx)"
  76. )
  77. }
  78. tmprmd <- dedoc(docx, dir = tempdir(), overwrite = TRUE)
  79. rstudioapi::documentNew(readfile(tmprmd), type = "rmarkdown")
  80. if (showdiff) {
  81. print(
  82. redoc_diff(docx)
  83. )
  84. }
  85. }