An economist
An economist

Reputation: 1311

Producing PDF report using user input files - Shiny

I have created R code that allows me to transform and analyze data and then output the results in form of tables into PDF report. Recently, I decided to share my work with colleges, who are not familiar with R. Therefore, I want to create Shiny app that will allow them to use my work without having R installed. The Shiny app will serve as platform to upload data and then download the report. Unfortunately, I am not able to output my results into PDF.

My Shiny code looks the following way:

library(knitr)

ui <- fluidPage(
  fluidRow(
    column(3,
           wellPanel(
             fileInput(inputId = "files",
                       label = "Choose csv files",
                       accept=c('text/csv', 
                                'text/comma-separated-values,text/plain',
                                '.csv'),
                       multiple = TRUE),
             textInput('filename', 'File name', value = ''),
             downloadButton('report', label = 'Download PDF')
             )
           )
    )
  )

server <- function(input, output) {

  data_set <- reactive({if(!is.null(input$files)) {
    max_table = length(input$files[,1])

    lst <- list()
    for(i in 1:length(input$files[,1])){
      lst[[i]] <- read.csv(input$files[[i, 'datapath']], sep = ",", header = TRUE, skip = 4, dec = ".")
    }

    lst <- lapply(lst, function(x) xtable(x))}})



  output$report = downloadHandler(
    filename = reactive({paste0(input$filename,'.pdf')}),

    content = function(file) {
      out = knit2pdf('pdf_shell.Rnw', clean = TRUE)
      file.rename(out, file) # move pdf to file for downloading
    },

    contentType = 'application/pdf'
  )
  }


shinyApp(ui = ui, server = server)

And my .Rnw file looks as follows:

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
\begin{tabularx}
\textbf{Test Name:}& \Sexpr{input$filename} \\
\textbf{Date:}&  \\
\end{tabularx}
\end{table}

<<echo = FALSE , message = F, >>=
data_set
@


\end{document}

Obviously loading the data_set is not working and I have no idea how to move forward. For now I would like to just output the table created out of the data that I load into the Shiny.

I would really appreciate your help.

Side note: the csv files are externally simple data frames consisting of headers and columns filled with data.

When coutputed in R they should look something like that:

$data001
 A   B   C   D   E
 X   10  30  50  70
 Y   20  40  60  80

Upvotes: 1

Views: 1608

Answers (1)

Batanichek
Batanichek

Reputation: 7871

Problem because in shiny

data_set is reactive so you cant use it like

<<echo = FALSE , message = F, >>=
data_set
@

in your .Rnw

So try

.Rnw

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
\begin{tabularx}
\textbf{Test Name:}& \Sexpr{input$filename} \\
\textbf{Date:}&  \\
\end{tabularx}
\end{table}

<<echo = FALSE , message = F, >>=
data_set_1
@


\end{document}

handler

downloadHandler(
    filename = reactive({paste0(input$filename,'.pdf')}),

    content = function(file) {
      data_set_1=data_set()
      out = knit2pdf('pdf_shell.Rnw', clean = TRUE)
      file.rename(out, file) # move pdf to file for downloading
    },

    contentType = 'application/pdf'
  )

Or may be simple data_set() in .Rnw

Upvotes: 3

Related Questions