shinyFiles defining a web directory as the root - r

I am trying to create a simple shiny app using shineFiles that allows users to select a file for download from a directory on the web.
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton('files', label='File select', title='Please select a file', multiple=FALSE)
)
server <- function(input, output) {
shinyFileChoose(input, 'files',
roots = (root = c('http://mirrors.vbi.vt.edu/mirrors/ftp.ncbi.nih.gov/blast/db/')),
filetypes=c('', 'txt' , '.gz' , '.md5'))
}
shinyApp(ui = ui , server = server)
However, I am getting the below error:
Listening on http://127.0.0.1:6772
Warning: Error in fileGet: Roots must be a named vector or a function returning one
Stack trace (innermost first):
60: fileGet
59: do.call
58: observerFunc
1: runApp
ERROR: [on_request_read] connection reset by peer
I need help in defining the directory as: http://mirrors.vbi.vt.edu/mirrors/ftp.ncbi.nih.gov/blast/db/

Related

R shinyFiles : Warning: Error in [: object of type 'closure' is not subsettable [No stack trace available]

I want to use shiny to select a local directory and output the files in selected directory.
But I got the following error, what is the problem?
Warning: Error in [: object of type 'closure' is not subsettable [No stack trace available]
Thanks a lot.
library(shiny)
library(shinyFiles)
ui <- shinyUI(bootstrapPage(
shinyDirButton('folder', 'Folder select', 'Please select a folder', FALSE)
))
server <- shinyServer(function(input, output) {
volumes = getVolumes()
shinyDirChoose(input, 'folder', roots= volumes)
})
shinyApp(ui=ui, server=server)
The famous error message "Object of type closure is not subsettable" indicates that you are trying to subset a function. In your case the issue is that getVolumes() returns a function which when called returns a vector of available volumes. To solve your issue change your call of shinyDirChoose like so:
server <- shinyServer(function(input, output) {
volumes = getVolumes()
shinyDirChoose(input, 'folder', roots = volumes())
})

R Shiny GoogleSheets4: Authentication error in Shinyio server on deployment?

Here is my code for a simple test app:
library(shiny)
library(shinydashboard)
library(googlesheets4)
library(googledrive)
library(DT)
drive_auth(email = "xxxx")
shinyws1<-gs4_create("WS1")
#table<-read_sheet("xxx")
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Test App"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
numericInput("bins",
"Number of friends:",
min = 1,
max = 100,
value = 50),
actionButton("submit","Submit",class="btn-success")
),
# Show a plot of the generated distribution
mainPanel(
#blank so far
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#results<-reactive(input$bins)
observeEvent(input$submit,{
shinyws1 %>% sheet_append(as.data.frame(input$bins))
})
}
# Run the application
shinyApp(ui = ui, server = server)
It works okay in my local server. But deployment fails.
Here is the error message generated after deployment in Shinyio server:
Error in value[[3L]](cond) : Can't get Google credentials.
Are you running googledrive in a non-interactive session? Consider:
* `drive_deauth()` to prevent the attempt to get credentials.
* Call `drive_auth()` directly with all necessary specifics.
* Read more in: https://gargle.r-lib.org/articles/non-interactive-auth.html
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
Does anyone know how to fix it? I've tried every workaround I found online, but it didn't work.

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.

How to access the environment of the function that started a Shiny application?

I have a Shiny application that I run by calling a function that calls shiny::runApp. The application can access the global environment, therefore it can access to data in objects that have a name decided in advance. However, I would like to pass it data through the parameters of the function that runs the app. The following example works.
f <- function(param) {
runApp(
list(
ui = fixedPage({
verbatimTextOutput('text')
}),
server = function(input, output) {
output$text <- renderPrint(param)
})
)
}
f("hello")
However, I can not reproduce this behaviour when the ui and server components are loaded from a file:
File contents:
$ cat ui.R
fixedPage({
verbatimTextOutput('text')
})
$ cat server.R
function(input, output) {
output$text <- renderPrint(param)
}
R code:
g <- function(param) {
runApp()
}
g("hello")
Error message:
Listening on http://127.0.0.1:3870
Warning: Error in renderPrint: object 'param' not found
Stack trace (innermost first):
86: renderPrint
85: func
84: eval
83: eval
82: withVisible
81: evalVis
80: utils::capture.output
79: paste
78: origRenderFunc
77: output$text
2: runApp
1: g [#2]
I guess that it has something to do with the fact that the components are not created in similar scopes in the two examples, but I could not find a workaround... In the second example, is there a way where I can access the environment of the function g from the Shiny app ?
Yes. Define the param object in the global environment:
g <- function(param) {
assign("param",param,.GlobalEnv)
runApp()
}
#this now works and print `hello` as intended
g("hello")

Resources