Upload files with shinyFiles - r

Can files be uploaded to server using the shinyFiles package? I'm looking for similar functionality to the standard fileInput from the shiny package. The closest thing I could find in shinyFiles:
app.r
library(shiny)
library(shinyFiles)
server <- function(input, output, session) {
shinyFileSave(input, 'save', session=session, roots=c(wd='.')) }
ui <- bootstrapPage(shinySaveButton('save', 'Save', 'Save as...'))
shinyApp(ui=ui,server=server)
But this only allows me to browse the server files (not local) and even when I save I don't see the file created.

The short answer is no (sorry about that)... shinyFiles is simply a browser for the server file system, mainly intended for use when a shiny app is meant for local use (to avoid the overhead of file copying required in native web implementations).
The functionality of shinyFileSave is simply to let the user specify a non-existing filename at a certain location and pass that information back to the server - It's up to the server logic to handle that information in a meaningful way, by creating a file at the specified location.
You could somehow couple shinyFileSave and fileInput to upload a file and put it in a specific location, but the UI for this would probably be quite messy as it would inevitably mix shinyFiles/Bootstrap ui with native ui elements. As the local filesystem is guarded from Javascript code for security reasons this would be the only approach though as long as the server and client resides at different locations...

Related

Calling global.R in an app.R file on shiny server; unable to call dataframes in for global use

I am developing a shiny app which is an exploratory data analysis tool. At this point, I am trying to optimise so that data is loaded in once but is readily available for all users. I am aware of the scoping materials on this i.e. Scoping I have the code to load the data in for the app within a global.R file like so:-
#global.R file
library(feather)
dataframe1<-read_feather("df1.feather")
dataframe2<-read_feather("df2.feather")
These are the dataframes that I need to call once and make available to all users of the app. However, I am calling the ui and the server code all within app.R and I have a hunch that this could be problematic.
I have tried a few implementations as guided by what I have been able to see on the web but it is obvious that I am not doing something properly. Here are some examples of what I have tried
source('global.R', local = T)
ui<-fluidPage({
#code for ui
})
server<-function(input,output,server){
# code for server
}
shinyApp(ui,server)
Where the outcome is this:-
The second thing I have tried is having the global file called in onStart parameter in the shinyApp(), but I have the same error.
The global file is saved in the GitLab repo that I am using to deploy this app on shiny server. The app.R script Can any one show me how I can correctly call the global.R file, so that the data is loaded in once and made available to all users of the app? Do I need to split the app.R file into a ui.R and a server.R file?

Change the directory of a file to be saved

I have a shiny app that I deployed as a library. Inside my shiny app, I write a json file in real time, I want to write this file in the working directory of each user. Nevertheless, the file is written in the directory of the library.(Please refer the code below)
app.R
library(Tree)
ui <- htmlTemplate("www/Tree.html",
text_output = tableOutput("table2")
)
server <- function(input, output, session){
# This block fires each time we receive a message from JavaScript
output$table2 <- renderTable({
#Write json file
json_value = input$jsonData
write(json_value, paste0(getwd(),"/",fileName, ".json"))
})
}
# Run the application
shinyApp(ui , server)
I though that with the function getwd()I will be able to see my json file in my working directory but taht is not happening. My json file is written in the directory of the library
This directory
/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Tree/
How can I change my code to be able to see my jsonfile in the RProject which the user wants to work?
A shiny app is running in your browser and, as a concequence, subject to strict safety regulations imposed by the browser. There are plenty of restrictions regarding the file system to make sure that web pages cannot freely roam your hard drive. That is why there might not be a solution with a good user experience to achieve what you want.
The safest solution would be the download button (as suggested by #HubertL).
If you want to work with the users' home directory you can get the path with file.path(Sys.getenv("HOME")) (at least on Windows it works). Now you can work with that path. Use OutputPath <- file.path(Sys.getenv("HOME"), fileName) to create the path %HOME%/fileName.
The third alternative (I can think of) is a simple textInput. Users can paste a string there that represents the path to the directory where they want the file to be stored. Check the string with dir.exists and use it if it is valid.

How can be created an independent directory for each user in Shiny app?

I'm building a shiny app that create multiple files depending on the uploade file by the user. The created files has the same names and this can make that when a user download the files end with a none relative information to his data.
How can be created an independent directory for each user in Shiny app?
I have found the next solution so far to create the directory to store the files:
directory <- paste0(format(Sys.time(),"%Y%m%d%H%M%S"),rnorm(1),
rnorm(1))
dir.create(directory)
setwd(directory)
But I have read that this may not work if I upload the app to a server. How should I do it?
This is the answer that I get in RStudio community and worked for me:
By #pieterjanvc from RStudio community:
Setting the working directory is likely not going to work like that in Shiny. I suggest you generate a folder based off the user's session token, which is generated when a user connects to a Shiny app and located in the session variable.
library(shiny)
ui <- fluidPage(
)
server <- function(input, output, session) {
dir.create(session$token)
file.create(paste0(session$token, "/userFile.txt"))
}
shinyApp(ui, server)
Once you created the directory, you can save any file in that one using again the token which is the name of the base folder for that user. You should remember to erase the folder after the task finished, or you will have a lot of folders soon.

Have shiny load from a file not named app.R

Does anyone know i it is possible to change the default file that shiny loads?
I was hoping for a bit more flexibility than one file per directory.
It depends on your setup.
Setup 1: Run app locally from a file
If you want to run an application locally (inside an interactive R session) you can use the command
shiny::shinyAppFile("path/to/my/appFile.R")
to load an application. The app file does not have to be named app.R in that case. Note however that with this approach all relative paths (for example image paths) will be resolved relative to your working directory rather than relative to the app's directory.
Setup 2: Run app on a server
If the app shold be run via shiny-server (or shinyapps.io) things are more complicated. In this case the server will expect the app to be defined either as app.R or ui.R/server.R in order to be loaded properly. The only workaround I am aware of here is to use shinyAppFile inside app.R but this might not be very useful in most situations.
Setup 3: Define the app as an object
You can also define an app as an R object and invoke it by printing the object.
someAppObj <- shinyApp(ui = fluidPage(), server = function(...) {})
## start the app by printing it
someAppObj
As mentioned in the answer of #ismirsehregal, you can also use runApp instead of the printing method which will take care if relative paths and handle the app-environment slightly differently.
runApp(someAppObj)
Setup 1 is actually related to setup 3 since since shinyAppFile returns an app-object.
For a single file app just rename it and add
app <- shinyApp(ui = ui, server = server)
runApp(app)
to be able to source it.

local image in shiny app without img(src())?

I would like to include a local image file in my shiny app, following these instructions:
Embedding Image in Shiny App
However, my IT networks security, for some reason, prevents R from reading that image.
I can confirm it is an IT security blockage, because the same exact code and file/directory structure works when I move to another computer.
It is also strange, because I am able to read other files from that folder, because other commands like read.csv() are not blocked. I dont know what subroutines go on inside img(src()) but my network does not like it.
Any alternative ways to embed an image in a shiny app ui?
Maybe with base64 encoding:
b64 <- base64enc::dataURI(file="myfile.png", mime="image/png")
ui <- fluidPage(
img(src=b64)
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Where myfile.png is in the same folder as the app.

Resources