dynamicFile: Dynamically generate Shiny file selection component based on...

View source: R/spsServerCollections.R

dynamicFileR Documentation

Dynamically generate Shiny file selection component based on option

Description

Depending on the "mode" in SPS options, this function renders a similar UI components but behaves differently on server.

  1. local mode will not copy file, directly use a path pointer.

  2. server mode upload file and store in temp. Expect similar behavior as shiny::fileInput.

Usage

dynamicFile(
  id,
  title = "Select your file:",
  label = "Browse",
  icon = NULL,
  style = "",
  multiple = FALSE,
  buttonType = "primary",
  placeholder = "No file selected",
  mode = spsOption("mode")
)

dynamicFileServer(
  input,
  session,
  id,
  mode = spsOption("mode"),
  roots = c(root = "default")
)

Arguments

id

element ID, Use ns() to wrap the id if you are using within a shiny module, but DO NOT use ns() to wrap the id on server side

title

element title

label

upload button label

icon

button icon, an object create by shiny::icon

style

additional button style, only works for local mode

multiple

bool, are multiple files allowed?

buttonType

string, Bootstrap button markup (color). Default in SPS is 'primary', other valid values include 'info', 'success', 'default', 'warning', 'danger'.

placeholder

string, text to display before the file is uploaded

mode

string, one of "local" or "server"

input

shiny server input

session

shiny server session

roots

a named character vector, paths where users can reach on the server, so only required for "server" mode, default is current directory + all system volumes. You can lock users to a specific path, so they are not allowed to browse parent folders. like only current directory: c(current=getwd()); a temp folder: c(current=tempdir()); unlimited: c(shinyFiles::getVolumes()())

Details

To setup the option:

The local mode uses functions from shinyFiles so it will reach file system on the server end. Although the latest shinyFiles limits users to only specified server end location (folder), there is still some risk. That's why it is named "local", you are encouraged to run the app on your local computer. The advantage of "local" is: for some very large files, it does not upload and store in the temp. Rather, it directly parses the path on the local file system and return the path immediately. It means the file has to exist on the file system that serves the Shiny app. If you deploy the app on places like shinyapps.io, users can only choose files from server.

On the other hand, server mode uses original but enhanced shiny default upload component. Users can upload files from local to server. So users do not have access to server end file system if you deploy it online. However, the limitations are:

  1. not ideal for large files, default limit is 30MB, and there is no break-point upload.

  2. If you are running the app on your own computer, local end and server end is the same, which is your computer. Using server mode will make a copy of your existing file to temp location and this is a waste of time and storage.

To set up options:

  1. Under SPS framework, edit options in global.R.

  2. Outside SPS framework with your own Shiny app, use spsUtil::spsOption() function, like spsUtil::spsOption("mode", "server") or spsUtil::spsOption("mode", "local") to set up mode.

If you are not sure what mode you are on, use spsUtil::spsOption('mode') to check.

Value

a Shiny upload component on UI

For the server end it returns a reactive object which is a dataframe, need to extract the value inside reactive expression, observe, or inside isolate. See examples

Examples

# Simple example
if(interactive()){
    spsOption("mode", value = "server") # Change the value to 'local' to see difference
    ui <- fluidPage(
        dynamicFile(id = "server_file", label = "server"),
        verbatimTextOutput("server_out")
    )

    server <- function(input,output,session){
        file_server <- dynamicFileServer(input,session, id = "server_file")
        output$server_out <- renderPrint({
            file_server()  # remember to use `()` for reactive value
        })
    }
    shinyApp(ui = ui, server = server)
}
# To demostrate different modes in the same app, we can set options before the function.
# This is NOT recommended, you should stick with only one mode for the entire app.
if(interactive()){
    spsOption("mode", "local")
    local_ui <- dynamicFile("local_file", "local")
    spsOption("mode", "server")
    server_ui <- dynamicFile("server_file", "server")
    ui <- fluidPage(
        column(
            6,
            local_ui,
            verbatimTextOutput("local_out")
        ),
        column(
            6,
            server_ui,
            verbatimTextOutput("server_out")
        )
    )

    server <- function(input,output,session){
        spsOption("mode", "local")
        file_local <- dynamicFileServer(input,session, id = "local_file")
        output$local_out <- renderPrint({
            file_local() # remember to use `()` for reactive value
        })
        spsOption("mode", "server")
        file_server <- dynamicFileServer(input,session, id = "server_file")
        output$server_out <- renderPrint({
            file_server()
        })
    }
    shinyApp(ui = ui, server = server)
}

systemPipeR/systemPipeS documentation built on Oct. 21, 2023, 12:18 p.m.