Shiny App Issue, ShinyDirButton is unresponsive - r

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)

Related

Unable to embed an mp4 video in Shiny R from computer

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)

R - shiny app - ERROR: Invalid git repo specification: 'ShinySky'

I try to run a shiny app on R studio, I get the error
"ERROR: Invalid git repo specification: 'ShinySky'"
with the message at the console:
"there is no package called ‘shinysky’
Warning: Error in parse_repo_spec: Invalid git repo specification: 'ShinySky'"
Any ideas how to resolve the issue?
install.packages("shiny")
library(shiny)
ui <- fluidPage(
sliderInput(inputId ="num", label = "Choose the sample size", value=5,
min=1, max=25 ),
plotOutput("hist")
)
server <- function(input, output, session) {
output$hist <- renderPlot({
hist(rnorm(input$num))
})
}
shinyApp (ui, server)

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").

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