Can we check shiny applications using testthat and usethis

Hi all,

I have a concern. Let me say I have an application that has some errors (That I cannot identify easily). Is there a way to capture what might be the error. Below are codes (right code and wrong code). I think we can make use to testthat package to capture this.

Right code

ui.r


# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
       sliderInput("bins",
                   "Number of bins:",
                   min = 1,
                   max = 50,
                   value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))

server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
   
  output$distPlot <- renderPlot({
    
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
    
  })
  
})

The above code is correct and I make some error to the above server.R file as shown below

Wrong code (input is not declared properly input$bi

server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
   
  output$distPlot <- renderPlot({
    
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bi + 1)  # wrong input
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
    
  })
  
})

In the above server.R code is there a way to check this through testthat, telling there is wrong in input declaration. Or is there any alternate to handle this?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.