(Go: >> BACK << -|- >> HOME <<)

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation request: how to use ezknitr with Shiny and paramaterised R markdown reports #15

Open
AmyMikhail opened this issue Oct 30, 2018 · 0 comments

Comments

@AmyMikhail
Copy link
AmyMikhail commented Oct 30, 2018

I have a parameterised R markdown report which creates some tables and figures after querying a database and fetching data for the genus or species specified by the user.

My YAML header for a file called AlertReport.Rmd looks like this:

---
params:
  genus: NA
  species: NA
title: "Alert report"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: 
  orgcharts::html_org:
    includes:
      in_header: org_logo.html
---

(orgcharts is a local package that produces a HTML template with my organisation's branding on it).

If I run this .Rmd directly from 'knit with parameters' it will output the resultant HTML to the same directory as the .Rmd and will call it AlertReport.html, such that when I choose different species via the parameters, this file will be over-written.

I therefore want to:

a) change the name of the output file to paste0("AlertReport_", params$species, "_", Sys.Date(), ".html");

b) change the directory that the file will be saved in to a completely different network drive;

c) wrap all this in a shiny app which would call ezknitr() to do (a) and (b) above.

First getting ezknitr() to do (a) and (b):

  • I can't figure out how to pass multiple elements to out_suffix as currently it seems just to take one? - - For the specifications above I want the species name that has been provided as a parameter by the user to be included in the output report name, but couldn't figure out how to do this since the current readme/example for ezknitr names the parameter value and deals with each separately and in my case I won't know what the user has entered (there are literally thousands of options so I'm not providing a menu, they will just type in the species as free text).
  • (b) is straight-forward as presumably I can just supply the full path to out_dir.

Next putting this in a shiny app:

  • I'm presuming this goes in the content slot of the downloadHandler(), but then what do I do with the 'filename' slot so that it doesn't over-write the changes I am making to the file name with the out_suffix argument?
  • Also the parameters have already been provided as input$genus and input$species, respectively in the ui - how do I pass these to the params argument of ezknit()?

See my (not working) attempt below:

###############################
# Set directories:
library(shiny)
library(here)
library(lubridate)
library(ezknitr)

# Define the path to the .Rmd file:
rmdwd <- here::here()

# Define the path to the root directory to save the report to (dummy example):
repwd <- "//MyNetworkDrive/Myfolder"

# Auto-calculate the reporting week and year:
repdate <- Sys.Date() - weeks(1)
ryear <- year(repdate)
rweek <- isoweek(repdate)
ryw <- paste0(ryear, rweek)

# Create the year/week sub-directory to save the report in:
outwd <- paste(repwd, "/", ryear, "/", ryw, sep = "")

###############################
# Create user interface:
ui = fluidPage(
    textInput(inputId = "genus", label = "Pathogen genus", value = "NA", placeholder = "To generate an alert report at the genus level, enter the pathogen genus in this box"),
    textInput(inputId = "species", label = "Pathogen species", value = "NA", placeholder = "To generate an alert report at the species level, enter the pathogen genus and species in this box"),
    downloadButton("report", "Generate report")
  )

###############################
# Create server logic:
server <- function(input, output) {
    
    # Create the output: 
    output$report = downloadHandler(
      

      # Set up parameters to pass to Rmd document
      params = list(genus = input$genus, species = input$species),
      
      # Create the temp file name:
      filename = function() {
        
        ifelse(params$genus == "NA", 
                        paste0("AlertReport_", params$species, "_", Sys.Date(), ".html"),
                        paste0("AlertReport_", params$genus, "_", Sys.Date(), ".html"))
      },
      
      # Define the content of the temp file:
      content = function(file) {
        
        # Knit the document, defining the destination path and file name:
        ezknitr::ezknit(file = "ExceedanceReports.Rmd", 
                        wd = rmdwd,
                        out_dir = outwd, 
                        fig_dir = "figs",
                        params = list(genus = "NA", species = "NA"), 
                        keep_md = FALSE, 
                        keep_html = TRUE)
        }
    )
  }

###############################
# Run app:
shinyApp(ui, server)

So in summary:

  • getting out_suffix to take a list of things to be appended, and specifically parameters for parameterised R markdown reports would be really handy (feature request unless there is already a way to do this)?
  • having some documentation to show how to use ezknit with parameterised reports in combination with Shiny would be a great help.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant