Home > OS >  R shiny, upload word doc/PDF and run function on uploaded document, Output issues
R shiny, upload word doc/PDF and run function on uploaded document, Output issues

Time:03-10

this is my first time using and exploring R shiny. The process seems pretty straightforward but I am having some difficulties getting everything to run smoothly.

I am trying to build an app that will allow a user to upload a word doc, PDF, or other text file (would be cool if there was also an option to paste text directly instead of uploading a file), and then a function would run on the file or pasted text which would result in a returned list of text.

My code works fine when not using R shiny:

    library(dplyr)
    library(stringr)
    library(readtext)
    library(XML)
    library(here)

### option to manually paste in text to use instead of uploading file:    
        text <- "This is a test. The purpose of the below function is to extract and return and in-text cictations with the following formats:(Smith, 2010), and (Smith 2010; Jones, 2001; Brown 2020), or Cooper (2015), or John Granger et al. (2015), and (Brown and Green 2004)."
    
    ######Option to read in word doc######
    wordtest<-readtext(here("Example.docx"))
    text<-wordtest$text
    
    ######Option to read in PDF file######
    PDFtest<-readtext("Example2.pdf")
    text<-PDFtest$text
    
    ##Return citations alphabetically:
    rx <- "(?:\\b(\\p{Lu}\\w*(?:\\s \\p{Lu}\\w*)*(?:\\s et\\s al\\.)?)?)\\s*\\(([^()]*\\d{4})\\)"
    res <- str_match_all(text, rx)
    result <- lapply(res, function(z) {ifelse(!is.na(z[,2]) & str_detect(z[,3],"^\\d $"), paste(trimws(z[,2]),  trimws(z[,3])), z[,3])})    
    sort(unique(unlist(sapply(result, function(z) strsplit(paste(z, collapse=";"), "\\s*;\\s*")))))

The result from running this code is the following:

[1] "Brown 2020"               "Brown and Green 2004"     "Cooper 2015"              "John Granger et al. 2015"
[5] "Jones, 2001"              "Smith 2010"

I want to run this same process with these same features, on an R shiny app where the user could upload a file containing such text or paste the text directly and it would return the same result.

here is what I have for my app right now:

# Load R packages
library(shiny)
library(shinythemes)
library(dplyr)
library(stringr)
library(readtext)
library(XML)
library(data.table)

# Define UI
ui <- fluidPage(theme = shinytheme("cerulean"),
                navbarPage(
                  theme = "cerulean",  # <--- To use a theme, uncomment this
                  "Extracting in-text citations app", #app title
                  tabPanel("Alphabetical Order",      # tab title
                           sidebarPanel(
                             # Input: Select a file ----
                             fileInput(inputId ="text", "Choose File",
                                       multiple = FALSE,
                                       accept = c("text/plain",".doc",".docx",".pdf")),
                             p("Accepted Files: .doc, .docx, .pdf, text/plain"),
                            ), # sidebarPanel
                                  mainPanel(
                                        h1("Output"),
                             
                                        h4("List of citations in Alphabetical Order"),
                                        verbatimTextOutput("txtout"),
                             
                                  ) # mainPanel
                         
                  ), # Navbar 1, tabPanel
                  tabPanel("Chronological Order", "This panel is intentionally left blank"),
                  
                  
                ) # navbarPage
) # fluidPage

#Define Server:
server<- function (input,output){
  
  output$txtout<-renderPrint({

    wordtest<-readtext(input$text)
    text2<-wordtest$text
  rx <- "(?:\\b(\\p{Lu}\\w*(?:\\s \\p{Lu}\\w*)*(?:\\s et\\s al\\.)?)?)\\s*\\(([^()]*\\d{4})\\)"
  res <- str_match_all(text2, rx)
  result <- lapply(res, function(z) {ifelse(!is.na(z[,2]) & str_detect(z[,3],"^\\d $"), paste(trimws(z[,2]),  trimws(z[,3])), z[,3])})    
  return(sort(unique(unlist(sapply(result, function(z) strsplit(paste(z, collapse=";"), "\\s*;\\s*"))))))
  
  })
}


# Create Shiny object
shinyApp(ui = ui, server = server)

I can get the app to run (open), but when I go to upload a file, nothing happens, or I get the following error: "file must be a character (specifying file location(s))."

As I said I am very new to R shiny so any insight would be helpful, thanks!

CodePudding user response:

fileInput doesn't return the filename/path directly.

It returns a dataframe with the name , size, type and datapath.

What you probably want is datapath, so try this.

file <- input$text

wordtest<-readtext(file$datapath)
  • Related