我是 Shiny 的新手,我正在尝试将作为 .R 脚本工作的现有代码转换为 Shiny 应用程序。
原码 - 链接
样本数据- 链接
重点是有一个文件输入,一个人选择一个pdf文件。然后处理 pdf。
这是我的代码:
server <- function(input, output) {
output$contents <- renderTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
req(input$file1)
test <- pdf_text(input$file1$datapath)
### Two Sided
test <- gsub("[\r\n]", " ", test)
list <- strsplit(test, " {2,}") #split anywhere where there are 2 or more consecutive spaces - hopefully only between two paragraphs (if not the output wont make much sense)
resi <- lapply(list, function(x) {
unl <- unlist(x)
len <- length(x)
uneven <- seq(from = 1, to = len , by = 2)
even <- seq(from = 2, to = len , by = 2)
uneven <- unl[uneven]
even <- unl[even]
uneven <- paste(uneven, collapse = " ")
even <- paste(even, collapse = " ") #intentionally leave a space between them, one could even use something that is not expected to occur in the document like "frafrafra" and use that in the gsub call later as gsub("(\\d)-frafrafra(\\d)", "\\1\\2", resi)
return(cbind(uneven, even))
}) #separate even from uneven rows
resi <- unlist(resi)
resi <- gsub("(\\d)- (\\d)", "\\1\\2", resi) #clean numbers
resi <- gsub("(\\b)- (\\b)", "\\1\\2", resi) #clean words
resi <- data_frame(line = 1:length(resi), text = resi) #change class/type - vector to dataframe
count <- resi %>%
unnest_tokens(word, text) %>% #split columns into word like elements (tokens)
count(word, sort = TRUE) #count frequency and sort in desc order
count$word <- gsub("[^0-9]", NA, count$word)
count$num_char <- nchar(count$word)
two_cols <- count %>%
filter(!is.na(word)) %>%
filter(n == 1) %>%
filter(num_char == 7 | num_char == 13 | num_char == 15)
if(input$disp == "head") {
return(head(test))
}
else {
return(two_cols)
}
})
}
代码似乎在测试之前有效<- pdf_text(input$file...
但是,我收到一个错误:错误的“按”参数登录
知道哪里出了问题吗?
编辑:
我正在测试原始代码的第二部分,这似乎是作用域规则的问题。也就是说,在服务器函数中定义一个函数。