gpt4 book ai didi

r - 将针织临时文件附加到 R Shiny 的电子邮件

转载 作者:行者123 更新时间:2023-12-04 11:06:56 25 4
gpt4 key购买 nike

我有一个 Shiny 的应用程序,它使用 Mailgun单击按钮时发送电子邮件,并在单击另一个按钮时生成 rmarkdown 报告。

这是工作代码,显然没有工作电子邮件身份验证:

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report"),
actionButton("mail", "send email"),
textOutput('mailo')
)
)

server.R
library(shiny)

sendEmail <- function(email = "xxx@you.org",
mail_message = "Hello"){

url <- "https://api.mailgun.net/v3/sandboxxxxxxxxx.mailgun.org/messages"
## username:password so api_key is all after the api:
api_key <- "key-0xxxxxxxxxxxx"
the_body <-
list(
from="Mailgun Sandbox <postmaster@sandboxxxxxxxxxxxxxxxx.mailgun.org>",
to=email,
subject="Mailgun from R test",
text=mail_message
)

req <- httr::POST(url,
httr::authenticate("api", api_key),
encode = "form",
body = the_body)

httr::stop_for_status(req)

TRUE

}


# Define server logic required to draw a histogram
shinyServer(function(input, output) {

event <- observeEvent(input$mail,{
sendEmail()
}, ignoreInit = TRUE)
output$mailo <- renderText({print("EMAIL SENT!")})

output$report <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)

# Set up parameters to pass to Rmd document
params <- list(n = input$slider)

# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).

rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)

})

我想一步完成两件事。也就是说,生成报告,将其附加到电子邮件并将其发送到给定的地址。我只是不知道如何对待 tempfile()引用文件时。

我目前也在 Shinyapps.io 上部署了该应用程序,因此保存到文件然后检索将不起作用。

有任何想法吗?

最佳答案

这是您需要的代码。我对此进行了测试,它奏效了,尽管我的 gmail 确实给了我巨大的亮黄色警告,表明该电子邮件包含一个可能存在危险的未经验证的文件。我还稍微简化了应用程序并删除了一些不必要的代码。

library(shiny)

ui <- fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
actionButton("mail", "send email")
)

sendEmail <- function(email = "RECIPIENT@gmail.com",
mail_message = "Hello",
file = NULL) {

url <- "https://api.mailgun.net/v3/sandboxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org/messages"
## username:password so api_key is all after the api:
api_key <- "XXXXXXXXXXXXXXXXXX-XXXXXXXXX-XXXXX"
the_body <-
list(
from = "Mailgun Sandbox <postmaster@sandboxXXXXXXXXXXXXXXXXXXX.mailgun.org>",
to = email,
subject = "Mailgun from R test",
text = mail_message
)
if (!is.null(file)) {
the_body$attachment = httr::upload_file(file)
}

req <- httr::POST(url,
httr::authenticate("api", api_key),
encode = "multipart",
body = the_body)

httr::stop_for_status(req)

TRUE

}

server <- function(input, output, session) {
observeEvent(input$mail, {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)

# Set up parameters to pass to Rmd document
params <- list(n = input$slider)

# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).

file <- rmarkdown::render(tempReport,
output_file = file.path(tempdir(), "report.html"),
params = params,
envir = new.env(parent = globalenv())
)

sendEmail(file = file)
})
}

shinyApp(ui, server)

顺便说一句,还有一个 IMmailgun如果您感兴趣,请打包,但它基本上实现了您使用此代码所做的工作。

关于r - 将针织临时文件附加到 R Shiny 的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49658597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com