Shiny + knit not reading docx reference

309 views
Skip to first unread message

Rafael Batista

unread,
Oct 17, 2016, 1:22:36 PM10/17/16
to Shiny - Web Framework for R
Hello everyone.

I am doying a shiny app to generate reports automatically. I followed the procedure on "http://rmarkdown.rstudio.com/articles_docx.html" and created an external docx reference for my report (for centralized titles, font colors, etc).

When I press the knit button on rstudio everything works fine. The styles are normally passed from the reference document to the final report.

However, when I tried to implement this on shiny app the final report is generated without any style. It seems that knit is not reading the reference document. I imagine that this is working directory issue but I really dont know what to do...

ui
downloadButton('downloadReport')

server
  output$downloadReport <- downloadHandler(
    filename
<- function() {
      paste
('wind.report',input$projname, sep = '.', switch(input$format, Word = 'docx'))
   
},
   
    content
<- function(file) {      
      library
(rmarkdown)
     
out <- rmarkdown::render('24_12_report.Rmd', switch(input$format, Word = word_document()))
      file
.rename(out, file)
   
}
 
)

I put everything in the same folder: Rmd file, server.R, ui.R and reference docx...

Yihui Xie

unread,
Oct 17, 2016, 1:58:37 PM10/17/16
to Rafael Batista, Shiny - Web Framework for R
You can try to change

switch(input$format, Word = word_document())

to

switch(input$format, Word = "word_document")

Regards,
Yihui
> --
> You received this message because you are subscribed to the Google Groups
> "Shiny - Web Framework for R" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shiny-discus...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/shiny-discuss/6a0e21fa-3947-4747-928e-833d8da14399%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Rafael Batista

unread,
Oct 17, 2016, 2:01:22 PM10/17/16
to Shiny - Web Framework for R, rafael...@gmail.com
Nahh it gives me:

ERROR: Unrecognized output format specified


Yihui Xie

unread,
Oct 17, 2016, 2:16:19 PM10/17/16
to Rafael Batista, Shiny - Web Framework for R
Did you specify "word_document" as the output format in
24_12_report.Rmd? What is the YAML frontmatter like in your Rmd?

Regards,
Yihui

Rafael Batista

unread,
Oct 17, 2016, 2:25:21 PM10/17/16
to Shiny - Web Framework for R, rafael...@gmail.com
Hi again Yihui. Thank you for sharing your time with me.


This is the YAML


---
title
: ''
author
: ''
date
: ''
output
:
   word_document
:
      reference_docx
: ./www/skeleton_report.docx
---

In reference_docx parameter I tried several different things, like:

reference_docx: skeleton_report.docx (to look for the skeleton in root folder)
reference_docx: ./www/skeleton_report.docx (to look for skeleton in www folder)
reference_docx: "./www/skeleton_report.docx" (using quotes to see if this is the problem...)

All the above syntax seems to work with rstudio+knit button.


Rafael Batista

unread,
Oct 18, 2016, 6:46:36 AM10/18/16
to Shiny - Web Framework for R, rafael...@gmail.com
Yihui... I decided to try a workaround solution using webshot to convert HTML report to PDF.

At the moment my code is the following:

# report output
  output$downloadReport
<- downloadHandler(
    filename
<- "plot.pdf",
     
    content
<- function(file) {
     
      library
(rmarkdown)
     
out <- rmarkdown::render('24_12_report.Rmd',  html_document())
      file
.rename(out, 'temp.html')
      webshot
("temp.html", file = file, cliprect = "viewport")
   
}
 
)


It appears to work but pdf contains the entire html. Do you know if there is a way to print several pages A4 sized like when you ctrl+p on Chrome? I was looking into this and apparently phantomjs can be set to A4. But I dont know how to do this within my shiny code.

Laurent Spanu

unread,
Nov 28, 2017, 7:02:27 AM11/28/17
to Shiny - Web Framework for R
Hi everyone,

I'm facing the same issue (Shiny + Rmd) : it seems that when Rmd file is compilated, the reference docx is not read. Yet, reference docx and Rmd file are both in the same directory...

Does anyone find a workaround ?

Thank you for your help,

Laurent
Reply all
Reply to author
Forward
0 new messages