ClevelandClinicQHS / riskcalc

An R package for building risk calculators
https://clevelandclinicqhs.github.io/riskcalc/
Other
2 stars 0 forks source link

Add shiny dependency #2

Closed zajichek closed 1 year ago

zajichek commented 1 year ago

The main purpose of the app is to create shiny apps, so need to configure the dependency and figure out how to write a function that creates an app.

zajichek commented 1 year ago

From the local repo:

library(devtools)
use_package("shiny")

The shiny package is added under the Imports section of DESCRIPTION

Then we can edit riskcalc_fun (our toy function example):

riskcalc_fun <-
  function()
  {
    # Make a UI
    ui <-
      shiny::fluidPage(
        title = "Example App",
        shiny::sidebarLayout(
          shiny::sidebarPanel(
            shiny::textInput(
              inputId = "test_input",
              label = "Add Text"
            )
          ),
          shiny::mainPanel(
            shiny::textOutput(outputId = "show_text")
          )
        )
      )

    # Make the server
    server <-
      function(input, output) {
        output$show_text <- shiny::renderText({input$test_input})
      }

    # Run the app
    shiny::shinyApp(ui, server)
  }

When we load_all() in the development package and run riskcalc_fun(), a shiny app is started.

Also, we can store the app in a variable, which can be used to testing, etc.

> my_app <- riskcalc_fun()
> class(my_app)
# "shiny.appobj"

Added the following to the documentation, which allowed examples to be tested w/o error in check():

if(interactive()) {
  riskcalc_fun()
}