R闪亮,上传word doc/PDF并在上传的文档上运行函数,输出问题
这是我第一次使用和探索R闪亮。这个过程看起来非常简单,但我在让一切顺利进行方面遇到了一些困难。
我正在尝试构建一个应用程序,允许用户上传 Word 文档、PDF 或其他文本文件(如果还有一个直接粘贴文本而不是上传文件的选项,那就太酷了),然后一个函数将在文件或粘贴的文本上运行,这将导致返回文本列表。
我的代码在不使用 Rshiny 时工作正常:
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*")))))
运行此代码的结果如下:
[1] "Brown 2020" "Brown and Green 2004" "Cooper 2015" "John Granger et al. 2015"
[5] "Jones, 2001" "Smith 2010"
我想在 Rshiny 应用程序上运行具有相同功能的相同进程,用户可以在其中上传包含此类文本的文件或粘贴直接输入文本,它会返回相同的结果。
这是我现在的应用程序:
# 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)
我可以让应用程序运行(打开),但是当我上传文件时,没有任何反应,或者出现以下错误:“文件必须是一个字符(指定文件)位置)。”
正如我所说,我对 R闪亮很陌生,所以任何见解都会有所帮助,谢谢!
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+quot;), 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+quot;), 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fileInput
不直接返回文件名/路径。它返回一个带有
name
、size
、type
和datapath
的数据帧。您可能想要的是
datapath
,所以试试这个。fileInput
doesn't return the filename/path directly.It returns a dataframe with the
name
,size
,type
anddatapath
.What you probably want is
datapath
, so try this.