[R] Help with use RMarkdown and knitr in an rdm output to word.doc

Bill Poling B|||@Po||ng @end|ng |rom ze||@@com
Sat Apr 6 23:44:55 CEST 2019


Thank you Jeff, I am so darn close, I solve one problem and another emerges!

However, I realized that back in July when I made my first and, up until this weekend, only attempt at this I was following the original url that was reposted by R-Bloggers that I mentioned in my original post earlier.
#https://www.datasurg.net/2018/05/16/elegant-regression-results-tables-and-plots-the-finalfit-package/

Low and behold realized I had asked the author (Ewen Harrison with DataSurg) these questions back then in the comments, UGH!
But since I ditched the idea in frustrationback then and I did not follow-up I hadn't realized the author created a companion url for this very topic, how to get from .Rmd to word/PDF etc..

I located his companion reference url to the original.

#https://www.datasurg.net/2018/05/22/finalfit-knitr-and-r-markdown-for-quick-results/

So following his further instructions I have made more progress, however, as I mention above the final document remains elusive.

SO I did some further googling and perused these sites as well

#https://rmarkdown.rstudio.com/articles_docx.html
#https://ourcodingclub.github.io/2016/11/24/rmarkdown-1.html

Between the companion url of Harrison and https://rmarkdown.rstudio.com/articles_docx.html I have almost got it.

---
title: "AutoDist Analysis"
author: "WHP"
date: "4/6/2018"
output:
    word_document:
    reference_docx: word-styles-reference-01.docx

---
```{r setup, include=FALSE}
# Load data into global environment.
library(finalfit)
library(dplyr)
library(knitr)
library(kableExtra)
load("C:/WHP/Revenue Development Products//BRA AutoDistribution/Test1.rda")
```

## Table 1 - Associations between Autodist Yes/No and other explanatory variables
```{r table1, echo = FALSE, results='asis'}
kable(table1, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"),
booktabs=TRUE)
```

## Table 2 - Logistic regression table
```{r table2, echo = FALSE, results='asis'}
kable(table2, row.names=FALSE, align=c("l", "l", "r", "r", "r", "r"),
booktabs=TRUE) %>%
kable_styling(font_size=8)
```

## Figure 1 - Odds Ratio Plot
```{r figure1, echo = FALSE, warning=FALSE, message=FALSE, fig.width=10}
tmp %>%
  or_plot(dependent, explanatory)

However, now the issue is:
"Error: Functions that produce HTML output found in document targeting docx output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
  always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
Execution halted"

This is confusing as heck, however, I tried adding that to the YAML but I get the same error
---
title: "AutoDist Analysis"
author: "WHP"
date: "4/6/2018"
output:
    word_document:
    reference_docx: word-styles-reference-01.docx
    always_allow_html: yes
---

SO needless to say  am really disappointed!

Whole Saturday on this, sheesh!

Thanks for listening

WHP


From: Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
Sent: Saturday, April 6, 2019 2:25 PM
To: Bill Poling <Bill.Poling using zelis.com>; r-help using r-project.org; r-help (r-help using r-project.org) <r-help using r-project.org>
Subject: RE: [R] Help with use RMarkdown and knitr in an rdm output to word.doc

Maybe you need to format the data frame as markdown before it gets displayed.

tmp %>%
summary_factorlist(dependent, explanatory, p=TRUE, add_dependent_label=TRUE) %>%
knitr::kable()

You can also tune how the columns are formatted a bit with arguments to the kable function.

On April 6, 2019 10:58:53 AM PDT, Bill Poling <mailto:Bill.Poling using zelis.com> wrote:
>Hello Jeff,as always, thank you for your response.
>
>Yes .Rmd, and here I thought I was being so thorough about providing as
>much detail as possible, my apologies.
>
>I have an open .Rmd file from the File ->New File menu in RStudio, so I
>think that part is correct.
>
>My code is this.
>---
>title: "AutoDist Analysis"
>author: "WHP"
>date: "April 6, 2019"
>output:
> word_document: default
> ---
>
>```{r ElegantRegress, include = FALSE}
>knitr::opts_chunk$set(echo = FALSE)
>```
>```{r global_options, include=FALSE}
>knitr::opts_chunk$set(echo=FALSE, warning=FALSE,
>message=FALSE,align=c("l", "l", "r", "r", "r", "r"))
>```
>
>```{r message=FALSE,warning=FALSE}
>library(finalfit)
>library(dplyr)
>library(knitr)
>library(memisc)
>load("C:/WHP/Revenue Development Products//BRA
>AutoDistribution/Autodist RegTests V1.RData")
>setwd("C:/WHP/Revenue Development Products/BRA AutoDistribution")
>```
>
>```{r echo=FALSE, results='asis'}
>explanatory = c("TBCat","ClaimType","drgcode2","typeofbillid2")
>dependent = "Autodist2"
>tmp %>% summary_factorlist(dependent, explanatory, p=TRUE,
>add_dependent_label=TRUE)
>```
>
>I do get a word.doc file output from the above code (attachment did
>not survive the communication as you mentioned) however, it is not in
>the format described in the tutorial.
>Here is the output I do get:
>
>AutoDist Analysis
>WHP
>
>April 6, 2019
>
>Dependent: Autodist2 No Yes p 5 TBCat 1 6257 (96.6) 222 (3.4) <0.001 6
>2 5780 (89.1) 709 (10.9)
>7 3 5424 (83.9) 1042 (16.1)
>8 4 5324 (82.2) 1156 (17.8)
>9 5 5120 (79.0) 1360 (21.0)
>10 6 5158 (79.6) 1322 (20.4)
>11 7 5022 (77.5) 1459 (22.5)
>12 8 5081 (78.4) 1398 (21.6)
>13 9 4905 (75.7) 1575 (24.3)
>14 10 5011 (77.3) 1469 (22.7)
>1 ClaimType 1 48113 (80.6) 11566 (19.4) <0.001 2 2 4969 (97.1) 146
>(2.9)
>3 drgcode2 FALSE 41015 (80.8) 9734 (19.2) <0.001 4 TRUE 12067 (85.9)
>1978 (14.1)
>15 typeofbillid2 FALSE 4560 (90.3) 490 (9.7) <0.001 16 TRUE 48522
>(81.2) 11222 (18.8)
>
>I will revisit the links you provide once again.
>I have tried to find my problem solution in these earlier.
>Not surprisingly given the breadth and depth of information these links
>provide I have probably overlooked something nuanced yet highly
>important to the formatting issue I am having.
>
>Once again thank you for your response Sir.
>
>WHP
>
>
>From: Jeff Newmiller <mailto:jdnewmil using dcn.davis.ca.us>
>Sent: Saturday, April 6, 2019 10:42 AM
>To: mailto:r-help using r-project.org; Bill Poling <mailto:Bill.Poling using zelis.com>; r-help
>(mailto:r-help using r-project.org) <mailto:r-help using r-project.org>
>Subject: Re: [R] Help with use RMarkdown and knitr in an rdm output to
>word.doc
>
>No surprise, the attachments did not come through. There are a limited
>few MIME types that are allowed, but most email programs don't let you
>have direct control over that, making embedding your entire
>reproducible plain-text example in the main body of the email the most
>sure path to successful communication on this mailing list.
>
>I did skim through what you wrote below, scratching my head over what
>"rdm" was, until it dawned on me that you might mean a file with an
>".Rmd" extension (are-mark-down) and that failing to use the correct
>extension might have something to do with your failure.
>
>Anyway, the topic on this mailing list is the R language, and
>contributed packages are (according to the Posting Guide) technically
>supposed to have support resources of their own [2] listed in their
>DESCRIPTION files [1]. I think the "how it works" discussion there
>could be useful in clarifying which other packages rmarkdown is using
>and which functions you might find useful documentation about. They
>also have a community forum over there I think.
>
>FWIW my own experience with rmarkdown has been that that most of its
>power comes from augmenting it with other packages, and those packages
>are often very specific to which final output format you are using. The
>Word output format is much less extensively supported than HTML or PDF
>(via LaTeX) are. Don't fall into the trap of using HTML or LaTeX
>features when generating Word output.
>
>[1] https://cran.r-project.org/web/packages/rmarkdown/index.html
>[2] https://rmarkdown.rstudio.com
>
>On April 6, 2019 6:42:01 AM PDT, Bill Poling
><mailto:Bill.Poling using zelis.com> wrote:
>>Hello:
>>
>>#sessionInfo()
>>#R version 3.5.3 (2019-03-11)
>>#Platform: x86_64-w64-mingw32/x64 (64-bit)
>>#Running under: Windows >= 8 x64 (build 9200)
>>
>>#I have been struggling with learning how to use RMarkdown and knitr
>in
>>an rdm while following this tutorial using my own data.
>>#I Tried many months ago to self-teach but it drove me nuts, however,
>I
>>am now at a point where it is essential I get this figured out.
>>
>>#The tutorial
>>#https://www.r-bloggers.com/elegant-regression-results-tables-and-plots-in-r-the-finalfit-package/
>>
>>#I have googled and googled but cannot solve the problem of "how to
>get
>>my rdm output" word doc to look like the tutorial examples.
>>
>>#Many googles
>>#https://stackoverflow.com/questions/20060370/in-rstudio-is-there-a-way-to-specify-a-fig-path-for-all-figures-for-this-file
>>#https://rstudio.github.io/distill/figures.html
>>#https://kbroman.org/knitr_knutshell/pages/Rmarkdown.html
>>
>>#And the list goes on. UGH!
>>
>>#I attach my rmd code and the word doc output, hopefully they get
>>through. Please let me know, and if not how I might improvise please.
>>
>>#Here is the basic procedure from the tutorial which runs fine into
>the
>>console, however, I am trying to output it with the suggested
>>formatting in the tutorial
>>explanatory = c("TBCat","ClaimType","drgcode2","typeofbillid2")
>>dependent = "Autodist2"
>>tmp %>%
>>summary_factorlist(dependent, explanatory, p=TRUE,
>>add_dependent_label=TRUE)
>>
>>#Here is a sample of my data.
>>
>>#I am sure there are also basic formatting inconsistencies or
>>redundancies in the rdm and would appreciate any advice or suggestion
>>for that as well, happy Saturday.
>>
>>sample <- tmp %>% slice(1:35)
>>> dput(sample)
>>structure(list(Autodist2 = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("No",
>>"Yes"), class = "factor"), TBCat = structure(c(4L, 10L, 6L, 8L,
>>9L, 4L, 6L, 8L, 4L, 8L, 5L, 10L, 9L, 6L, 7L, 4L, 3L, 8L, 7L,
>>4L, 10L, 7L, 8L, 8L, 7L, 4L, 10L, 9L, 3L, 4L, 9L, 8L, 5L, 2L,
>>8L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
>>"10"), class = "factor"), RestictedPayorID = structure(c(2L,
>>2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L,
>>2L, 2L), .Label = c("FALSE", "TRUE"), class = "factor"), ClaimType =
>>structure(c(1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L), .Label = c("1", "2"), class = "factor"),
>>ClaimStatus_Non_Acceptance = structure(c(1L,
>>1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L,
>>1L, 1L), .Label = c("0", "1"), class = "factor"), ClaimStatus_Accepted
>>= structure(c(2L,
>>2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
>>2L, 2L), .Label = c("0", "1"), class = "factor"), ClaimManagerID3 =
>>structure(c(2L,
>>1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"),
>>ClaimStatus_In_Process = structure(c(1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L), .Label = c("0", "1"), class = "factor"), Appeals2 =
>>structure(c(2L,
>>2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>2L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"), drgcode2 =
>>structure(c(1L,
>>1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L,
>>2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L,
>>1L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"), edi2 =
>>structure(c(1L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
>>2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
>>2L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"), typeofbillid2
>>= structure(c(2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L), .Label = c("FALSE", "TRUE"), class = "factor"),
>>ExternalNetworkID2 = structure(c(1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>1L, 1L), .Label = c("FALSE", "TRUE"), class = "factor"),
>>AdjustmentType2 = structure(c(2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L), .Label = c("FALSE", "TRUE"), class = "factor"),
>>CustomEOPLanguage2 = structure(c(2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
>>2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
>>2L, 2L), .Label = c("FALSE", "TRUE"), class = "factor")), row.names =
>>c(NA,
>>-35L), class = "data.frame")
>>
>>
>>
>>Confidentiality Notice This message is sent from Zelis. This
>>transmission may contain information which is privileged and
>>confidential and is intended for the personal and confidential use of
>>the named recipient only. Such information may be protected by
>>applicable State and Federal laws from this disclosure or unauthorized
>>use. If the reader of this message is not the intended recipient, or
>>the employee or agent responsible for delivering the message to the
>>intended recipient, you are hereby notified that any disclosure,
>>review, discussion, copying, or taking any action in reliance on the
>>contents of this transmission is strictly prohibited. If you have
>>received this transmission in error, please contact the sender
>>immediately. Zelis, 2018.
>>______________________________________________
>>mailto:R-help using r-project.org mailing list -- To UNSUBSCRIBE and more,
>see
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide
>>http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>
>--
>Sent from my phone. Please excuse my brevity.
>
>Confidentiality Notice This message is sent from Zelis. This
>transmission may contain information which is privileged and
>confidential and is intended for the personal and confidential use of
>the named recipient only. Such information may be protected by
>applicable State and Federal laws from this disclosure or unauthorized
>use. If the reader of this message is not the intended recipient, or
>the employee or agent responsible for delivering the message to the
>intended recipient, you are hereby notified that any disclosure,
>review, discussion, copying, or taking any action in reliance on the
>contents of this transmission is strictly prohibited. If you have
>received this transmission in error, please contact the sender
>immediately. Zelis, 2018.

--
Sent from my phone. Please excuse my brevity.

Confidentiality Notice This message is sent from Zelis. This transmission may contain information which is privileged and confidential and is intended for the personal and confidential use of the named recipient only. Such information may be protected by applicable State and Federal laws from this disclosure or unauthorized use. If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, review, discussion, copying, or taking any action in reliance on the contents of this transmission is strictly prohibited. If you have received this transmission in error, please contact the sender immediately. Zelis, 2018.


More information about the R-help mailing list