0

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

}

我已经单独测试了电子邮件,它可以工作。它只是在应用程序中不起作用。我使用反应性错误吗?应该观察吗?

4

1 回答 1

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)

}```
于 2019-07-18T22:16:28.210 回答