Change the file upload progress bar behavior in Shiny - r

I have a Shiny application that requires the user to upload multiple small text files, I do this with fileInput. When I upload multiple files, the progress bar seems to half reset after each file is uploaded in turn. This looks very messy and I'd prefer it if the bar simply showed how many files had finished uploading rather than the progress of any individual file.
Here is a gif of the behavior:
My question is: Is there a way using base Shiny to change the behavior of the upload progress bar for fileInput? And if not, are there any other packages that could be used?
Here is an example of using fileInput
library(shiny)
ui <- fluidPage(
titlePanel("File Input Progress Bar Demo"),
fileInput(
inputId = "MyFiles",
label = "Upload multiple files",
multiple = T
),
mainPanel()
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)

Related

Download A File Generated Within An R Shiny App

I am currently trying to generate a Shiny App that at the press of a button generates and downloads data for a user. I have a user defined function that pulls data from some places, analyzes, and creates a .csv comprised of numerous tables.
The bottleneck I'm running into is successfully taking this .csv file comprised of multiple tables and allowing it to be easily downloaded. I have tried applying the solution presented here, but when I run the application script, all of the data is generated from the button but it doesn't bring up the download window as I'd hoped. Is there a way of using downloadHandler and link it to something other than a downloadButton or downloadLink function?
Current UI Function:
ui <- dashboardPage(
dashboardHeader(title = "Download Center"),
dashboardSidebar(
sidebarMenu(id="mytabs",
sidebarMenuOutput("menu")
)
),
dashboardBody(tabItems(
tabItem(tabName = "DataHarah", h2("Download Your Data Here"),
textInput(inputId='startyear', label='What is the first year?'
,value=2021, width = NULL, placeholder = "Inputs should be
a four digit year (e.g. 2022)."),
textInput(inputId='endyear', label='What is the last year?'
,value=2022, width = NULL, placeholder = "Inputs should be
a four digit year (e.g. 2022)."),
actionButton("do", "Generate & Download Your Data!")
))
))
Current Server Function & Initialization:
observeEvent(input$do,
{DataTalk(input$startyear,input$endyear) # function that generates a csv file
# containing multiple tables.
output$downloadData <- downloadHandler( #download handler that may not be connected properly.
filename = c('mycsv.csv'),
content = function(file){
file.copy('mycsv.csv',file)})})}
shinyApp(ui, server)

Can you exclude certain files from being uploaded in an R shiny app?

I am working on a shiny app where the user uploads a number of csv files based on which some output is created. The files to upload are produced by an external application and all lie in the same folder. I want the user to be able to select all files in the folder for upload and I am currently using
shiny::fileInput() with the option multiple = TRUE in the shiny App.
The problem I have is that the output folder contains a file that can be quite large and that is not required for the analysis in the app. The app works fine if the user just deselects that file in the upload menu. But I was wondering if there is a way that I can tell the app to ignore a certain file (based on the file name), so that even if the user selects all files in the folder for upload, the large file is not uploaded while all the other selected files still are.
I know that you can set a maximum file size in the options (shiny.maxRequestSize) but this does not seem to solve my problem, as the upload then ends with an error if the large file is selected.
Here is a minimal example of the app:
library(shiny)
options(shiny.maxRequestSize = 30*1024^2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
# Upload menu is created here:
fileInput(
inputId = "upload",
label = "Upload Files",
buttonLabel = "Select files...",
multiple = TRUE
)
),
mainPanel(
tableOutput(outputId = "files")
)
)
)
server <- function(input, output, session) {
output$files <- renderTable(
input$upload[, c("name", "size")]
)
}
shinyApp(ui, server)

Is it possible to display an info text when hovering over a R shiny selectInput?

I am currently building a shiny app to build biological networks. To build them, you can choose between many different parameters, which i included in different selectInputs or numericInputs.
Is it possible to have some kind of info text, when hovering the mouse over those input fields? I dont want to add 3-4 sentences of text to the title of each select/numericInput.
Thanks :)
If you don't mind an extra package dependancy then you can use bsTooltip from the shinyBS package. Note that the hover tooltip sometimes doesn't show up unless you click on the input in the RStudio viewer pane, but if you run your app in your browser, the hover trigger should work:
library(shiny)
library(shinyBS)
ui <- fluidPage(
selectInput("input1", "Select input", c("choice1", "choice2")),
bsTooltip(id = "input1",
title = "Here is some text with your instructions")
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)

How open PDF file in the same window as R Shiny application works?

How to load PDF file in same window of R Shiny application?
Here is an example:
library(shiny)
ui <- fluidPage(
navbarPage("Demo",
tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
tabPanel("PDF file", fluidRow(uiOutput("load_pdf_file")))))
server <- function(input, output) {
# 1. Load PDF file
output$load_pdf_file <- renderUI({
# 1.1. This loads pdf file in a 'new' window
browseURL("http://www.africau.edu/images/default/sample.pdf")
# 1.2. How to load pdf file in the 'same' window where R Shiny app works
# Expect to see pdf file on the page in 'PDF file' section
# ...
})
}
shinyApp(ui = ui, server = server)
The method that I know of, does not rely on serving up PDF's it uses the standard HTML methods one would use in a traditional webpage, with the shiny code js wrappers to place it in an iFrame.
With this method, in the tabPanel you set up an iFrame and give it some dimension ( I picked arbitrary ones) the decide if it scrolls or not, the use src to supply it with the path to your pdf file. The condition of course is that your PDF file is discoverable by some kind of a local path or URL to the outside world.
library(shiny)
ui <- fluidPage(
navbarPage("Demo",
tabPanel("Overview", fluidPage(fluidRow("Overview page"))),
tabPanel("PDF file",
tags$iframe(style="height:800px;
width:200%;
scrolling=no",
src="https://your-path/your-file.pdf"))))

How to extract images from uploaded word document in Shiny

I am working on a Shiny app that reads Word documents uploaded by users. The uploaded document then displays a table of all elements in the document and their formatting. I want it to also show any pictures from the uploaded Word doc. Documents containing multiple images aren't an issue - users will only ever upload documents with one image.
To do this, I am using the officer package. It has a function called media_extract where you can do exactly what I want. The issue is, while the documentation says this function can be used to extract images from .doc or .ppt files, I can only get it to work for the latter. This is because media_extract takes the image file path as an argument, but I cannot generate a file path for Word docs. The file path is generated by using one of two officer functions depending on the file type: docx_summary or pptx_summary. These are also the functions I use to generate the tables rendered in my app. The pptx_summary creates a table with a media_path column, which displays a file path for image elements, while docx_summary generates no such column. Absent that column and the path it includes, I don't know how to extract images from Word docs using this function.
For your convenience, here is my code for two Shiny apps: one that reads powerpoints and one for word docs. If you upload a powerpoint file and word file that include an image you will see how the tables generated in each app are different. My powerpoint app also renders an image, to show you how that is done. Obviously that functionality is not in my word app...
Powerpoint reader app:
library(officer)
library(DT)
library(shiny)
ui<- fluidPage(
titlePanel("Document Scanner"),
sidebarLayout(
sidebarPanel(
fileInput("uploadedfile", "Upload a file", multiple=FALSE,
accept=c(".ppt", ".pptx", ".docx"))
),
mainPanel(
tags$h3(tags$b("Document Summary")),
br(),
DT::dataTableOutput("display_table"),
br(),
imageOutput("myImage")
)
)
)
server<-function(input,output) {
#creating reactive value for uploaded file
x<-reactive({
uploadedfileDF<- input$uploadedfile
uploadedfileDataPath<- uploadedfileDF$datapath
read_pptx(uploadedfileDataPath)
})
#rendering formatting table
output$display_table<-DT::renderDataTable({
req(input$uploadedfile)
DT::datatable(pptx_summary(x()))
})
#rendering images from powerpoint
output$myImage<-renderImage({
readFile<-x()
fileSummaryDF<-pptx_summary(readFile)
#Getting path to image (this is basically straight from the documentation
#for media_extract)
fileSummaryDF_filtered<- fileSummaryDF[fileSummaryDF$content_type %in% "image", ]
media_file <- fileSummaryDF_filtered$media_file
png_file <- tempfile(fileext = ".png")
media_extract(readFile, path = media_file, target = png_file)
list(src = png_file,
alt="Test Picture")
})
}
shinyApp(ui, server)
Word reader app:
library(officer)
library(DT)
library(shiny)
ui<- fluidPage(
titlePanel("Word Doc Scanner"),
sidebarLayout(
sidebarPanel(
fileInput("uploadedfile", "Upload a file", multiple=FALSE,
accept=c(".doc", ".docx"))
),
mainPanel(
tags$h3(tags$b("Document Summary")),
br(),
DT::dataTableOutput("display_table"),
imageOutput("image1")
)
)
)
server<-function(input,output) {
# creating reactive content from uploaded file
x<-reactive({
print(input$uploadedfile)
uploadedfileDF<- input$uploadedfile
uploadedfileDataPath<- uploadedfileDF$datapath
docDF<-read_docx(path=uploadedfileDataPath)
summaryDF<-docx_summary(docDF)
})
#rendering formatting table
output$display_table<-DT::renderDataTable({
req(input$uploadedfile)
DT::datatable(x())
})
#how to render image without a image path anywhere in table?
}
shinyApp(ui, server)
If this can't be done in officer then I'm happy to do it a different way. Thank you.

Resources