Skip to content

Instantly share code, notes, and snippets.

@johndrummond
Created August 5, 2019 22:44
Show Gist options
  • Save johndrummond/8a8e3254c27d8d6a6141d5942b7ce621 to your computer and use it in GitHub Desktop.
Save johndrummond/8a8e3254c27d8d6a6141d5942b7ce621 to your computer and use it in GitHub Desktop.
demo shiny server app with health handler returning plain json on health path
#
# This is a Shiny web application.
# with a handler that returns plain json
# run and in a browser look for /health/
# e.g. http://127.0.0.1:5732/health/
# from https://stackoverflow.com/users/1455889/amaurel
# on https://stackoverflow.com/questions/19991654/shiny-server-print-json-as-a-result-output
# You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(jsonlite)
# Define UI for application that draws a histogram
ui <- 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")
)
)
)
httpHandler = function(req){
message = list(value="hello")
return(list(status = 200L,
headers = list('Content-Type' = 'application/json'),
body = toJSON(message)))
}
shiny:::handlerManager$addHandler(shiny:::routeHandler("/health",httpHandler) , "a_unique_id")
# Define server logic required to draw a histogram
server <- 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')
})
}
# Run the application
shinyApp(ui = ui, server = server)
@johndrummond
Copy link
Author

Thanks for letting me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment