如何发送在Shiny App中输入包含csv文件内容的电子邮件?

时间:2019-07-18 19:16:02

标签: r shiny sendmailr

我正在尝试制作一个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)

}

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

1 个答案:

答案 0 :(得分:0)

已解决:

## 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)

}```