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

Outsource rmd render #16

Merged
merged 9 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rapRegTemplate
Type: Package
Title: Template to be used for Making New Registries at Rapportekt
Version: 1.0.1
Version: 1.1.0
Authors@R: c(
person(given = "Are",
family = "Edvardsen",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rapRegTemplate 1.1.0

* More content added to sample report
* Rendering of Rmarkdown now performed by rapbase
* General improvements made as result of changes in rapbase

# rapRegTemplate 1.0.1

* linting code
Expand Down
Binary file added inst/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 20 additions & 12 deletions inst/samlerapport.Rmd
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
---
title: "Samlerapport"
author: "Rapporteket"
date: '`r format(Sys.time(), "%d. %B %Y")`'
params:
var: "mpg"
type: pdf
var: mpg
bins: 5
output: html_document
title: Samlerapport
subtitle: en eksempelrapport fra rapRegTemplate
author: Rapporteket
userFullName: Tore Tester
registryName: SKDE
reglogo: '`r system.file("logo.png", package = "rapRegTemplate")`'
regtext: '`r readLines(system.file("shortDescription.txt", package = "rapRegTemplate"))`'
date: '`r format(Sys.time(), "%d. %B %Y")`'
abstract: Om rapporten trenger et sammendrag kan det settes inn her
---

```{r setup, include=FALSE}
Expand All @@ -16,7 +22,7 @@ knitr::opts_chunk$set(echo = FALSE)
```


## Samlerapport
# Samlerapport
Dette er eksempel på en samlerapport som er egnet til å kobinere presentasjon
av dynamisk tekst, tabeller og figurer. Samlerapporter kan vises i selve
Rapporeket, lastes ned eller sendes (rutinemessig) per epost til de som ønsker
Expand All @@ -25,22 +31,24 @@ det.

## Eksempel på dynamisk tekst
Idag, `r format(Sys.Date(), "%A %d. %b %Y")` er det
`r as.numeric(format(Sys.Date(), "%j"))` dager siden nyttår
`r as.numeric(format(Sys.Date(), "%j"))` dager siden nyttår.


## Eksempel på tabell
Tabellen under er kjedelig, men illustrerer poenget:
Tabell \@ref(tab:eksTab) under er kjedelig, men illustrerer poenget.

```{r eksTab}
dt <- mtcars[1:6, ][params$var]
kable(dt) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
cap <- paste("Eksempeltabell som viser variabelen", params$var, "fra mtcars.")
rapbase::mst(tab = dt, type = params$type, cap = cap)
```


## Eksempel på figur
Vi har sett den før, men tåler å få figuren på nytt:
Samlerapporter kan også inneholde figurer basert på data slik som vist i Figur \@ref(fig:eksFig).

```{r eksFig, echo=FALSE}
```{r eksFig, echo=FALSE, out.width="80%", fig.cap=paste("Eksempelfigur som viser frekvensfordelingen av", params$var, "fra mtcars.")}
f <- rapRegTemplate::makeHist(df = mtcars, var = params$var, bins = params$bins, makeTable = FALSE)
```
51 changes: 22 additions & 29 deletions inst/shinyApps/app1/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@ server <- function(input, output, session) {
# Last inn data
# regData <- getFakeRegData()

# Gjenbrukbar funksjon for å bearbeide Rmd til html
htmlRenderRmd <- function(srcFile, params = list()) {
system.file(srcFile, package = "rapRegTemplate") %>%
knitr::knit() %>%
markdown::markdownToHTML(.,
options = c("fragment_only",
"base64_images",
"highlight_code")) %>%
shiny::HTML()
}

# Brukerinformasjon i menylinja (navbar)
output$appUserName <-
shiny::renderText(
paste(rapbase::getUserFullName(session),
rapbase::getUserRole(session), sep = ", "))
output$appOrgName <- shiny::renderText(rapbase::getUserReshId(session))
userInfo <- rapbase::howWeDealWithPersonalData(session,
callerPkg = "rapRegTemplate")
userInfo <-
rapbase::howWeDealWithPersonalData(session, callerPkg = "rapRegTemplate")
shiny::observeEvent(input$userInfo, {
shinyalert("Dette vet Rapporteket om deg:", userInfo,
type = "", imageUrl = "rap/logo.svg",
Expand All @@ -35,7 +24,10 @@ server <- function(input, output, session) {

# Veiledning
output$veiledning <- renderUI({
htmlRenderRmd("veiledning.Rmd")
rapbase::renderRmd(
system.file("veiledning.Rmd", package = "rapRegTemplate"),
outputType = "html_fragment"
)
})


Expand All @@ -55,28 +47,29 @@ server <- function(input, output, session) {
# Samlerapport
## vis
output$samlerapport <- shiny::renderUI({
htmlRenderRmd(srcFile = "samlerapport.Rmd",
params = list(var = input$varS, bins = input$binsS))
rapbase::renderRmd(
system.file("samlerapport.Rmd", package = "rapRegTemplate"),
outputType = "html_fragment",
params = list(type = "html",
var = input$varS,
bins = input$binsS)
)
})

## last ned
output$downloadSamlerapport <- shiny::downloadHandler(
filename = function() {
"rapRegTemplateSamlerapport.html"
basename(tempfile(pattern = "rapRegTemplateSamlerapport",
fileext = paste0(".", input$formatS)))
},
content = function(file) {
srcFile <- normalizePath(system.file("samlerapport.Rmd",
package = "rapRegTemplate"))
tmpFile <- "tmpSamlerapport.Rmd"
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(srcFile, tmpFile, overwrite = TRUE)
out <- rmarkdown::render(tmpFile,
output_format = rmarkdown::html_document(),
params = list(var = input$varS,
bins = input$binsS),
output_dir = tempdir())
file.rename(out, file)
srcFile <-
normalizePath(system.file("samlerapport.Rmd", package = "rapRegTemplate"))
fn <- rapbase::renderRmd(srcFile, outputType = input$formatS,
params = list(type = input$formatS,
var = input$varS,
bins = input$binsS))
file.rename(fn, file)
}
)

Expand Down
38 changes: 24 additions & 14 deletions inst/shinyApps/app1/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,31 @@ ui <- shiny::tagList(
),
shiny::tabPanel("Samlerapport"
# ,
# tabPanel("Fordeling av mpg",
# sidebarLayout(
# sidebarPanel(width = 3,
# selectInput(inputId = "varS",
# label = "Variabel:",
# c("mpg", "disp", "hp", "drat", "wt", "qsec")),
# sliderInput(inputId = "binsS",
# label = "Antall grupper:",
# min = 1,
# max = 10,
# value = 5),
# downloadButton("downloadSamlerapport", "Last ned!")
# shiny::tabPanel("Fordeling av mpg",
# shiny::sidebarLayout(
# shiny::sidebarPanel(
# width = 3,
# shiny::selectInput(
# inputId = "varS",
# label = "Variabel:",
# c("mpg", "disp", "hp", "drat", "wt", "qsec")),
# shiny::sliderInput(
# inputId = "binsS",
# label = "Antall grupper:",
# min = 1,
# max = 10,
# value = 5),
# shiny::selectInput(
# inputId = "formatS",
# label = "Velg format for nedlasting:",
# choices = list(PDF = "pdf", HTML = "html")
# ),
# shiny::downloadButton(
# outputId = "downloadSamlerapport",
# label = "Last ned!")
# ),
# mainPanel(
# uiOutput("samlerapport")
# shiny::mainPanel(
# shiny::uiOutput("samlerapport")
# )
# )
# )
Expand Down
1 change: 1 addition & 0 deletions inst/shortDescription.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rapRegTemplate er en enkel mal som kan brukes som utgangspunkt for etablering og utvikling av resultattjenester for kvalitetsregistre på Rapporteket. Malen inneholder noen typiske elementer som er vanlig å benytte. Men husk, det er fult mulig å utvide eller lage ny funksjonalitet som går utover det som ligger i denne malen.
2 changes: 1 addition & 1 deletion inst/veiledning.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Utgangspunket for de neste stegene er bruk av det innebygde datasettet "mtcars",
Tips til oppgave B:
```{r eval=FALSE}
## Sammendrag
output$distSummary <- renderTable({
output$distSummary <- shiny::renderTable({
as.data.frame(sapply(regData, summary))[input$var]
}, rownames = TRUE)
```
Expand Down