R send email with attachment using sendmailR - r

I'm trying to attach a file in R using sendmailR.
Is this possible?
excluding attach.files line sends email without issue.
to <- "<me#localhost>"
subject <- "test attachment"
body <- list("test")
attach.files = c("test.jpeg"),
sendmail(from, to, subject, body,
control=list(smtpServer="192.168.0.51"))}
Error: unexpected '}' in:
"sendmail(from, to, subject, body,
control=list(smtpServer="192.168.0.51"))}"

Try the following code:
to <- "<me#localhost>"
subject <- "test attachment"
attachment -> mime_part (x = "test.jpeg")
body <- list("test", attachment)
sendmail(from, to, subject, body,
control=list(smtpServer="192.168.0.51"))}

Related

No data received from httr POST R with body

I'm trying to extract the province and city options respectively from this website: https://www.handmadepizza.co.kr/store/store.
For the province, there was no body needed for the POST code, hence this code below works.
raw.data <- POST("https://www.handmadepizza.co.kr/store/StoreSO/selectStoreSiList.do",
body = NULL,
encode = "form")
raw.data <- content(raw.data)
However, the POST code for the city requires a body. Tried to add the body as per the code chunk below, but no data was received.
argus <- '{"_window_name":{
"ADDR_SI" : "%EC%9A%B8%EC%82%B0"
}
}'
raw.data <- POST("https://www.handmadepizza.co.kr/store/StoreSO/selectStoreGuList.do",
body = argus,
encode = "form")
raw.data <- content(raw.data)
I know it's a matter of finding the right way to add the body to the POST code chunk but just can't wrap my head around it!

R programming _ get function for http with query parameters-

I'm using R programming and httr package to request a HTTP
I want to apply get function for http using query parameters.
GET(url = NULL, config = list(), ..., handle = NULL)
the request contains , seperated by question mark ?
1- url https://example.com
2- url parameter:'title='
# Function to Get Links to specific page
page_link <- function(){
url <- "https://example.com?"
q1 <- list (title = "")
page_link<- GET (url, query = q1)
return (page_link)
}
If you're asking how to bind the url to get one element to request with GET, you should try paste0(), e.g.:
url <- paste0("https://example.com?",q1[x])<br>
page_link <- GET(url)

add optional query parameters into R POST request

I want to make a POST request to https://rest.ensembl.org. Currently the following works:
server <- "https://rest.ensembl.org"
ext <- "/vep/human/hgvs"
r <- POST(paste(server, ext, sep = ""),
content_type("application/json"),
accept("application/json"),
body = '{ "hgvs_notations" : ["chr2:g.10216G>T"] }')
which results in this URL https://rest.ensembl.org/vep/human/hgvs/chr2:g.10216G>T. I would like to use the ? parameter to modify my URL to https://rest.ensembl.org/vep/human/hgvs/chr2:g.10216G>T?CADD=1 however I can't see how to do this in the POST request function in R.
Any help would be great!
If it is always the same parameter you need to send, why not just include it in the URI then?
You could do something like POST( paste0(server, ext, '?CADD=1'), [...] ).
Or would that not be dynamic enough for your usecase?
The following would be the less hacky way to include parameters:
library(httr)
library(jsonlite)
r <- POST(
paste0(server, ext),
query = list('CADD' = 1),
content_type_json(),
accept_json(),
body = toJSON(list('hgvs_notations' = c('chr2:g.10216G>T')))
)

RDCOMClient (Outlook) - ggplot

I'm using the RDCOMClient library to create an Outlook email. I want to send a ggplot as an image inside the email body (inline), not as an attachment.
The only way I see this as possible is adding the plot as an image inside the HTMLBody property. I tried 2 different methods to add the image in html.
1 - Using the RMarkdown library, I created a html page with the plot. This did not work because the image is encoded as a base64 string, that Outlook does not support.
2 - Saving the ggplot to a file and manually creating a simple html such as: <html><body><img src="**path**/my_plot.png" /></body></html>. This also shows an error instead of the image.
Is there a way to add an image inline?
EDIT:
The second method works on local email, but the receiver's message has an error instead of the actual image.
You could attach the image and reference it in the email body using a content id ("cid"):
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggsave(tf<-tempfile(fileext = ".png"), p, dpi = 100, width = 5, height = 5)
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
attach <- outMail[["Attachments"]]$Add(tf)
invisible(attach$PropertyAccessor()$SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x370E001E",
"image/png"
))
invisible(attach$PropertyAccessor()$SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E",
cid <- "myggplotimg"
))
outMail[["To"]] = "johndoe#example.com"
outMail[["Subject"]] = "ggplot image"
outMail[["HTMLbody"]] <- sprintf('<p>Here is your image:<br><img src="cid:%s"></p>', cid)
invisible(outMail$Save())
rm(outMail, attach, OutApp)

How to persistently store value of an object instead of its name?

This is in continuation of my related question: Error when trying to interactively load data file saved by paused batch script. I decided to present my question with a reproducible example separately to avoid making the already large description in the previous question even bigger. In the following reproducible example, I expect to retrieve the value of the stored object ("Important data"), but instead, as you see, I retrieve the name of the object itself ("sf.data.devLinks"). I suspected that it could be because of me using as.name, but I tested additionally a primitive example in an interactive session and as.name worked fine. I also, as you see, have tried using eval and substitute, but it didn't help.
library(RCurl)
info <- "Important data"
ATTR <- "SQL"
request <- "SELECT info FROM topSecret"
dataName <- "sf.data.devLinks"
rdataFile <- "/tmp/testAttr.rds"
save <- TRUE
getData <- function() {
return (info)
}
requestDigest <- base64(request)
# check if the archive file has already been processed
message("\nProcessing request \"", request, "\" ...\n")
# read back the object with the attribute
if (file.exists(rdataFile)) {
# now check if request's SQL query hasn't been modified
data <- readRDS(rdataFile)
message("Retrieved object '", data, "', containing:\n")
message(str(data))
requestAttrib <- attr(data, ATTR, exact = TRUE)
if (is.null(requestAttrib)) {
message("Object '", data, "' doesn't have attribute \"",
ATTR, "\"\n")
}
else {
message("Object '", data, "' contains attribute ", ATTR, ":\n\"",
base64(requestAttrib), "\"\n")
if (identical(requestDigest, requestAttrib)) {
message("Processing skipped: RDS file is up-to-date.\n")
save <- FALSE
return
}
}
rm(data)
}
if (save) {
message("Saving results of request \"",
request, "\" as R data object ...\n")
assign(dataName, getData())
data <- as.name(dataName)
#eval(substitute(assign(dataName, getData()),
# list(data <- as.name(dataName))))
# save hash of the request's SQL query as data object's attribute,
# so that we can detect when configuration contains modified query
attr(data, ATTR) <- base64(request)
# save current data frame to RDS file
saveRDS(data, rdataFile)
}
Please note that testing this code requires running it twice (first run - to store the object, second - to retrieve).
The problem is in your use of as.name, not in the code to save the object. This works perfectly fine:
data <- 1:10
object.name <- 'data.name'
query <- 'SELECT * FROM TABLE'
file <- tempfile()
assign(object.name, structure(data, SQL = query))
saveRDS(get(object.name), file)
read.object <- readRDS(file)
identical(read.object, get(object.name))
You're creating a name object, and assigning attributes to it, but you're expecting the data to be there. It won't be, a symbol is just a pointer to the value. You need to use eval() or something similar to get the value from the symbol.
Finally, I was able to return to this question. I have figured out the correct solution, so I'm answering my own question. The answer here is based on my reproducible example, but I have made corresponding changes in my more complex real-world R code. The solution is rather simple, but twofold, as follows.
Replace the original code data <- readRDS(rdataFile) with assign(dataName, readRDS(rdataFile)).
Replace the original code as.name(dataName) with get(dataName). An alternative to get() here is eval(parse(text=dataName)), which IMHO is more cumbersome.
Below I provide the solution's full source code, based on the original reproducible example. I don't provide the script's output, which is easy to reproduce (remember to run script at least twice). Again, thanks to everyone who helped with this question.
library(RCurl)
info <- "Important data"
ATTR <- "SQL"
request <- "SELECT info FROM topSecret"
dataName <- "sf.data.devLinks"
rdataFile <- "/tmp/testAttr.rds"
save <- TRUE
getData <- function() {
return (info)
}
requestDigest <- base64(request)
# check if the archive file has already been processed
message("\nProcessing request \"", request, "\" ...\n")
# read back the object with the attribute
if (file.exists(rdataFile)) {
# now check if request's SQL query hasn't been modified
assign(dataName, readRDS(rdataFile))
message("Retrieved object '", dataName, "', containing:\n")
message(str(get(dataName)))
requestAttrib <- attr(get(dataName), ATTR, exact = TRUE)
if (is.null(requestAttrib)) {
message("Object '", dataName, "' doesn't have attribute \"",
ATTR, "\"\n")
}
else {
message("Object '", dataName, "' contains attribute \"",
ATTR, "\":\n\"", base64(requestAttrib), "\"\n")
if (identical(requestDigest, requestAttrib)) {
message("Processing skipped: RDS file is up-to-date.\n")
save <- FALSE
return
}
}
}
if (save) {
message("Saving results of request \"",
request, "\" as R data object ...\n")
assign(dataName, getData())
message(str(dataName))
data <- get(dataName)
# alternative to using get(), but more cumbersome:
# data <- eval(parse(text=dataName))
# save hash of the request's SQL query as data object's attribute,
# so that we can detect when configuration contains modified query
attr(data, ATTR) <- base64(request)
message(str(data))
# save current data frame to RDS file
saveRDS(data, rdataFile)
}

Resources