r - 如何使用 Reticulate 在 R shiny App 中导入 Python 函数以供下载 App
问题描述
data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))
我想在我的function.py文件中使用python函数,并且有一个名为run_xi的函数,它只是从数据库中提取数据并将数据框返回给R。但是,如果我替换,这段代码可以成功运行使用 reticulate 的代码(如下代码所示),它将无法生成 csv 文件,只返回一个 HTML 文件。有什么解决办法吗?
library(shiny)
library(reticulate)
library(writexl)
#reticulate::source_python("function.py")
#data_xi <- run_xi(26)
if (interactive()) {
ui <-fluidPage(
downloadButton("downloadData", "Download Metrics Reports")
)
#data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21)) # uncomment here, code will working
reticulate::source_python("function.py") # not working when try to use python function by reticulate
data_xi <- run_xi(26)# not working when try to use python function by reticulate
server <- function(input,output){
output$downloadData <- downloadHandler(
filename = function(){
paste(Sys.time(), 'site_mtx.xlsx')
},
content = function(file){
write_xlsx(data_xi, file)
}
)
}
shinyApp(ui, server)}
解决方案
我已经解决了这个问题,我使用 reticulate 函数创建了另一个 R 函数来导入 python 函数,然后在服务器中使用source
直接导入 r 函数,然后它可以下载为 Excel 而不是 html
推荐阅读
- node.js - 序列化模型 node.js 中的关联
- git - 通过 Intellij Idea UI 的 Git 命令显示以下错误:.git/hooks/prepare-commit-msg: line 32: node: command not found
- pandas - 从具有大量标签的 Pandas 数据框中创建 TensorFlow 数据集?
- javascript - 创建一个列,每当来自其他 div 的 img 发生更改时,该列就会滚动
- c# - 尝试访问 DataContext 时出现 System.ObjectDisposedException
- spring-boot - 在 @SpringBootTest 中设置类时,骆驼 @MockEndpoints 停止工作
- kubernetes - Kubernetes - 显示命令帮助,但仅显示示例
- azure - 在 Azure 和 MailNickname 中创建安全组
- node.js - 如何更新 mongo 上的许多文档并返回更新后的文档?
- c# - 为什么我听不到来自其他订户 SIP 的声音?