Add a MS Word Comment via Rmarkdown
Asked Answered
S

4

10

Is there a way to add a MS Word "Comment" via a R markdown file? I'm using a reference_docx, and familiar with adding custom styles...but haven't figured out how to get a comment to show up on the side like this:

enter image description here

To clarify: I want to add a tag (or something?) to my plaintext Rmd file, so that when I "knit" the resulting MS Word doc has a rendered comment.

Serrano answered 11/9, 2019 at 15:29 Comment(0)
E
12

Actually, this is possible. Yes, Markdown (and RMarkdown) are for plain text writing, but they are translated using pandoc. So I googled this and found the following code, which works well:

---
title: "test"
output:
  word_document: default
---

This text contains a [This is the comment]{.comment-start id="0" author="Johannes G." date="2020-01-13T10:12:00Z"}comment.[]{.comment-end id="0"}.

enter image description here

This will create a bit of a mess when knitting to other formats, so you might want to think about using an R functions instead:

```{r echo=FALSE}
word_comment <- function(comment, highlight = "") {
  if (isTRUE(knitr:::pandoc_to() == "docx")) {
    paste0('[', comment, ']{.comment-start id="0" author="Johannes G."',
           'date="2020-01-13T10:12:00Z"}', highlight, '[]{.comment-end id="0"}')
  }
}
```

This text contains a `r word_comment("This is the comment", "comment.")`.

The code can probably be improved but I couldn't find documentation for the chunk that creates the comment so this works well enough for the moment.

Erigeron answered 13/1, 2021 at 18:3 Comment(0)
D
6

I want to expand the nice answer by @JBGruber:

This code will work for html and word as output:

```{css, echo=FALSE}
span.comment-start{
    background: #e0f3db;
}
span.comment-end{
    background: #e0f3db;
}
span.comment-start::after{
    content: " (" attr(author) ", " attr(date) ") [";
}
span.comment-end::before{
    content: "]"
}
span.comment-text{
    background: #fdbb84;
}

```

```{r, echo=FALSE}
commentN <- 0
cmt <- function(txt, cm, author, date = "2021-01-01", comN = commentN){
  cmt_str <- paste0('<span class="comment-text">[',cm,']{.comment-start id="', comN, '" author="', author, '" date="', date, '"}', txt, '[]{.comment-end id="',commentN,'"}</span>')
  assign("commentN", commentN + 1, envir = .GlobalEnv)
  return(cmt_str)
}
```

and you can add comments in your text by calling

`r cmt("Text to be commented", "The comment itself", "myName", "Date")`
Diptych answered 24/1, 2021 at 17:25 Comment(2)
Great thank you ! Any idea of how to put several comments on the same highlighted text ?Hamil
Shouldn't it be cmt_str <- paste0('<span class="comment-text">[',cm,']{.comment-start id="', comN, '" author="', author, '" date="', date, '"}', txt, '[]{.comment-end id="',comN,'"}</span>') Instead ? In case comN is defined by the userHamil
M
2

Loved JBGruber's function. Tweaked it to generate a legible in-line generic comment format when compiling to other file types.

wordComment <- function(
  comment, 
  highlight = "", 
  author = "Rob", 
  time = "1970-01-01T00:00:00Z",
  id = "0"
) 
{
  if (isTRUE(knitr:::pandoc_to() == "docx")) {
    return(
      sprintf(
        '[%s]{.comment-start id="%s" author="%s" date="%s"} %s []{.comment-end id="%s"}',
        comment,
        id,
        author,
        time,
        highlight,
        id
      )
    )
  } else {
    return(
      sprintf(
        "*%s* **[Comment id %s by %s at time %s: %s]**", 
        highlight,
        id,
        author, 
        time, 
        comment
      )
    )
  }
}
Maestas answered 22/6, 2022 at 17:27 Comment(3)
Works great. Can be called inline; e.g.: The moment of r wordComment(highlight = "truth.", comment = "Bla bla bla.", author = "me").Casebook
I found that this works in quarto as well and the id field does not need to be updated (they are all 0 for me but it looks fine in the document). I was also able to replace time with Sys.time() and the comment shows up as the day you created the document at midnight.Cavour
Yeah, Sys.time() is probably the better default value. Should have thought of that.Maestas
P
1

Markdown (and RMarkdown) are for plain text writing. Therefore, a you cannot add a comment as in word (and enable to pop up in some place in the screen).

Nevertheless, you can add plain text comments in RMarkdown that, after rendering to Docx, you can see in word as a normal word comment (it also works from Word to RMarkdown).

For the detail see the redoc library

Prolate answered 11/9, 2019 at 15:49 Comment(4)
The "Nevertheless" sentence is what I am asking how to do. I've been playing with redoc and haven't figured out how to make a comment (the tracking changes is great)....I'll keep poking around with redoc though, I'm probably just missing something.Serrano
Try {>> <<} and see https://github.com/CriticMarkup/CriticMarkup-toolkit/Prolate
"Markdown (and RMarkdown) are for plain text writing." - I strongly disagree. RMarkdown is for preparing documents with plain text, but also tables, figures, etc., and other ways of presenting results generated by R. Thus, it is very flexible. There's no fundamental reason why specifying a comment should not work. Except that nobody has implemented it so far.Family
I am pretty sure it is possible to have an implementation where users can add popuup comments and many other features. Yet, that is not the purpose of markdown. Yes, markdowns was cretaed to elabotrate (even very sophisticated) documents and Rmarkdown enables to do it including reproducible code. But also, the idea is to have a cleaner interface with basic text, not a enriched document. Therefore, I do not see a fit with popup comments.Prolate

© 2022 - 2024 — McMap. All rights reserved.