generate_static_code: Generate R code from shiny server code

Description Usage Arguments Value Examples

Description

Takes in a shiny server function, searches working directory for app.R and global.R files, and reconstructs an independent R script from the server components.

Usage

1
2
3
4
generate_static_code(server, ..., dots = list(), call_outputs = TRUE,
  initialize_params = TRUE, keep_returns = FALSE, flatten_outputs = TRUE,
  files = file.path(getwd(), c("^app\\.R$", "^global\\.R$")),
  envir = parent.frame(), session = get("session", envir = envir))

Arguments

server

the server function to generate an R script for

...

names of output names for which to build code

dots

optionally pass ellipses names of outputs as list

call_outputs

whether calls to the specified outputs should be appended at the end of the script

initialize_params

whether to add code to instantiate server function arguments in the generated script. this is generally useful for the top level call, but not for subsequent nested modules.

keep_returns

whether to prune return statements if they arent' needed for generated specified outputs. this is generally useful in situations where a specific output is desired, but unwanted when generated compelete nested module code.

flatten_outputs

whether a singular output should be collapsed into parent script

files

the filepaths to search for available shiny globally scoped code

envir

the environment in which to search for arguments originally passed to the server function. A construction of those arguments as they exist in the current state will attempt to be built into the generated script.

session

the active shiny session, defaults to the value of the variable by the same name in the parent environment.

Value

a script representing the current state of the shiny app, allowing for independent reproduction of the shiny outputs.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## Not run: 
## Shiny Example (app.R contents)
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)), silent = TRUE) # R Studio
try(setwd(dirname(dirname(parent.frame(2)$ofile))), silent = TRUE) # running as a script

library(shiny)

my_data <- datasets::mtcars

# additional code before shinyApp call will be captured if in app.R
ui <- fluidPage(
  selectInput('x', 'x axis', choices = names(my_data)),
  selectInput('y', 'y axis', choices = names(my_data)),
  plotOutput('plot'),
  verbatimTextOutput('code')
)

srv <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(x = my_data[[input$x]],
         y = my_data[[input$y]])
  })
  output$code <- renderPrint({
    cat(get_code(srv, 'plot'))
  })
}

shinyApp(ui, srv)

## End(Not run)

dgkf/scriptgloss documentation built on June 8, 2019, 8:43 p.m.