Run Shiny Applications natively via the Linux command line

Follow

You may wish to run Shiny apps natively via the command line to help isolate code-related issues when when working with Posit Workbench or Posit Connect. Formally speaking, code-related questions are outside of our support SLA! However, running code natively via the command line can help identify if issues are within code or within the product. Rscript allows you to test shiny applications and run them via the R command line. The following documentation contains more information on this:

https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/Rscript
https://support.posit.co/hc/en-us/articles/218012917

Example Usage

First, let's create our Shiny Web Application. For this example, we will be using a single file application (app.R), however, multiple file web applications can be used (ui.R, and server.R). The file is located in /home/posit/testapp/app.R. The content of this application can be seen below:

library(shiny)

# 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")
)
)
)

# 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',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
}

# Run the application
shinyApp(ui = ui, server = server)

Once we have our application built, we can run this from the Linux command line using Rscript:

Cecil@PositWorkbench:~# R -e "shiny::runApp('/home/posit/testapp')"

This will run your code natively via the R command line:

Cecil@PositWorkbench:~# R -e "shiny::runApp('/home/posit/testapp')"

R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> shiny::runApp('/home/posit/testapp')
Loading required package: shiny

Listening on http://127.0.0.1:5772

In this instance, 5772 is the port provided, however, this will be different for your specific application. Paste the URL into your web browser and you will be able to see your application presented:

Comments