Hi!
I’m investigating the possibility to use shiny apps in knime JS views too.
The main problem is that shiny apps use a mini-http server in R, so they run indefinitely, while a KNIME R source node need the R script to end its work.
So I considered the use of R Future library package which allows to run the http server in parallel to the main R script.
In the R script node, you can use the following skeleton for your shiny app:
library(shiny)
library(future)
ui <- fluidPage(
titlePanel("Title"),
#action to close the server
actionButton("close", "Click to disconnect"),
#your UI code...
)
server <- function(input, output) {
#stop server, freeing resources
observeEvent(input$close, {
stopApp()
})
#your server code...
}
#run server in parallel
plan(multisession)
app <- shinyApp(ui, server)
f <- future({
runApp(appDir=app, host=knime.flow.in[["host_ip"]], port=knime.flow.in[["host_port"]], launch.browser=FALSE)
})
“host_ip” and “host_port” are two flow variables, used to configure the R mini-http server . Their value can be 127.0.0.1 and 6767 respectively.
Please note the "Click to disconnect" button, which the user has to press in order to stop the shiny app in the parallel process: this is necessary to avoid the R mini-http server to run indefinitley. I haven’t found yet a better solution
In order to view the shiny app, I connected the R script node to a Generic JS node via a flow variable connector.
The Generic JS node has the following content:
var body = document.getElementsByTagName('body')[0];
var address = "http://$${Shost_ip}$$:$${Shost_port}$$";
var html += "<iframe src=\""+ address +"\" height=\"400\" width=\"800\"></iframe>";
body.innerHTML = html;
There are still important issues that have to be resolved here, expecially when I’m crazy enough to think the usage of shinyApps in a KNIME Server.
It’s frightening, when you think about the uncontrolled parallel R processes spawned by several KNIME jobs! The need to stop them automatically is a prority here . Moreover there would be serious security issues .
Maybe some hacking in shinApp R code might be needed…