gaut
gaut

Reputation: 5958

R starting and stopping a shiny app via cmd / shell

I start a shiny app from my main R script using

system("R -e shiny::runApp(launch.browser=T,port=5000)", wait=F)

How can I later on stop this app from the same main R script? I tried both

shiny::stopApp()
system("R -e shiny::stopApp()")

Without success (hanging)

Upvotes: 1

Views: 801

Answers (1)

gaut
gaut

Reputation: 5958

This works for me on Windows based on the port number

# start demo UI ----------------------------------------------------------------
library(dplyr)
log_info("starting demo app")
shell("R -e shiny::runApp(launch.browser=T,port=5000)", wait=F)

# mingle with your app here

log_info("quitting demo app")
netstat <- system("netstat -ano", intern = T)
ns <- netstat[grepl("127.0.0.1:5000",netstat)] %>% strsplit(" ") 
ns <- lapply(ns, function(x) x[x!=""]) 
ns <- suppressMessages(sapply(ns, rbind) %>% t %>% as_tibble(.name_repair = "unique"))
pids <- ns[,5] %>% unique %>% as.data.frame() %>% unlist()

sapply(pids, function(i) system(paste0("taskkill /F /PID ",i)))

To fully quit chrome:

system("taskkill /F /IM chrome.exe /T")

Upvotes: 1

Related Questions