【问题标题】:How to send an email with content of a csv file input in a Shiny App?如何在 Shiny App 中发送包含 csv 文件内容的电子邮件?
【发布时间】:2019-11-27 18:58:14
【问题描述】:

我正在尝试制作一个 Shiny 应用程序,该应用程序将读取 csv 文件,并根据文件的内容向我发送一封电子邮件。这是文件阅读器的 Shiny 应用介绍,我正在尝试适应我的问题:

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
        accept = c(
          "text/csv",
          "text/comma-separated-values,text/plain",
          ".csv")
        ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header = input$header)
  })
}

shinyApp(ui, server)
}

这就是它的样子,去掉我不需要的部分:

if (interactive()) {

    ui <- fluidPage(
        sidebarLayout(
            sidebarPanel(
                fileInput("file1", "Choose CSV File",
                          accept = c(
                              "text/csv",
                              "text/comma-separated-values,text/plain",
                              ".csv")
                )
            ),
            mainPanel( )
        )
    )

    server <- function(input, output) {


        reactive({

        mail <- read_delim(input$file1$datapath, 
                           ";", 
                           escape_double = FALSE, 
                           col_names = FALSE, 
                           trim_ws = TRUE)

        if (is.null(mail))
            return(NULL)

            send.mail(from = "XXXXXXX",
                      to = "XXXXXX",
                      subject = mail[1,1],
                      body = mail[1,2],
                      html = T,
                      smtp = list(host.name = "smtp.gmail.com",
                                  port = 465,
                                  user.name = "XXXXX",
                                  passwd = "XXXXXX",
                                  ssl = T),
                      authenticate=T)

        })

    }

    shinyApp(ui, server)

}

我已经对电子邮件本身进行了测试,并且可以正常工作。它只是在应用程序中不起作用。我使用反应性错误吗?应该观察吗?

【问题讨论】:

    标签: r shiny sendmailr


    【解决方案1】:

    固定为:

    ## Only run examples in interactive R sessions
    if (interactive()) {
    
        ui <- fluidPage(
            sidebarLayout(
                sidebarPanel(
                    fileInput("file1", "Choose CSV File",
                              accept = c(
                                  "text/csv",
                                  "text/comma-separated-values,text/plain",
                                  ".csv")
                    )
                ),
                mainPanel( )
            )
        )
    
        server <- function(input, output) {
    
            mail <- reactive({
    
                read_delim(input$file1$datapath, 
                           ";", 
                           escape_double = FALSE, 
                           col_names = FALSE, 
                           trim_ws = TRUE)
    
            })
    
            observe({
    
            if(!is.null(input$file1)){
    
                send.mail(from = "XXXXX",
                          to = "XXXX",
                          subject = as.character(mail()[1,1]),
                          body = as.character(mail()[1,2]),
                          html = T,
                          smtp = list(host.name = "smtp.gmail.com",
                                      port = 465,
                                      user.name = "XXXXXX",
                                      passwd = pass,
                                      ssl = T),
                          authenticate=T)
    
            }
    
            })
        }
    
        shinyApp(ui, server)
    
    }```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2018-12-07
      • 2013-05-30
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多