$30 off During Our Annual Pro Sale. View Details »

R Not Only in Production

Kara Woo
September 20, 2023

R Not Only in Production

I will share what our team has learned from successfully integrating R in all areas of our company's operations. InsightRX is a precision medicine company whose goal is to ensure that each patient receives the right drug at the optimal dose. At InsightRX, R is a first-class language that's used for purposes ranging from customer-facing products to internal data infrastructure, new product prototypes, and regulatory reporting. Using R in this way has given us the opportunity to forge fruitful collaborations with other teams in which we can both learn and teach.

Kara Woo

September 20, 2023
Tweet

More Decks by Kara Woo

Other Decks in Technology

Transcript

  1. Kara Woo


    posit::conf(2023)
    R not only in production

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. How can the strengths of R, and the
    people who use it, influence the
    organization as a whole?

    View Slide

  6. Meet Angela

    View Slide

  7. View Slide

  8. Vancomycin can treat MRSA

    View Slide

  9. View Slide

  10. There’s no one dose that works for everyone

    View Slide

  11. We use pharmacological models +
    patient’s unique characteristics to
    recommend the optimal dose

    View Slide

  12. View Slide

  13. Patient data, lab


    results, past doses

    View Slide

  14. Patient data, lab


    results, past doses
    Recommended dose

    View Slide

  15. R is core to our product

    View Slide

  16. R is core to fulfilling our mission

    View Slide

  17. View Slide

  18. ETL

    View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. • REST API


    • Data processing pipeline


    • Analyses and new models


    • Model validation


    • Regulatory reporting


    • New product prototypes


    • …

    View Slide

  25. View Slide

  26. View Slide

  27. {
    "firstName": "First name",
    "lastName": "Last name",
    "dob": "Date of birth",
    "changeMyPassword": "Change my password",
    "logout": "Logout",
    ...
    }
    {
    "firstName": "Vorname",
    "lastName": "Nachname",
    "dob": "Geburtsdatum",
    "changeMyPassword": "Ändere mein Passwort",
    "logout": "Ausloggen",
    ...
    }

    View Slide

  28. {
    "firstName": "First name",
    "lastName": "Last name",
    "dob": "Date of birth",
    "changeMyPassword": "Change my password",
    "logout": "Logout",
    ...
    }

    View Slide

  29. {
    "firstName": "First name",
    "lastName": "Last name",
    "dob": "Date of birth",
    "age": "Age",
    "changeMyPassword": "Change my password",
    "logout": "Logout",
    ...
    }
    👀

    View Slide

  30. {
    "firstName": "First name",
    "lastName": "Last name",
    "dob": "Date of birth",
    "age": "Age",
    "changeMyPassword": "Change my password",
    "logout": "Logout",
    ...
    }
    {
    "firstName": "Vorname",
    "lastName": "Nachname",
    "dob": "Geburtsdatum",
    "changeMyPassword": "Ändere mein Passwort",
    "logout": "Ausloggen",
    ...
    }
    👀

    View Slide

  31. {
    "firstName": "First name",
    "lastName": "Last name",
    "dob": "Date of birth",
    "age": "Age",
    "changeMyPassword": "Change my password",
    "logout": "Logout",
    ...
    }
    {
    "firstName": "Vorname",
    "lastName": "Nachname",
    "dob": "Geburtsdatum",
    "age": "Alter",
    "changeMyPassword": "Ändere mein Passwort",
    "logout": "Ausloggen",
    ...
    }
    👀

    View Slide

  32. The mindset of R as a first class
    language empowers us to solve
    problems.

    View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. We can do this in R!

    View Slide

  37. 💡


    Idea

    View Slide

  38. 💡


    Idea
    “It works


    on my


    machine”

    View Slide

  39. 💡


    Idea
    “It works


    on my


    machine”
    ? Final


    product
    ?
    ?

    View Slide

  40. 💡


    Idea
    “It works


    on my


    machine”
    ? Final


    product
    ?
    ?

    View Slide

  41. Reliability
    Deployability

    View Slide

  42. We need to detect
    kidney injury accurately,
    all the time.

    View Slide

  43. View Slide

  44. Elevated creatinine suggests kidney damage
    Easy, right?
    • Data gaps


    • Out of order data


    • Data in di
    ff
    erent units


    • Missing baseline


    • Non-numeric data


    • Data entry errors

    View Slide

  45. test_that("AKI stage is calculated correctly", {
    result <- calc_aki_stage(
    scr = c(0.7, 1.0),
    t = c(0, 24),
    egfr = c(60, 40)
    )
    expect_equal(result$stage, "stage 1")
    })

    View Slide

  46. test_that("AKI stage is calculated correctly", {
    result <- calc_aki_stage(
    scr = c(0.7, 1.0),
    t = c(0, 24),
    egfr = c(60, 40)
    )
    expect_equal(result$stage, "stage 1")
    })

    View Slide

  47. test_that("AKI stage is calculated correctly", {
    result <- calc_aki_stage(
    scr = c(0.7, 1.0),
    t = c(0, 24),
    egfr = c(60, 40)
    )
    expect_equal(result$stage, "stage 1")
    })

    View Slide

  48. test_that("AKI stage is calculated correctly", {
    result <- calc_aki_stage(
    scr = c(0.7, 1.0),
    t = c(0, 24),
    egfr = c(60, 40)
    )
    expect_equal(result$stage, "stage 1")
    })

    View Slide

  49. test_that("stage correct: 0.3 mg/dl rise", {
    # ...
    })
    test_that("stage correct: 1.5x baseline rise", {
    # ...
    })
    test_that("sorts times correctly", {
    # ...
    })
    test_that("handles non-numeric inputs", {
    # ...
    })
    test_that("handles only 1 observation", {
    # ...
    })

    View Slide

  50. Jenny Bryan
    “If you use software that lacks automated
    tests, you are the tests.”

    View Slide

  51. Get latest


    code
    Build the


    R package
    Run the


    tests
    Continuous integration

    View Slide

  52. 5d01863 Add function to detect AKI ✅
    a28vf11 Rewrite AKI function to improve
    performance

    682hcKa Fix handling of missing data ✅

    View Slide

  53. Continuous integration
    • Find bugs quickly


    • Be lazy free up time for more important work


    • Make sure the code continues to behave as intended

    View Slide

  54. 💡


    Idea
    “It works


    on my


    machine”
    ? Final


    product
    Testing
    Continuous


    Integration

    View Slide

  55. 💡


    Idea
    “It works


    on my


    machine”
    Docker Final


    product
    Testing
    Continuous


    Integration

    View Slide

  56. R


    R packages


    your code
    Docker

    View Slide

  57. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile

    View Slide

  58. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile

    View Slide

  59. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile

    View Slide

  60. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile

    View Slide

  61. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile

    View Slide

  62. FROM rstudio/plumber:v1.1.0
    RUN Rscript -e "install.packages('clinPK')"
    COPY . /myproject
    WORKDIR /myproject
    ENTRYPOINT ["Rscript", "entrypoint.R"]
    Dockerfile
    https://github.com/karawoo/dockerdemo

    View Slide

  63. Image: Royyan Razka, Noun Project

    View Slide

  64. Get latest


    code
    Build the

    R package
    Run the


    tests
    Continuous integration

    View Slide

  65. Get latest


    code
    Build the


    docker image
    Run the


    tests
    Continuous integration

    View Slide

  66. 💡


    Idea
    “It works


    on my


    machine”
    Docker Final


    product
    Testing
    Continuous


    Integration

    View Slide

  67. View Slide

  68. View Slide

  69. We are motivated to answer
    questions

    View Slide

  70. View Slide

  71. Database query was too slow

    View Slide

  72. Database query was too slow


    Added an index to the database

    View Slide

  73. Database query was too slow


    Added an index to the database


    Data processing pipeline was too slow

    View Slide

  74. Database query was too slow


    Added an index to the database


    Data processing pipeline was too slow


    Removed the index

    View Slide

  75. View Slide

  76. If you can’t make changes because
    you’re afraid of breaking something,
    it’s already broken.

    View Slide

  77. View Slide

  78. We can visualize and
    communicate our findings

    View Slide

  79. View Slide

  80. Tests
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })

    View Slide

  81. Tests
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    test_that("...", {
    # ...
    })
    become data

    View Slide

  82. View Slide

  83. We are used to dealing with
    uncertainty

    View Slide

  84. View Slide

  85. View Slide

  86. Motivated to answer questions


    Skilled at visualizing and communicating results


    Able to handle uncertainty

    View Slide

  87. View Slide

  88. Using R throughout the stack =
    doing science throughout the stack

    View Slide

  89. View Slide

  90. pos.it/slido-keynote

    View Slide