r-在Shiny中迭代通过源python函数的文件路径列表



我提出了一个python函数,我已经确认它运行良好。我正在尝试使用Shiny的reticulate将其放入Shiny应用程序中。我对Shiny不是很熟悉,但无论如何都需要使用它。

为了了解我正在做的事情,我编写了一些python代码,它接受多个文件,并根据一个常见的字符串列表匹配字符串。当我在机器上运行python文件时,这段代码运行良好。

我需要让其他人使用一个闪亮的应用程序,在那里他们可以上传文件,然后让应用程序运行底层的python代码。

到目前为止,我已经设置了这个闪亮的应用程序,它可以接收多个文件。我很难思考如何使用reactive列出文件路径名,然后发送到我的python代码(其中包括打开和读取文件的步骤(,这样它就可以完成自己的任务。

这是迄今为止我的应用程序的代码:

library(shiny)
library(shinyFiles)
# define UI
ui <- fluidPage(
titlePanel('Counter of Gendered Language'),
fileInput("upload", "Choose a folder",
multiple = TRUE,
accept = c('text')),
tableOutput('text'),
downloadButton('output', 'Download Count File .csv'))
# define server behavior
server <- function(input, output){
# Setup
#* Load libraries
library(reticulate)
#* Use virtual environment for python dependencies
use_virtualenv('file/path/py_venv', required = TRUE)
#* Source code
source_python('code/counting_gendered_words.py')
#* Load list of words to match raw text against
dictionary <- read.csv('data/word_rating.csv')
text <- reactive(
list <- list.files(path = input$upload[['name']])
)
output$counted <- gendered_word_counter(dictionary, text())
output$downloadData <- downloadHandler(
filename = function(){
paste0(input$upload, ".csv")
},
content = function(file){
vroom::vroom_write(text$counted, file)
}
)
}
# Run the application 
shinyApp(ui = ui, server = server)

当我运行这个应用程序时,它告诉我的是:

错误:没有活动的反应上下文,不允许操作。

  • 你试图做一些只能在被动消费者内部完成的事情

所以我想做的基本上只是将某人上传到应用程序的每个文件名传递给我的gendered_word_counter()python函数。

我该怎么做?

我非常自信,我只是一个新手,这可能是一个非常简单的解决方案。任何来自那些对Shiny更满意的人的帮助都将不胜感激!

编辑:我注意到我的代码只是调用文件的名称,如果没有上传文件的内容,这对我来说毫无意义!如果我读闪亮应用程序中的文件而不是我的.py文件,会更好吗?

如果没有python代码,我无法复制应用程序,但我可以看到这一行:

output$counted <- gendered_word_counter(dictionary, text())

具有在没有反应上下文的情况下被调用的反应对象(text()(。它应该被包裹在observeobserveEvent中。

observe({
output$counted <- gendered_word_counter(dictionary, text())
})

此外,让我们在此处添加括号:

content = function(file){
vroom::vroom_write(text()$counted, file)
}