rshiny

How to know if the app is running at local or on server? (R Shiny)


I test my app on my laptop, and then deploy it to shinyapps server. Before deploying, I need to remove the statement setting the path, e.g.,

setwd('/Users/MrY/OneDrive/Data')

Is there a way the code can find out if it's running locally or on server, so that it will be like:

if (isLocal()) {
       setwd('/Users/MrY/OneDrive/Data')
}

A trivial sample code (it will fail on server if setwd isn't removed):

server.R

library(shiny)

setwd('/Users/Yuji/OneDrive/Data/TownState')  

data = 'data1.csv'  # to test, using an empty .csv file

shinyServer(function(input, output) {


}) 

ui.R

library(shiny)

shinyUI(pageWithSidebar(
    headerPanel("Click the button"),

    sidebarPanel(
        actionButton("goButton", "Go!")
    ),
    mainPanel(

    )
))

Solution

  • The standard way to do this in Shiny is with: Sys.getenv('SHINY_PORT'). You could write something like:

    is_local <- Sys.getenv('SHINY_PORT') == ""