0

I am creating a shiny app to automate Rmarkdown reports. I'm using data similar to the one that can be replicated with this code:

Report 1:


---
title: "`r htmltools::HTML('<h1>Report 1 </h1>')`"
params:
  fecha: "2022-01-01"
  institucion: ""
---

```{r setup,  echo = FALSE}
date <- params$fecha
``` # erase this "#"

**Date**: `r params$fecha` <br>

Bla, bla, bla. 


```{r,  echo = FALSE}
library(DT)
dt <- datatable(iris)
dt

``` # erase this "#"

Report 2:


---
title: "`r htmltools::HTML('<h1>Report 2 </h1>')`"
params:
  fecha: "2022-01-01"
  institucion: "DIGERCIC"
---

```{r setup,  echo = FALSE}
date <- params$fecha
inst_reporte <- params$institucion
``` #erase this "#"

**Date**: `r params$fecha` <br>
**Institution**: `r params$institucion`

Bla, bla, bla. 

Shiny:

library(shiny)
library(rmarkdown)

rm(list = ls())

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      titlePanel("Repotería seguimiento PEIPRDCI"),
      selectInput("input_type", "Report type",
                  c("Global", "By institutions")),
      conditionalPanel(
        condition = "input.input_type != 'Global'", 
        selectInput("param", "Select the institution:", 
                    choices = c("BDE", "DIGERCIC"))
      ),
      selectInput("date", "Seleccione the date:", 
                  choices = c("2023-01-01", "2023-02-01")), 
      actionButton("generate", "Generar reporte")
    ), 
    mainPanel(
      htmlOutput("report"), 
      # uiOutput("dtout") 
      conditionalPanel(
        condition = "input.input_type == 'Global' && input.generate > 0",
        uiOutput("dtout")
      )
    )  
  )
)




server <- function(input, output) {

  a <- eventReactive(input$generate, {
    if (input$input_type == "By institutions") {
      rmarkdown::render("C:/Users/santy/Documents/report2.Rmd",
                        params = list(institucion = input$param,
                                      fecha = input$date))
      includeHTML("C:/Users/santy/Documents/report2.html")
      
    } else {
      rmarkdown::render("C:/Users/santy/Documents/report.Rmd",
                        params = list(fecha = input$date))
      includeHTML("C:/Users/santy/Documents/report.html") 
    }
  })
  

  output$report <- renderUI({
              a()
  })

b <- reactive({
       env <- new.env(parent = globalenv())
       rmarkdown::render("C:/Users/santy/Documents/report.Rmd",
                  envir = env)
       dtout <- env$dt
       })  

  output$dtout <- renderUI({
b()
  })
  
}

shinyApp(ui = ui, server = server)


As you can see, the code generates three inputs:

(1) input_type: allows the user to select the type of report. Two options: "Global" and "Por instituciones.

(2) param: Only shown if input$input_type == "Por instituciones". It allows the user to select the institution for which he/she wants to generate the report.

(3) date: Allow the user to select the date of the report.

Report 1 has an interactive table from the DT package. As DT tables have problems displaying in Shiny when they come from Rmarkdown, I did what was suggested here: datatable doesn't render in rmarkdown inside shiny app. That is, to include the interactive table directly in shiny, as it is conveniently placed at the end of the report.

However, my situation is different, as I have another report of different nature. Two problems arise:

(1) When the user

- input_type to "Global"
- generates a report 
- changes input_type to "By institutions" 
- generates a report  
- changes the input type to  "Global" again

The Report 1 interactive table appears in Report 2 before the user clicks on "generate". This can be seen here:

enter image description here

(2) When the user

- Selects "By institutions" in "input_type" first.
- Generates a report. 
- Selects "Global" in "input_type"

Then, the headers of the interactive table appears again, but there is an error in everything else. After that, the datatable is not updated. Even when the "generate" button is pressed, it remains with the error. I am clueless on why this is the case. This can be seen here:

enter image description here

And here:

enter image description here

Any ideas on how to solve these issues?

Alan Schofield
  • 19,839
  • 3
  • 22
  • 35

0 Answers0