I'm trying to make a Shiny app which will read a csv file, and send me an email based on the content of the file. Here's the Shiny app introduction to a file reader, which I'm trying to adapt to my problem:
## 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)
}
Here's what it looks like, cutting out the parts which are not necessary in my case:
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)
}
I've already tested the email by itself, and it works. It just doesn't work within the app. Am I using reactivity wrong? Should it be observe?