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.
Related
The result is a black background where the video should be playing.
The video is in the directory of the getwd() in R.
This is the code:
library(shiny)
ui <- shinyUI(
fluidPage(
tags$video(src="videoname.mp4", type = "video/mp4",height ="400px", width="400px",controls="controls")
)
)
server <- shinyServer(function(input, output, session) {})
runApp(list(ui = ui, server = server), launch.browser =T)
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).
This code should produce a pop up that allows Directory Selection for a Shiny App.
However, when I run the code the button appears but is unresponsive in both the R-studio viewer and when run in a web-browser.
Does the code work for you? Is there anything that I am not doing right?
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyDirButton('folder', 'Select a folder', 'Please select a folder', FALSE)
)
server <- function(input, output){
volumes = getVolumes() # this makes the directory at the base of your computer.
observe({
shinyDirChoose(input, 'folder', roots=volumes, filetypes=c('', 'txt'))
print(input$folder)
})
}
shinyApp(ui=ui, server=server)
Thank you in advance
I found a different function that works, choose.dir().
library(shiny)
library(shinyFiles)
ui <- fluidPage(
actionButton("dir", 'select a folder'),
textOutput("wd") #display working directory
)
server <- function(input, output) {
observeEvent(input$dir, {
setwd(choose.dir("c:/")) #selecting a directory
output$wd <- renderText(getwd())})
}
shinyApp(ui = ui, server = server)
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.
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?