Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a MS Word Comment via Rmarkdown

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.

like image 694
ldecicco Avatar asked Sep 11 '19 15:09

ldecicco


People also ask

How do I add comments to R Markdown?

After drag the lines you want to make comment, press SHIFT + CMD + C (macOS), SHIFT + CTRL + C (Windows). This is the shortcut of R Markdown editor (R Studio) to comment out. For me it was Ctrl+ Shift + C.

How do you add a comment to Word?

Insert a comment Select the text you want to comment on, or click at the end of the text. On the Review tab, click New Comment. Type your comment. Word shows your comment in a balloon in the document's margin.


4 Answers

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.

like image 101
JBGruber Avatar answered Oct 22 '22 21:10

JBGruber


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")`
like image 25
taffel Avatar answered Oct 22 '22 19:10

taffel


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

like image 39
Orlando Sabogal Avatar answered Oct 22 '22 20:10

Orlando Sabogal


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
      )
    )
  }
}
like image 1
Robert Payn Avatar answered Oct 22 '22 20:10

Robert Payn