Post image files to GoogleDrive from R? - r

I've been working with a nice dynamic dataset where I get data from a spreadsheet on GoogleDrive and analyze it in R. No issues there. Is there a way of serving some figures (i.e., some image files) back to GoogleDrive? This can be done in PHP (and Python I imagine). Any idea of whether this can be done directly in R? I.e., given
png(filename = "foo.png")
plot(1:10)
dev.off()
Can I upload foo.png to GoogleDrive from within an R script?

Related

Is there a way to convert an .Rmd file exported as a basic R Script using purl back into an .Rmd file?

I have a dataset that I am trying to get published as part of the supplementary information of a study that is in .Rmd format. The .Rmd file is set up to not only provide an easily readable printout of the statistical analyses performed in the study, but it also set up so to be a tool for other researchers working in the same area to use on their data. The intent being all they would have to do is insert their data and re-knit the file and their results would be printed out without having to rework the R code from scratch.
However, the journal will not accept .Rmd scripts as .Rmd files and possibly will not accept knitted .html printouts of an .Rmd file. The journal suggested to me that I save the .Rmd file as a plain R script using purl() and submit that instead. However, this creates a problem in that the script no longer generates an easily navigable printout (i.e., issues with headers and document text) and is more difficult to use. At the same time, I noticed that purl() seems to produce an R script that contains most of the information of the .Rmd file, particularly if one uses the options documentation=1 or documentation=2.
I am trying to figure out if there is any way to convert an .Rmd file back into an .Rmd file after it has been exported as a basic R script using purl? This way I could potentially submit the analysis as a basic R script as per the journal's requirements, but the user could convert it back into the script that produces a knitted html report if that is what they desire.

How to export mulitple PDFs from R

I'm fairly new to R and am running into some issues exporting multiple graphs from R into a PDF file. The graphs I've created are:
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
What would be the most efficient way to export these to a pdf?
Any guidance would be greatly appreciated.
Thank you!
Darko
Think of it this way - to save an R graph as a pdf file, you need to:
Open the pdf file;
Place the graph in that file;
Close the pdf file.
What this means in your case is that you have to do something like this:
pdf("plot1.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
dev.off()
pdf("plot2.pdf")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
dev.off()
pdf("plot3.pdf")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
The 3 pdf files you created will be stored in R's working directory, which you can find with the command:
getwd()
Once you know where R saved the pdf files, you have to locate that directory on your computer and then you can manually select and open the files. Each file will be single-page and contain one graph.
As already suggested here, you can also create a single, multi-page pdf file which will contain all 3 graphs (one graph per page):
pdf("plot.pdf")
plot(mirt.nom.cohen.pf.fa2,type="info")
plot(mirt.nom.cohen.pf.fa2,type="infotrace")
plot(mirt.nom.cohen.pf.fa2,type="trace")
dev.off()
This file will also be stored in R's working directory.
If you look at the help of the pdf function in R, you'll discover that you can use it with various options (e.g., width and height):
help(pdf)
do you want one to a page, or all on the same page?
to simply get them to a pdf file, you open the pdf device, indicate the file name, and then everything until you close it will go there.
pdf("plots.pdf")
your statements
dev.off()

Using R to transform all pages of a PDF into text, for multiple files

I'm using a looped 'pdf_render_page' function to create a bitmap of PDF documents that are then turned into raw text via the tesseract package. However this function works only given knowledge of file size. Does anyone know a way to take a pdf with an unknown page number total and discover the page count to then run this loop?
when using the pdftools package you can assign the length of pdf 'dummy.pdf' by doing:
pdf_length <- pdf_info("dummy.pdf")$pages

Loading ftp directly to R dataframe - how to see the original text?

Is it possible to load ftp files directly to the R workspace without downloading it?
I have 700+ files each around 1.5 Gb and I want to extract approx 0.1 % of the information from every files and add them into a single dataframe.
I had a look at Download .RData and .csv files from FTP using RCurl (or any other method), could not get it to work.
Edit: After some reading, i managed to get the files into R
library(httr)
r <- GET("ftp://ftp.ais.dk/ais_data/aisdk_20141001.csv", write_memory())
when i try to read the body i use
content(r, "text")
but the output is gibberish. It might be because of the encoding, but how do i know which encoding the server uses. Any ideas on how to get the original data from the ftp?
I found a solution, which is very simple, but works nonetheless:
library(data.table)
r <- fread("ftp://ftp.ais.dk/ais_data/aisdk_20141001.csv")
This blog was helpfull

OpenCPU and multi-page plots

I'm trying to capture a multi-plot pdf from a function. In R, this gives me a three page PDF:
pdf(file='test.pdf', onefile=TRUE)
lapply(1:3, 'plot')
dev.off()
Using OpenCPU:
$ curl http://localhost:6977/ocpu/library/base/R/lapply -H 'Content-Type: application/json' -d '{"X":[1,2,3], "FUN":"plot"}'
/ocpu/tmp/x0dc3dad0/R/.val
/ocpu/tmp/x0dc3dad0/graphics/1
/ocpu/tmp/x0dc3dad0/graphics/2
/ocpu/tmp/x0dc3dad0/graphics/3
/ocpu/tmp/x0dc3dad0/stdout
/ocpu/tmp/x0dc3dad0/source
/ocpu/tmp/x0dc3dad0/console
/ocpu/tmp/x0dc3dad0/info
I can get any of the individual pages as a single-page PDF file, but not as one combined file.
Two possible work-arounds, not without their share of problems:
Use par(mfrow), layout(), or a similar mechanism, though this will create a monster image in the end (I'm dealing with more than three images in my code).
Use tempfile, create an Rmd file on-the-fly, return the filename in the session (have not tested this yet), and use OpenCPU's processing of Rmd files. Unfortunately, this now uses LaTeX's geometries and page numbering (workarounds exist for this).
Are there other ways to do this?
Good question. OpenCPU captures graphics using evaluate which stores each graphic individually. The API itself doesn't support combining multiple graphics within a single file. I would personally do this sort of PDF post processing in the application layer (i.e. with non-R tools), but perhaps it would be useful to support this in the API.
Some suggestions:
Any file that your R function/script saves to the working directory (i.e. getwd()) will also become available through the API. So one thing you could do is in your R code manually create your combined pdf file and save it to the working directory and then download it through opencpu.
Graphics are actually recordedPlot objects, and besides png, pdf and svg, you can also retrieve the graphic as rds or rda. So you could write an R function that downloads the recordedPlot object from the API and then prints it. Not sure if that would be helpful in your use case.

Resources