【发布时间】:2022-01-15 15:21:51
【问题描述】:
我想出了一个我已经确认可以正常工作的 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 文件中读取文件会更好吗?
【问题讨论】:
标签: r shiny reticulate