Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design...

36
Be The Best With Shinytest Chris Campbell Senior Consultant ccampbell@mango- solutions.com @CSJCampbell

Transcript of Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design...

Page 1: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Be The Best With Shinytest

Chris CampbellSenior Consultant

[email protected]

@CSJCampbell

Page 2: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Be The Best With Shinytest

Web Development

Shiny

Testing

Shinytest

Page 3: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Web Development

Page 4: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Two Roles for Data Scientists

• Analyst • Developer

Page 5: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Analyst

• design and build workflow – asking a question– gathering data– generating insight– guiding decision making

• investigate impact of assumptions and determine value of workflow

• use existing tools and develop new tools

Page 6: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Developer

• design algorithms that are independent of data

• identify desirable behaviours of algorithm, recognise edge cases

• abstract algorithm into conceptual units• create and document code• test units to demonstrate expected

behaviour• maintain code as software dependencies

and needs of business change

Page 7: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

More Roles

• Analyst • Web Developer

• Developer

Page 8: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Web Developer

• wireframe/layout a web application• write HTML and CSS• turn a design into a working website• add interactivity to a website with JavaScript or

jQuery• write an application in an object-oriented

programming language (Ruby, Python or PHP)• work with a version control system like Git• manage a domain’s DNS settings• deploy a website to a website hosthttps://teamtreehouse.com/community/the-list-of-minimum-skills-you-need-to-master-to-call-yourself-a-web-developer

Page 9: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shiny

Page 10: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shiny

• Massively popular web application framework

• Winston Chang, Joe Cheng, ...

Page 11: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Web Developer

• wireframe/layout a web application• write HTML and CSS• turn a design into a working website• add interactivity to a website with JavaScript or

jQuery• write an application in an object-oriented

programming language (Ruby, Python or PHP)• work with a version control system like Git• manage a domain’s DNS settings• deploy a website to a website host

Page 12: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Web Developer

• shiny• shiny• shiny• shiny• write an application in R

• work with a version control system like Git• manage a domain’s DNS settings• deploy a website to a website host

Page 13: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,
Page 14: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,
Page 15: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Soon Every R Conference Presentation will be about Shiny

Page 16: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Testing

Page 17: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Testing

• Define expected output• Compare object with expected• Fast and fun

Page 18: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Testing Vital for Effective Development• Find defects• Manage impact of new features• Manage impact of changes to

dependencies

Page 19: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Testing Essential for Validation

• Demonstrate features are present• Qualify operation of installed application

Page 20: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Quick to Check Tests Pass

> library(testthat)> test_package("mangoNCA") test AUCInfObs: ......... test AUCInfPred: ......... test AUCLast: ....................test AUCLin: .............. test AUCLog: ................

Page 21: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Clear Failures

Failed --------------------------

1. Failure: check selectPoints(@test_selectPoints.R#25) --------

test3$lamznpt not equal to 4.

1/1 mismatches [1]

3 - 4 == -1

Page 22: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shinytest

Page 23: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shinytest

• R-based testing framework for Shiny• Gabor Csardi

Shinytest

Page 24: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shinytest

• Snapshot based testing• Record– script of test actions– app state

• Replay and compare

Page 25: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Not yet on CRAN

library(devtools)

install_github("rstudio/webdriver")

install_github("rstudio/shinytest")

Page 26: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Create New Test

• Start recording• Interact with Shiny app• Take snapshots

Page 27: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Start Recording

> recordTest("Quiz")

Page 28: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Interact with Shiny app

• Click through app

Page 29: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Take Snapshot

• Recorder app

• Snapshot and continue or exit

Page 30: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Script of Test Actions

app <- ShinyDriver$new("..")

app$snapshotInit("mytest")

app$setInputs(

q1 = "reg = read.csv('Temp/reg.csv')")

app$setInputs(button = "click")

app$snapshot()

Page 31: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Test Directory Created in App

• Test script• App data• Screenshot

Page 32: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Running Tests

• Test driver process• Shiny server process• Web browser client process

Page 33: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Quick to Check Tests Pass

testApp("Quiz", "mytest")

Running mytest.R

====== Comparing mytest ======

Page 34: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Clear Failures

testApp("Quiz", "mytest")Running mytest.R====== Comparing mytest ======Differences detected between

mytest-current/ and mytest-expected/:

Name Status 001.json != Files differ001.png != Files differ

Page 35: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

Shinytest

• Automated testing framework for Shiny apps

• All necessary tools installable from R• Outputs can be used with other test

frameworks• Best development practice makes the

best web apps

Page 36: Supporting the R Community - Be The Best With Shinytest · 2018. 4. 10. · Developer •design algorithms that are independent of data •identify desirable behavioursof algorithm,

References

• https://github.com/rstudio/shinytest• https://rstudio.github.io/shinytest/• https://www.mango-

solutions.com/datascience/products/valid-r/