Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing Shiny Apps

Tags:

r

testing

shiny

So I have been writing a fairly detailled shiny app, and in the future will need updating as the functionality behind what is run is constantly changing.

What I need to be able to do is have unit tests (either using testthat or another library more useful for shiny apps) that enables me to run these tests in a more automated fashion.

I have written a simple shiny app. For the sake of testing in this would like a way to know that if I choose the number 20 in the numeric input then I get 400 as the output$out text. But want to be able to do this without actually running the app myself.

library(shiny)

ui <- fluidPage(title = 'Test App', 
    numericInput('num', 'Number', 50, 1, 100, 0.5),
    'Numeric output',
    textOutput('out')
)

server <- function(input, output, session) {
  aux <- reactive(input$num ^ 2)

  output$out <- renderText(aux())
}

shinyApp(ui = ui, server = server)
like image 739
Ashley Baldry Avatar asked Mar 31 '16 16:03

Ashley Baldry


People also ask

Do you need R to run Shiny app?

But the simplest way to run a Shiny app is to run it locally. You only need the shiny R package installed, and you can run the app in your browser.

What is Shinytest?

shinytest provides a simulation of a Shiny app that you can control in order to automate testing. shinytest uses a snapshot-based testing strategy: the first time it runs a set of tests for an application, it performs some scripted interactions with the app and takes one or more snapshots of the application's state.

How do you make a Shiny app faster?

Caching is particularly effective for Shiny apps, because the cache can be shared across users. That means when many people are using the same app, only the first user needs to wait for the results to be computed, then everyone else gets a speedy result from the cache.

What can a Shiny app do?

With Shiny, you can host standalone applications on a webpage, embed interactive charts in R Markdown documents, or build dashboards. You can also extend your Shiny applications with CSS themes, HTML widgets, and JavaScript actions. Shiny enables you to write powerful interactive web applications entirely in R.


2 Answers

As many already mentioned, you can use the package shinytest combined with testthat.

Here a simple example:

library(shinytest)
library(testthat)

context("Test shiny app")

#open shiny app
app <- ShinyDriver$new('path_to_shiny_app')

test_that("app gets expected output", {
  #set numeric input
  app$setInputs(num = 20)
  #get output
  output <- app$getValue(name = "out")
  #test
  expect_equal(output, "400")  
})

#stop shiny app
app$stop()
like image 164
Ferand Dalatieh Avatar answered Nov 25 '22 19:11

Ferand Dalatieh


I see two potential approaches here – testing the underlying functionality, and performing tests of the web application itself. Note that the latter actually would require running the server, but is a more accurate representation of if your web app works or not.

By testing the underlying functionality, what I mean is refactoring the calculations you currently perform in the server to their own, independent functions. Instead of squaring the number directly in the server, you ought to separate the functionality from the server so it can be tested. For example, like so:

square_of_number <- function(n) return(n^2)

Now, you can separately test the square_of_number function for its expected output.

library('testthat')

square_of_number <- function(n) return(n^2)

expect_equal(square_of_number(4), 16)

Further, if you want to test the application itself, you could also create tests using a headless browser on the actual UI you generate with Shiny. One method as suggested in the comments is using Shinytest, but one approach that I'd suggest trying is:

  • Running the server with a specific port,
  • Interfacing this server with a tool like rvest or RSelenium to manipulate the page and then scrape the output,
  • then verifying said output with testthat.
like image 31
Stephen Kirk Avatar answered Nov 25 '22 18:11

Stephen Kirk