Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rselenium can't connect to running shiny app

I configured selenium server in docker. It works ok - I can connect to it, but when I want to interact with running local shiny app Rselenium does not see it. Details below:

I did step-by-step:

  • I run selenium server:

    docker run -d -p 4445:4444 selenium/standalone-chrome

  • successfully connected to selenium server:

remDr <- remoteDriver(remoteServerAddr = "localhost" , port = 4445L , browserName = "chrome" , platform = "MAC")

> remDr$open() [1] "Connecting to remote server"

  • run shiny app from terminal (in separate r session):

> shiny::runApp(file.path(find.package("RSelenium"), "apps", "shinytestapp"), port = 6012) Listening on http://127.0.0.1:6012

  • and then tried to do some tests:

remDr$navigate("localhost:6012") appTitle <- remDr$getTitle()[[1]] expect_equal(appTitle, "Shiny Test App")

and got error:

Error: 'appTitle' not equal to "Shiny Test App". 1/1 mismatches x[1]: "localhost" y[1]: "Shiny Test App"

  • after all I made a screenshot:

remDr$screenshot(display = TRUE)

and it looks like this:

enter image description here

Do you have idea why RSelenium does not see shiny app running locally?

like image 794
Taz Avatar asked Aug 23 '17 22:08

Taz


People also ask

Do you need R to run Shiny app?

But the simplest way to run a Shiny app is to run it locally. You only need the shiny R package installed, and you can run the app in your browser.

How do you run a Shiny app in R studio?

Open the app. R script in your RStudio editor. RStudio will recognize the Shiny script and provide a Run App button (at the top of the editor). Either click this button to launch your app or use the keyboard shortcut: Command+Shift+Enter (Control+Shift+Enter on Windows).

How do you stop a Shiny app from running?

Automatically stop a Shiny app when closing the browser tabBy adding a single line to the server code session$onSessionEnded(stopApp) , a Shiny app will automatically stop running whenever the browser tab (or any session) is closed.

How do you run a Shiny app with two files?

To create a two-file app, create a new directory (for example, newdir/ ) and place two files, called ui. R and server. R , in the directory. To run it, call runApp("newdir") .


1 Answers

I figured it out with a LOT OF HELP from @jdharrison.

First make docker compose file (be careful with indentations - one indention must be 2 spaces) and save as docker-compose.yml:

version: '2'
services:
  ropensci:
    image: rocker/ropensci
    ports:
      - "8788:8787"
    links:
      - selenium:selenium
      - shiny:shiny
  selenium:
    image: selenium/standalone-chrome
    ports:
      - "4445:4444"
    links:
      - shiny:shiny
  shiny:
    image: rocker/shiny
    container_name: shiny
    volumes:
      - ~/Users/username/services/volumes/shiny/apps:/srv/shiny-server/
      - ~/Users/username/services/volumes/shiny/logs:/var/log/
      - ~/Users/username/services/volumes/shiny/packages:/home/shiny/

or download: https://codeshare.io/2j4yLB

then run docker-compose up from folder where docker-compose.yml file is.

  • Add your apps to /home/username/services/volumes/shiny/apps
  • To navigate to your app from selenium use http://shiny:3838/myapp

To check if it works you can save below code as app.R in: ~/Users/username/services/volumes/shiny/apps/example/:

library(shiny)
server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100)
    ),
    mainPanel(plotOutput("distPlot"))
  )
)
shinyApp(ui = ui, server = server)

and run:

library(RSelenium) 
remDr <- remoteDriver(remoteServerAddr = "selenium", port = 4444L, browser = "chrome") 
remDr$open()
remDr$navigate(url = "http://shiny:3838/example")
remDr$screenshot(display = TRUE)

If everything is ok you should see screenshot: enter image description here

like image 121
Taz Avatar answered Sep 29 '22 19:09

Taz