yihui / servr

A simple HTTP server in R
https://cran.rstudio.com/package=servr
278 stars 35 forks source link

Start a server without launching browser #54

Closed msgc closed 3 years ago

msgc commented 3 years ago

Hi, everyone.

I have a local folder created with LDAvis, This folder has four files: "d3.v3.js" "index.html", "lda.css", "lda.json" "ldavis.js" (local folder is d <- "C:\Users\msgc\AppData\Local\Temp\RtmpMBtR5L\file53d47f732b79" file53d47f732b79.zip

Executing servr::httd(d) to the local folder works great. The issue is that I am packaging this process with an electron-quick-start. When I execute the httd(d) function it creates the server but the application stops (maybe because is not interactive).

Is there a way to start the server with http(d) in the background (without trying to launch a browser) and then use the resulting url with the browseURL? I am thinking something like: utils::browseURL(httd(d)$url)

Thank you so much in advance.

yihui commented 3 years ago

Please see the help page ?servr::httd. The ... arguments are passed to server_config(), so you can

servr::httd(browser = FALSE, daemon = FALSE)

Then the server won't be shut down until the R process is shut down.

msgc commented 3 years ago

Thank you yihui. When I execute m<-httd(d, daemon = FALSE)$url to save the url that I can then use with browseURL(m) The application shiny app (packaged with electron) expects that I break it with Esc or control + c to keep moving.

I tried adding a set limit to stop the http function after a few second since the server will still be active as follows: setTimeLimit(1, 5, transient = TRUE) m<-httd(d, daemon = FALSE)$url

But shiny breaks with the following error:

stderr:Error in .Call("_later_execCallbacks", PACKAGE = "later", timeoutSecs,  :
  reached elapsed time limit

Is there a way to activate the server without expecting the user to hit escape to continue?

Thank you in advance.

yihui commented 3 years ago

I'm not sure if I fully understand your use case. It seems that you will have a Shiny app running after you launch servr::httd(). In that case, you should use daemon = TRUE, otherwise httd() will hang R and you won't have a chance to run a Shiny app later.

msgc commented 3 years ago

I knew you are a genius and super generous! Thank you! I promised I read all help but could not get it to work (and after a week I opened this thread). Before closing, this I want to post the process and solution based on your input: The shiny app is as follows:

  1. loads corpus,
  2. does text mining
  3. computes topic modeling. as part of 3, it includes httd()

There are some issues with windows and gistr in terms of parsing json. So I needed to deploy json from a local server using httd(): The following solution works (modified serVis function from the LDAvis package:

       serVis2 <- function (json, out.dir = tempfile(),  
                as.gist = FALSE, ...)
            {
                dir.create(out.dir)
                src.dir <- system.file("htmljs", package = "LDAvis")
                to.copy <- Sys.glob(file.path(src.dir, "*"))
                file.copy(to.copy, out.dir, overwrite = TRUE, recursive = TRUE)
                cat(json, file = file.path(out.dir, "lda.json"))
                d<<-(out.dir)
            m<<-httd(d, daemon = TRUE)$url
        }

            serVis2(json_lda)
            print(d) #just to point the tmp local dir
        print(m) #this should be active based on @yihui help!!! Thank you again!
        utils::browseURL(m) 
yihui commented 3 years ago

after a week I opened this thread

I really appreciate it! Next time please don't hesitate to ask earlier. I'm fine with people asking questions after they have done some research beforehand (not necessarily to the point of banging your head against the wall :). I'm happy to answer questions that would take me one minute but take you days. I understand that documentation is sometimes unclear and cannot help all people. Anyway, I'm glad that you figured it out finally! Thanks for your patience and posting back!

mmahmoudian commented 1 year ago

@yihui Sorry to bump an old issue, but I landed here because while using Emacs and running R I got the error about the browser and searching among the issues got me here. Are you open for a PR to add either or both of these:

  1. a sentence or two in the details of the servr::httd help page that explains the browser thing and guide the user
  2. add a sentence to the error message and guide the user to read ?servr::server_config because the error (copied below) does guide the user towards defining the browser, which led me to believe having browser is a must.
> servr::httd()
To stop the server, run servr::daemon_stop(1) or restart your R session
Error in browseURL(url, browser = get_browser()) : 
 'browser' must be a non-empty character string

If you are open to such PR, I will happily make one.

yihui commented 1 year ago

@mmahmoudian I'd like to improve the get_browser() function instead: https://github.com/yihui/servr/blob/640fea1b02b00b329f993e6c48bb67f3310c702c/R/utils.R#L54-L56

I wonder what getOption('browser') is in your Emacs R session. Thanks!