How do I know if an application is running on a local or server? (R Shiny)

I test my application on my laptop and then deploy it to shinyapps server. Before deploying, I need to remove the instruction that sets the path, for example,

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

Is there a way that the code can find out if it works locally or on the server, so that would look like this:

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

A trivial code example (it will not work on the server if setwdit is not deleted):

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(

    )
))
+4
source share
3 answers

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') == ""
+4
source

, , , session$clientData$url_hostname. , , 127.0.0.1, , shinyapps - shinyapps.io.

runApp(shinyApp(
  ui = fluidPage(
  ),
  server = function(input, output, session) {
    observe({
      if (session$clientData$url_hostname == "127.0.0.1") {
        setwd(...)
      }
    })
  }
))

- , ,

+5

You can get the host name and request. Computers must have different host names.

    library(R.utils)
    hname <- System$getHostname()

gives

           nodename 
"mikes-air-3.wisedom.local"
0
source

All Articles