[R] Help with knitr pkg

Rainer Hurling rhur||n @end|ng |rom gwdg@de
Thu Jul 19 09:44:33 CEST 2018


Hi Bill,

Am 18.07.18 um 18:33 schrieb Bill Poling:
> Hi,
> I worked through this excellent tutorial:
> #Elegant regression results tables and plots in R: the finalfit package
> https://www.r-bloggers.com/elegant-regression-results-tables-and-plots-in-r-the-finalfit-package/
> 
> 
> 
> Now I am applying it to my own data.
> 
> In the tutorial there is mention of:
> 
> # Tables can be knitted to PDF, Word or html documents. We do this in
> # RStudio from a .Rmd document. Example chunk:
> #   ```{r, echo = FALSE, results='asis'}
> # knitr::kable(example_table, row.names=FALSE,
> #              align=c("l", "l", "r", "r", "r", "r"))
> # ```
> 
> I am having a difficult time understanding how this works?
> 
> I have read through the help:
> 
> ?knitr
> #"This function takes an input file, extracts the R code in it according to a list of patterns, evaluates the code and writes the output in another file.
> #It can also tangle R source code from the input document (purl() is a wrapper to knit(..., tangle = TRUE)).
> #The knitr.purl.inline option can be used to also tangle the code of inline expressions (disabled by default)."
> 
> install.packages("knitr")
> library(knitr)
> ?knit
> ?stitch
> install.packages("stitch")#package 'stitch' is not available (for R version 3.5.1)
> ?spin
> install.packages("spin") #package 'spin' is not available (for R version 3.5.1)Warning in install.packages :  Perhaps you meant 'SPIn' ?
> 
> I have also looked at the github and knitr author's links
> 
> https://github.com/yihui/knitr
> 
> https://yihui.name/knitr/demo/stitch/
> 
> https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.Rmd
> 
> 
> If I understand this correctly I have to have a template already in place as the input object, is that correct? How would I construct this it that is so?
> 
> I also tried writing out directly to pdf and png with no success.
> 
> #pdf("c:/WHP/Appeals/OutputPDFs/EX&DE V1.pdf")
> #png("c:/WHP/Appeals/OutputPDFs/EX&DE V1.png")
> #opts_chunk$set(fig.path = "c:/WHP/Appeals/OutputPDFs/EX&DE V1.pdf") <--I don't even understand what this does, poached it from one of the google sites I have been reviewing and tried to make it work?
> 
> #This is the script I would like the output placed in PDF
> explanatory = c("claimStatusId", "AgeCat", "PatientGender", "PayorID")
> dependent = "AppealOverturned" # Appeals Status
> appdf1DT2 %>%
>    summary_factorlist(dependent, explanatory, p=TRUE, add_dependent_label=TRUE)
> 
> #dev.off()
> 
> 
> str(appdf1DT2)
> # Classes 'data.table' and 'data.frame':   3983 obs. of  21 variables:
> #   $ ClaimServiceID   : Factor w/ 3983 levels "51318639","51318640",..: 1 2 4 3 5 12 6 8 7 9 ...
> # $ LineNumber       : Factor w/ 140 levels "1","2","3","4",..: 1 2 4 3 5 7 1 3 2 4 ...
> # $ claimStatusId    : Factor w/ 2 levels "2","3": 2 2 2 2 2 1 1 1 1 1 ...
> # $ PatientGender    : Factor w/ 3 levels "F","M","UNK": 2 2 2 2 2 1 1 1 1 1 ...
> # $ PayorID          : Factor w/ 19 levels "000","234","239",..: 1 1 1 1 1 1 1 1 1 1 ...
> # $ AppealID         : Factor w/ 512 levels "79765","116998",..: 1 1 1 1 1 2 2 2 2 2 ...
> # $ ZipCode          : Factor w/ 223 levels "2155","3037",..: 72 72 72 72 72 102 102 102 102 102 ...
> # $ EditID           : Factor w/ 21 levels "","0","001X",..: 2 12 8 12 8 8 2 8 12 8 ...
> # $ CurrentBilled    : num  14394 14394 14394 14394 14394 ...
> # $ ClaimLineSavings : num  0 0 0 0 0 ...
> # $ StatusChangeMo   : Factor w/ 7 levels "2018-01","2018-02",..: 4 4 4 4 4 4 4 4 4 4 ...
> # $ Grouping         : Factor w/ 9 levels "","Agencies",..: 4 4 4 4 4 4 4 4 4 4 ...
> # $ AppealOverturned : Factor w/ 2 levels "1","2": 2 2 2 2 2 1 1 1 1 1 ...
> # $ PrimaryDX        : Factor w/ 360 levels "","8442","912",..: 2 2 2 2 2 171 171 171 171 171 ...
> # $ RevCodeCats      : Factor w/ 41 levels "AdminStorProcBlProd",..: 2 2 18 2 18 18 2 2 2 18 ...
> # $ AgeCat           : Factor w/ 9 levels "[0-5]","[11-20]",..: 4 4 4 4 4 8 8 8 8 8 ...
> # $ ClaimLevelSavings: num  0 0 0 0 0 ...
> # - attr(*, ".internal.selfref")=<externalptr>
> 
> head(appdf1DT2)
>     ClaimServiceID LineNumber claimStatusId PatientGender PayorID ProviderID AppealID ZipCode        TIN EditID
> 1:       51318639          1             3             M     000     149385    79765              33904      0
> 2:       51318640          2             3             M     000     149385    79765              33904    022
> 3:       51318642          4             3             M     000     149385    79765              33904   00504
> 4:       51318641          3             3             M     000     149385    79765              33904     022
> 5:       51318643          5             3             M     000     149385    79765              33904   00504
> 6:       85833537          7             2             F     000    3240182   116998              46635   00504
>     CurrentBilled ClaimLineSavings StatusChangeMo                                 Grouping AppealOverturned PrimaryDX        RevCodeCats
> 1:      14394.08                0        2018-04  Ambulatory Health Care Facilities                2      8442        AmbSurgCare
> 2:      14394.08                0        2018-04  Ambulatory Health Care Facilities                2      8442        AmbSurgCare
> 3:      14394.08                0        2018-04  Ambulatory Health Care Facilities                2      8442 MedSurgSuppandDevs
> 4:      14394.08                0        2018-04  Ambulatory Health Care Facilities                2      8442        AmbSurgCare
> 5:      14394.08                0        2018-04  Ambulatory Health Care Facilities                2      8442 MedSurgSuppandDevs
> 6:      23472.92                0        2018-04  Ambulatory Health Care Facilities                1     M1712 MedSurgSuppandDevs
>      AgeCat ClaimLevelSavings
> 1: [31-40]              0.00
> 2: [31-40]              0.00
> 3: [31-40]              0.00
> 4: [31-40]              0.00
> 5: [31-40]              0.00
> 6: [61-70]            296.25
> 
> 
> Maybe I am in over my head in this pursuit given my novice status with R, however, any direction would be appreciated.
> 
> Thank you.
> 
> WHP
> 
> Confidentiality Notice This message is sent from Zelis. ...{{dropped:15}}
> 


Not sure, if I get you right. Seems, that you use knitr:: and code 
chunks without the necessary context?

Please have a look at https://rmarkdown.rstudio.com/ to get a more 
general understanding about using knitr within RMarkdown context.

HTH,
Rainer Hurling




More information about the R-help mailing list