shiny sourced function stdout to logfile to UI issue - r

I made a portable shiny app with portable chrome and R. I launch rscript with vbs script in which stdout is redirected to a logfile with:
RScriptFile & " 1> " & logfile & " 2>&1"
What I want to do is to read in the log file so that the user has feedback about the calculation progress. The following reactivePoll works if the "main" shiny script is writing to the console, but it does not if a sourced function writes. Note that both are displayed in the log file, therefore the fault is surely not in the redirection, but somewhere in my logic or in shiny.
If I set bool_for_testing = T the test button works, on the other hand if I set it False not even the first "Go!" is printed on the console until the script is finished, but both are displayed real time in the logfile:
library(shiny)
bool_for_testing = F # note its use below!
logfile_name = "some_log_file_that_exists" # this is created by the vbs (command line) script!
ui = shinyUI(fluidPage(
actionButton("btn_test", "push meh!"),
fluidRow(
column(12, wellPanel(style = "overflow-y:scroll; max-height: 600px",
tags$b(paste("log file is this one:", logfile_name)),
verbatimTextOutput("logText")
))
)
)
) # end UI
server = shinyServer(function(input, output, session) {
observe({
if(input$btn_test > 0){
if(bool_for_testing) {
# this works!
print("Go!")
} else {
# not a letter on log here
print("Go!")
source("some_barely_interesting_function.R")
some_barely_interesting_function()
}
}
})
logFile <- reactivePoll(1000, session,
checkFunc = function() {
if (file.exists(logfile_name))
# should always refresh
runif(1) # Sys.time() does not work either
else
""
},
valueFunc = function() {
readLines(logfile_name)
}
)
# # this does not work either
# fileReaderData <- reactiveFileReader(500, session,
# logfile_name, readLines)
output$logText <- renderText({
text <- logFile()
paste(text, collapse = '\n')
})
session$onSessionEnded(function() {
# for portable R and chrome application
stopApp()
q("no")
})
})
shinyApp(ui = ui, server = server)
If I manually open the log file it shows the progress continuously, therefore I think the GUI thread is held up until the sourced calculation is finished (if I open a new shiny window only for reading the log file, it will work well, too). Is this truly the case, and is there a workaround for this?

Related

Return system console output to user interface

I have a bash script that i'm running using shiny and system. It takes a long time to run so I'd like to provide feedback to the user about progress. In the bash script I have messages that periodically updates the user and I'm trying to find a way to have them printed in the UI.
Here is a minimal working example for which I'd like to have "Output 1" and "Output 2" returned to the user as they appear in the console.
Any help is greatly appreciated.
library(shiny)
ui <- fluidPage(
actionButton("run", "Print to Console")
)
server <- function(input, output, session) {
observeEvent(input$run,{
system(c("echo output 1; sleep 2; echo output 2"))
})
}
shinyApp(ui, server)
I'd suggest to run your system command asynchronously and redirect the output to a log file. In parallel you can continuously read in the logfile via reactiveFileReader.
In contrast, when intern = TRUE the R session (and shiny) is blocked while the command is executed.
Please check the following:
library(shiny)
file.create("commands.sh", "output.log")
Sys.chmod("commands.sh", mode = "0777", use_umask = TRUE)
writeLines(c("#!/bin/bash", "echo output 1","sleep 2", "echo output 2"), con = "commands.sh")
ui <- fluidPage(
actionButton("run_intern", "Run intern"),
textOutput("myInternTextOutput"),
hr(),
actionButton("run_extern", "Run extern"),
textOutput("myExternTextOutput")
)
server <- function(input, output, session) {
systemOutputIntern <- eventReactive(input$run_intern,{
system(command = "echo output 1; sleep 2; echo output 2", intern = TRUE)
})
output$myInternTextOutput <- renderText(systemOutputIntern())
observeEvent(input$run_extern,{
system(command = "./commands.sh 2>&1 | tee output.log", intern = FALSE, wait = FALSE)
})
log <- reactiveFileReader(200, session, filePath = "output.log", readLines)
output$myExternTextOutput <- renderText(log())
}
shinyApp(ui, server)
PS: As an alternative you might want to check AsyncProgress from library(ipc).

How to use the results of a reactive expression in an output

I'm trying to build a Shiny App that monitors my running containers on a host machine, and here is what I have tried so far. Question is how can I display the outputs, in this case (docker ps -a) contents in the mainPanel? I'm new to Rshiny, so any help will be much appreciated!
library(shiny)
library(ssh)
ui <- fluidPage(
titlePanel("Dashboard"),
sidebarLayout(
sidebarPanel(
actionButton("bttn", "Click")
),
mainPanel(
h1("Running containers"),
textOutput("dispContainers")
)
)
)
# Define server logic
server <- function(input, output, session) {
session <- ssh_connect("jeroen#dev.opencpu.or")
observeEvent(input$bttn, {
ssh_exec_wait(session, command = 'docker ps -a')
})
}
# Run the application
shinyApp(ui, server)
You can use the other ssh_exec_internal instead to collect the output of your docker command. Something like:
server <- function(input, output, session) {
session <- ssh_connect("jeroen#dev.opencpu.or")
text_output <- reactiveVal("")
observeEvent(input$bttn, {
response <- ssh_exec_internal(session, command = 'docker ps -a')
text_output(rawToChar(response$stdout))
})
output$dispContainers <- renderText(text_output())
}
You might also want to switch textOutput to verbatimTextOutput to get the text as you'd see it in a terminal.

Store user-upload file to specified local folder

I want to create an app where User A uploads a file to server and User B can download it to a local folder.
I am first implementing the actions of uploading the file, and then immediately store that file to my own specified folder (since I am the sole User here). This is the code:
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose csv File',
accept=c('text/csv'))
)
server <- function(input , output){
rootDir <- 'C:/RShiny/Dir'
inFile <- reactive({input$file1})
file.copy(inFile()$datapath,
file.path(rootDir, inFile()$name, fsep = .Platform$file.sep))
}
shinyApp(ui = ui , server = server)
However, I keep receiving this error message:
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
53: stop
52: .getReactiveEnvironment()$currentContext
51: getCurrentContext
50: .dependents$register
49: inFile
47: server [C:\RShiny\.../app.R#12]
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
And the app just closes right away. Not sure what it means and how to fix it. If someone could help explaining?
Thanks,
Please try the below:
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose csv File',
accept=c('text/csv'))
)
server <- function(input , output){
rootDir <- 'C:/RShiny/Dir'
inFile <- reactive({input$file1})
observe({
file.copy(inFile()$datapath,
file.path(rootDir, inFile()$name, fsep = .Platform$file.sep))
})
}
shinyApp(ui = ui , server = server)
You need to put the file.copy() code in an observe ("a reactive expression or observer").

Get PID for subprocesses for asynchronous futures in R shiny

server <- function(input, output, session) {
out1_rows <- reactiveVal()
observeEvent(input$run1, {
prog <- Progress$new(session)
prog$set(message = "Analysis in progress",
detail = "This may take a while...",
value = NULL)
fut1 = future({
system(paste("Command1" , input$file ">", "out1.txt"))
system(paste("Command2" , out1.txt ">", "out2.txt"))
head_rows <- read.delim("out2.txt")
return(head_rows)
}) %...>%
out1_rows() %>%
finally( ~ prog$close())
NULL
})
observeEvent(req(out1_rows()), {
output$out_table <-
DT::renderDataTable(DT::datatable(
out1_rows(),
)
))
observeEvent(input$cancel, {
async_pid <- fut1$job$pid ##this is empty
#async_pid <- Sys.getpid() ##this return PID for main process and kills "/opt/shiny-server/R/SockJSAdapter.R" but not for subprocesses inside future()
system(paste("kill -15", async_pid))
})
}
Here, i would need to kill the process running the commands inside future(). I tried in the above way to fetch the PID running the future() process and kill when input$cancel is triggered. However, fut1$job$pid is not returning any PID value and hence the kill operation is not successful.
This link from future vignettes shows how to fetch PID for future() jobs. However, in my case i am not able to use Sys.getpid() inside future() as i am not sure how to store the PID value as the process is already returning some output from my system commands.
This page future GIT shows an alternate way of External Kill with the syntax fut1$job$pid. But this fails to fetch the PID.
I couldn't figure this out after trying different ways or blinded with the syntax. Could someone hint the way to do this.
Can you please provide us with a full reproducible example?
You might want to have a look at library(future.callr):
using plan(callr) you can get the pid and kill the process like this:
library(future)
library(promises)
library(future.callr)
plan(callr)
myFuture <- future({
Sys.sleep(5)
return(runif(1))
})
myFuture$process$get_pid()
myFuture$process$is_alive()
# myFuture$process$kill()
# myFuture$process$is_alive()
then(myFuture, onFulfilled = function(value){
print(value)
}, onRejected = NULL)
Edit - adapted from your code:
library(shiny)
library(DT)
library(future)
library(promises)
library(future.callr)
library(shinyjs)
library(V8)
plan(callr)
ui <- fluidPage(
useShinyjs(),
titlePanel("Trigger & kill future"),
sidebarLayout(
sidebarPanel(
actionButton(inputId="run1", label="run future"),
actionButton(inputId="cancel", label="kill future")
),
mainPanel(
dataTableOutput('out_table')
)
)
)
server <- function(input, output, session) {
disable("cancel")
out1 <- reactiveValues(rows=NULL)
observeEvent(input$run1, {
disable("run1")
enable("cancel")
out1$rows <- NULL
prog <- Progress$new(session)
prog$set(message = "Analysis in progress",
detail = "This may take a while...",
value = NULL)
fut1 <<- future({
# system(paste("Command1" , input$file, ">", "out1.txt"))
# system(paste("Command2" , out1.txt, ">", "out2.txt"))
# head_rows <- read.delim("out2.txt")
head_rows <- data.frame(replicate(5, sample(runif(20, 0, 1), 20, rep=TRUE)))
Sys.sleep(5)
return(head_rows)
})
print(paste("Running async process with PID:", fut1$process$get_pid()))
then(fut1, onFulfilled = function(value){
out1$rows <<- value
}, onRejected = function(error){NULL})
finally(fut1, function(){
prog$close()
disable("cancel")
enable("run1")
})
return(NULL)
}, ignoreInit = TRUE)
observeEvent(req(out1$rows), {
output$out_table <- DT::renderDataTable(DT::datatable(out1$rows))
})
observeEvent(input$cancel, {
async_pid <- fut1$process$get_pid()
print(paste("Killing PID:", async_pid))
# system(paste("kill -9", async_pid)) # Linux - kill
# system(paste("taskkill /f /pid", async_pid)) # Windows - kill
fut1$process$kill() # library(future.callr) - kill
out1$rows <- NULL
disable("cancel")
enable("run1")
}, ignoreInit = TRUE)
}
shinyApp(ui = ui, server = server)
Errors occuring occasionally:
Unhandled promise error: callr failed, could not start R, exited with non-zero status, has crashed or was killed
Warning: Error in : callr failed, could not start R, exited with non-zero status, has crashed or was killed
95: <Anonymous>
Maybe this statement from #HenrikB tells us what we are running into:
However, to get this working properly you probably also need to make
your future expression / future code interrupt aware using
withCallingHandlers() etc. It'll also unknown to me what happens if
you signal too many interrupts in a row - it might be that you manage
to interrupt the main R-loop of the worker, which then will cause the
R worker to terminate. That'll result in a missing R worker and you've
got that problem you mention at the beginning.
The error is also mentioned here but currently in the future.callr-context I don't know how to work around it.
2nd Edit:
By now I got some further feedback from Henrik Bengtsson. He once again mentions that the core future API currently isn't supporting the termination of futures. So, in the end no matter what backend we use, we might run into problems.
Disregarding this info, I'd have another look at the library(ipc) vignette which provides two examples on the topic Killing a long running process. But I pointed to this here already - which probably led to this question.
In the end all of this might be useless regarding your scenario because you are using system() calls which create subprocesses of their own (and have their own pid accordingly). Therefore why don't you use wait = FALSE in your system command (as I mentioned in the comments here already) to get your async behaviour and catch their pid using something like myCommand & echo $! (see this). This way you don't need any futures.
Could find the PID by insetring cat() as such:
future({
cat('future process PID \n', Sys.getpid(), '\')
<expensive operation>
})
the PID number will appear in console when running the app. This is how I have found out that, in my case, the PID outside and inside the future were identical.

How to remove tags in textOutput - RShiny

I am new to R and I am actually developing a page where a directory (string characters like "xx/xx") is given in the server and I want to take back this directory to include it in the source of my ui app.
UI:
library(shiny)
file<-textOutput("paramfile")
source(file(file), local = TRUE, encoding = 'UTF-8')
SERVER :
filedir<-renderText({
"entries/5429_param.R"
})
output$paramfile<-renderText({
filedir()
})
I then have an error :
"Warning in file(filename, "r", encoding = encoding) : cannot open
file '< div id="paramfile" class="shiny-text-output">< /div>':
Invalid argument
Error in file(filename, "r", encoding = encoding) :
cannot open the connection"
Do you know how can I remove those tags or if there is another function that can allow me to take a string in the server and to include it into a source.
Here is a basic example of a ShinyApp:
# Example of UI with fluidPage
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("dir", label = "Enter a directory")
),
mainPanel(
verbatimTextOutput("dirPrint"),
verbatimTextOutput("lsFiles")
)
)
)
# Server logic
server <- function(input, output) {
output$dirPrint <- renderPrint({
print(input$dir)
})
output$lsFiles <- renderPrint({
fls <- list.files(input$dir)
print(fls)
})
}
# Complete app with UI and server components
shinyApp(ui, server)
If you enter the path of a directory in the textinput, the second renderPrint function is showing all files, that are found at that path.
I would suggest you go over the Shiny-Tutorials, as there seem to be some syntax-problems in your code and I am not sure what exactly you want to achieve.

Resources