How to extract images from uploaded word document in Shiny - r

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.

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)

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

display.mode = 'showcase' in shinyApp() call - no code shown

I would like to be able to use display.mode = 'showcase' in an app run with the shinyApp() function call. According to the docs I should be able to pass any arguments that go runApp() through the options argument. The showcase mode works (the window is split) but it does not show the code. What's interesting is that if I run runExample("01_hello") everything works fine. I'm using shiny 1.0.5.
Code:
library(shiny)
ui <- fluidPage(
titlePanel("Sample App"),
sidebarLayout(
sidebarPanel(
selectInput("data", label = "Data set",
choices = c("mtcars", "iris"))
),
mainPanel(
tableOutput("table")
)
)
)
server <- function(input, output) {
data <- reactive({
get(input$data, 'package:datasets')
})
output$table <- renderTable({
head(data())
})
}
shinyApp(ui, server, options = list(display.mode = 'showcase'))
Output:
I was having the same issue. I had the app.R file and created the DESCRIPTION file using notepad but couldn't deploy a shinyApp with the code. Then I copied the DESCRIPTION file from shiny\examples\01_hello and noticed this:
Turns out my file had a TXT extension, so Shiny wasn't reading it as a metadata file. Once I used the correct DESCRIPTION file (which you can edit using notepad), everything worked out fine.
This is more of an addendum to Gus_est's answer, since I had the same problem and wasn't able to get it run right from there.
Create a file inside the directory your app.R-file resides in, e.g. a txt-file. Write into the file what display mode is to be used using Debian Control file format. In our case it would look like that (Title is not necessary):
Title: My App
DisplayMode: Showcase
Then rename the file DESCRIPTION without providing a file ending. Ignore the warning.
When you run the app now, it will always be in display mode "showcase", you can override this only inside the runApp()-statement. So I find the documentation to be misleading.
Check your current working directory. This problem seems to occur, if the working directory is not set to the folder with the app code.

Change the file upload progress bar behavior in Shiny

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)

Resources