I try to create button for downloading with knitr package.
I have found some example which works a bit but I'm not able to change the name of the pdf. moreover it causes errors then.
enter image description here
second problem is that when I download the file , it is not a .pdf extension and I have to choose program to open it when I try to open. How can I download file as .pdf in default?
here are my codes (example from the internet):
server.R
library(knitr)
shinyServer(function(input, output) {
output$myreport = downloadHandler(
filename = "adads.pdf",
content = function(file) {
out = knit2pdf('input.Rnw', clean = TRUE )
file.rename(out, file) # move pdf to file for downloading
},
contentType = 'application/pdf'
)
})
ui.R
library(shiny)
shinyUI(basicPage(
textInput('firstname', 'First name', value = 'Jimmy'),
textInput('lastname', 'Last name', value = 'John'),
downloadButton('myreport')
))
and input.Rnw file
\documentclass{article}
\begin{document}
<<names>>=
input$firstname
input$lastname
#
\end{document}
Related
I build a shiny app that can filter data and automatically render .docx reports. As I want to customize my reports I would like to use the reference_docx function in the YAML header. I have the shiny app, the markdown document and the template.docx all in one folder. However, the shiny app does not automatically "use" the template.docx as markdown usually does. I assume that I have to load the template in a tempdir() or something like this, but I couldn't quite figure out how. What do I have to do to load the reference docx into the shiny app?
Thank you for your help!
Keep your rmarkdown and word template in app folder.
In your downloadhandler:
output$your_output <- downloadHandler(
filename = function() {
'output_title.docx'
},
content = function(file) {
tempReport <- file.path(temp_folder,
"rmarkdown_template.Rmd")
tempTemplate <- file.path(temp_folder, "template.docx")
file.copy("rmarkdown_template.Rmd", tempReport, overwrite = TRUE)
file.copy("template.docx", tempTemplate, overwrite = TRUE)
# Params
)
rmarkdown::render(tempReport, output_file = file, output_format = 'word_document',
params = params,
envir = new.env(parent = globalenv())
)
}
)
Then in your yaml:
output:
word_document:
reference_docx: template.docx
I have a parameterized RMarkdown PDF report that I am running from a Shiny dashboard (after copying it to a temporary directory).
Example code:
Shiny dashboard:
---
title: "Company Report Frontend"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}
library(flexdashboard)
library(shiny)
```
### Select Company
```{r}
textInput('name', label ='Firm')
```
### Report
```{r}
uiOutput("downloadUI")
# Create the actual downloadButton
output$downloadUI <- renderUI( {
downloadButton("downBtn", "Download Report")
})
# Add download handling
output$downBtn <- downloadHandler(
filename = "full_report.pdf",
content = function(file) {
tempReport <- file.path(tempdir())
file.copy("test_report.Rmd", paste0(tempReport, "/test_report.Rmd"), overwrite = TRUE)
rmarkdown::render(paste0(tempReport, "/test_report.Rmd"), output_file = file,
params = list(input=input),
envir = new.env(parent = globalenv()), clean = FALSE,
knit_root_dir = tempReport,
)
}
)
```
test_report.RMD:
---
title: "Report"
header-includes:
\usepackage{graphicx}
params:
input: NA
output:
pdf_document:
latex_engine: xelatex
---
```{r}
input <- params$input
data(mtcars)
plot(mtcars$hp, mtcars$mpg)
title(input$name)
```
When I run it, it successfully produces the .tex file but then can't compile it. When I try to compile the .tex file directly in a LaTeX editor, I get undefined control sequence and Missing endcsname defined errors on any \includegraphics lines like this one:
\includegraphics{C:/User/LONGUS~1/Temp/file5b381fa967c8_files/figure-latex/unnamed-chunk-1-1.pdf}
where LONGUS~1 is a shortened folder name from the actual Windows username LONGUSERNAME.
The error goes away, and the PDF compiles, if I replace LONGUS~1 with LONGUSERNAME, or just point it to the relative filepath. LaTeX does tend to get finicky about filepaths sometimes.
How can I instruct RMarkdown to either avoid shortening folder names, or skip the absolute filepath and just use the relative? test_report.RMD compiles fine if I run it by itself (and specify a default input), so I'd guess this is something to do with the use of tempdir() or at least something in the render() function. But I really should keep the tempdir() stuff in the case of multiple simultaneous users of the shiny app. I did try removing the knit_root_dir option but that didn't fix it.
Any suggestions welcome. Thank you!
I am trying to create a downloadable report from a shiny app. The report shows a table in which there are images.
I am passing the table with the url in the table as a parameter to the report as follows:
server <- function(input, output,session) {
data <- reactive({
data <- data.frame(RV3$data[,input$Map_EndoscopistIn],
RV3$data[,input$Map_FindingsIn],
RV3$data[,input$Map_MicroscopicTextIn],
RV3$data$url)
names(data)<-c(input$Map_EndoscopistIn,input$Map_FindingsIn,input$Map_MicroscopicTextIn,"Image")
if(!is.null(input$EndoscopistChooserIn)){
data<-data%>%filter(get(input$Map_EndoscopistIn)==input$EndoscopistChooserIn)
}
data
})
}
output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.doc",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
browser()
# Set up parameters to pass to Rmd document
params <- list(performanceTable=data()
)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
the url column in data() contains the following path eg:
![](<img src='Images/Images Captured with Proc Data Audit.files/img2527.jpg'>)
The report is as follows:
---
title: "Dynamic report"
always_allow_html: yes
output:
word_document:
fig_caption: true
params:
performanceTable: NA
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```
```{r,echo=FALSE,warning=FALSE}
pander(params$performanceTable, justify='left',split.table=Inf,caption="Table 1: Cancer diagnoses")
```
The report is in the same folder as the Images folder.
With this I get the following error example:
[pandoc warning] Could not find image `img%20src='Images/Images%20Captured%20with%20Proc%20Data%20Audit.files/img2500.jpg'', skipping...
What am I doing wrong? Is this a reformatting in pander issue or a paths issue or what? Or could this be that the report root is the tmp directory whereas I am referring to images stored in anoter static directory. If it is the latter, what do I do to get paths relative to the temp directory so I can see the images?
The reason you couldn't locate your image file is because you render your report.Rmd from a temp directory where your image doesn't exist:
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
The solution is that the image should also be copied to the same temp directory where markdown syntax ![](img.png) will pick up the file.
As in code change, you should be able to get it working by doing this:
assign a tempDir
tempDir <- tempdir()
Move Rmd as well as images to tempDir
file.copy(c("report.Rmd", "Images/Images Captured with Proc Data
Audit.files/img2527.jpg"), tempDir, overwrite = TRUE)
then render from temp dir
rmarkdown::render(file.path(tempDir, 'report.Rmd'), output_file = file,
params = params,
envir = new.env(parent = globalenv())
I want to generate a pdf report from my shiny app on shiny server. It work well on my computer.
I try to put in on my shiny server, but when i want to generate the report, I get a firefox "file not found" page instead of getting the pdf.
I use the code shown at : https://shiny.rstudio.com/articles/generating-reports.html
I also try to use directly my .Rmd file instead of copy/paste it to the temp dir, but I got the same error.
My server file :
output$pdfGen <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "rapport_preci.pdf",
content = function(file) {
withProgress(message = "Génération du pdf en cours", value = 0,{
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(varSite = input$pdfSite,
...
varTrans= input$valTrans1
)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
out <- render("report.Rmd", output_file = file,
params = params,
encoding = "UTF-8"
)
file.rename(out, file)
})
}
)
I think my app does not found my .Rmd file, but how do I fix that ? All my files are in the same folder.
I just run part of the code, and apparently the problem is when you set owd <- setwd(tempdir())
Here is what that does:
> tempdir()
[1] "/tmp/RtmpKafhlt"
> owd = setwd(tempdir())
> owd
[1] "/home/med"
and you probably don't have access to the home dir of the host server. You need to set replace on.exit(setwd(owd)) with on.exit(owd)
I would like to know how to load packages reading a text file in shiny. I have put the text file containing package list in www directory of the shiny application. The application loads the packages and works fine locally, however when I deploy it on shiny server, it does not load the packages and gives error of not finding the packages.
Error in library(simpleaffy) : there is no package called ‘simpleaffy’
The example code is below.
server.R
shinyServer(function(input, output, session) {
withProgress(message = "Please wait", value = 0, expr = {
for (i in 1:15) {
source(textConnection(readLines("www/packages.txt", warn = FALSE)[i]))
incProgress(amount = 1/15, detail = paste0("Loading package ", i, "/15"))
Sys.sleep(time = 0.1)
}
})
})