mrjoh3
September 20, 2018, 12:06am
1
Split from the original thread: Generating downloadable reports (rmarkdown) : reference_docx (yaml header) is not read by knitr - #7
have tried using word_document()
and absolute paths as suggested above, but this has made no difference.
It is odd because it works as expected on rstudio server but not shiny server; even though they are running on the same machine.
I am using absolute paths in with temporary folders, so /tmp/RtmpnWjfwF/oGLv9Kz/template.docx
. Could this be causing a problem? Both .Rmd
and template.docx
are in the same temporary folder.
thanks for the quick reply, unfortunately I am still getting the same behaviour. So, style works fine on rstudioserver but not once deployed to shinyserver. This is all running on an Ubuntu machine, but both servers are using the same system R install.
I have included below the error message I get from word (not very informative) and the render
and yaml
code chucks. Any further suggestions or advice would be greatly aprpeciated.
rmarkdown::render("/tmp/RtmpnWjfwF/YY7KrNO/report.Rmd",
'word_document',
knit_root_dir = "/tmp/RtmpnWjfwF/YY7KrNO",
output_file = file,
clean = FALSE,
params = params,
envir = new.env(parent = globalenv())
)
And the yaml:
---
output:
word_document:
reference_docx: photo_log_template.docx
params:
organisation: NA
photographer: NA
df: NA
---
thanks again
Matt
jcheng
September 21, 2018, 9:59pm
2
Would it be very difficult to create a minimal reproducible example, and post it as an issue at https://github.com/rstudio/rmarkdown ? This seems like a real issue but I don't know if any one person knows rmarkdown and shiny well enough to diagnose this without a working reprex.
Thanks...
1 Like
mrjoh3
September 22, 2018, 12:04am
3
Thanks for your response, I am just testing on another computer. This will give me a repex so if I get the same problem I will post the issue to github. I will report back here as well.
mrjoh3
September 27, 2018, 2:19am
4
Thanks @jcheng I have just submitted an issue with reprex. Please advise if more details are required.
opened 02:17AM - 27 Sep 18 UTC
closed 12:05AM - 07 Dec 18 UTC
I have been having trouble rendering a word docx file using a `reference_docx` s… tyle document on shiny server. The file renders but the style is not applied.
Curiously, the file and the style render as expected when the shiny app is run from rstudio server on the same machine. Plus I have tested a reprex on another machine and could not reproduce the error.
I have created a [gihub repo](https://github.com/mrjoh3/reprex-shiny-rmd-docx) with a minimum example. I will include the shiny app code below.
The shiny server (open source) is version `Shiny Server v1.5.3.838" "Node.js v6.10.0` and is running on `Ubuntu 16.04.1`. and I have updated the `shiny` and `rmarkdown` packages.
The rmd and docx files are copied to a temp folder for rendering and I have ensured that the user permissions are and have ensured that the user permissions are `chmod 1777 /tmp`.
A similar issue was flagged on the [rstudio community forum](https://community.rstudio.com/t/generating-downloadable-reports-rmarkdown-reference-docx-yaml-header-is-not-read-by-knitr/3308), but the suggestions made there did not resolve the issue.My question there was moved [here](https://community.rstudio.com/t/issues-generating-downloadable-reports-from-shiny-server-word-document/14655), @jcheng5 requested I submit an issue here with a reprex. All required files are in the [github repo](https://github.com/mrjoh3/reprex-shiny-rmd-docx), the `app.R` and `.Rmd` files are copied below. The `.docx` template file is [here](https://github.com/mrjoh3/reprex-shiny-rmd-docx/blob/984cc998709de6f08d55bb8321319d7bcd9d1f8d/photo_log_template.docx).
please advise if more details are required.
## The app.R file
```r
library(shiny)
library(dplyr)
library(knitr)
library(rmarkdown)
library(DT)
library(jsonlite)
library(hashids)
cat("Doing application setup\n")
onStop(function() {
cat("Removing Temporary Files and Folders\n")
unlink(temp_folder, recursive=TRUE)
})
temp_folder <<- file.path(tempdir(),
hashids::encode(as.integer(Sys.time()),
hashid_settings(salt='fews', min_length = 6))
)
dir.create(temp_folder)
ui <- shinyUI(fluidRow(
column(10,
h2('Data Output'),
DT::DTOutput("images"),
downloadButton("report", "Generate report",
style="color: #fff; background-color: #337ab7; border-color: #2e6da4")
)
)
)
server <- shinyServer(function(input, output, session) {
dat <- reactiveValues(images = data_frame("photographer" = c('A','B'),
"organisation" = 'The Org',
"location" = 'The Place',
"address" = c('2 some place', '3 that place'))
)
output$images <- DT::renderDT(dat$images,
server = FALSE,
rownames = FALSE,
class = 'dt-body nowrap',
selection = 'single', editable = TRUE,
extensions = c('Buttons','Responsive'),
options = list(pageLength = 5,
dom = 'Blfrtip',
buttons = c('csv', 'excel')))
output$report <- downloadHandler(
filename = "Photo_Log.docx",
content = function(file) {
tempReport <<- file.path(temp_folder, "report.Rmd")
tempTemplate <<- file.path(temp_folder, "photo_log_template.docx")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
file.copy("photo_log_template.docx", tempTemplate, overwrite = TRUE)
params <- list(organisation = input$select_org,
photographer = input$select_user,
df = select(dat$images,
`Premises / Location` = location,
Address = address)
)
rmarkdown::render(tempReport, 'word_document',
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
})
shinyApp(ui = ui, server = server)
```
## The rmd file
````
---
title: "Photograph Log"
output:
word_document:
reference_docx: photo_log_template.docx
params:
organisation: NA
photographer: NA
df: NA
---
### Photograph Log: `r params$organisation`
### Photographer: `r params$photographer`
```{r, echo=FALSE, warning=FALSE}
library(knitr)
knitr::kable(params$df)
```
````