Skip to content

Instantly share code, notes, and snippets.

@sachsmc
Created April 8, 2016 17:46
Show Gist options
  • Save sachsmc/768b497f314db57757067f20c06ea4a5 to your computer and use it in GitHub Desktop.
Save sachsmc/768b497f314db57757067f20c06ea4a5 to your computer and use it in GitHub Desktop.
Exporting data and figures from Shiny apps
## Inside shinyServer(function(input, output, session) {})
output$downloadData <- downloadHandler(
filename = function() { paste("NIH-percent-", input$yvar, ".xlsx", sep = "") },
content = function(file) {
myStyles <- c(rep("textStyle", ifelse(is.null(byvar2()), 2, 3)),
ifelse(input$yvar == "investigators", "countStyle",
"moneyStyle"), "percStyle")
outtab <- nicetable()
outtab[,grep("Percent", colnames(outtab))] <- outtab[,grep("Percent", colnames(outtab))]/100 ## because Excel stupidly multiplies by 100
title <- paste0("NIH ", ifelse(input$yvar=="investigators", "Investigators", "Funding"), " by Fiscal Year, ",
ifelse(input$mpi, " Contact PI only, ", " Including MPIs, "),
switch(input$rpg, "R01" = "R01 Equivalents",
"RPG" = "Research Project Grants",
"RG" = "Research Grants"), ifelse(input$noncompete, ", Competing awards only", ""))
wb <- formatDataFrame(outtab, ## this function is from the rreporttools package
"www/Standard Report Template.xlsx", ## edit this template if you want
file,
save = TRUE,
sheet = 2,
sr = 9,
sc = 1,
styleList = myStyles, title = title,
freezeFrame = FALSE)
}
)
output$downloadFigure <- downloadHandler(
filename = function() { paste("NIH-percent-Figure.jpeg") },
content = function(file) {
jpeg(file, width = 12, height = 10, units = "in", res = 500)
par(mar = c(16, 4, 4, 1))
mybarplot()(longSummary(), xlcol())
leg <- unique(longSummary()[,c(input$byvar1, byvar2()), drop = F])
if(ncol(leg) == 2){
xvar <- mylevels(leg[,1])
yvar <- mylevels(leg[,2])
} else{
yvar <- mylevels(leg[,1])
xvar <- NULL
}
pushViewport(viewport(x = 0.5, y = 0, height = .2, just = "bottom"))
mylegend(xvar, yvar, xlcol())
dev.off()
}
)
library(R2PPT)
## This assumes that there are 12 figures names img1.png - img12.png in the /figures-new folder
myPres <- PPT.Init(method = "RDCOMClient")
myPres <- PPT.AddBlankSlide(myPres)
for(i in 1:12){
filen <- paste(getwd(), "/figures-new/img", i, ".png", sep = "")
if(i > 1) myPres <- PPT.AddBlankSlide(myPres)
myPres <- PPT.AddGraphicstoSlide(myPres, file = filen,
size = c(10, 20, 700, 480))
}
myPres <- PPT.ApplyTemplate(myPres, file = "C:/Users/sachsmc/AppData/Roaming/Microsoft/Templates/NIH Template.potx")
myPres <- PPT.SaveAs(myPres, file = paste(getwd(), "Req 688 - Figures - Revised.pptx", sep = "/"))
myPres <- PPT.Close(myPres)
rm(myPres)
@sachsmc
Copy link
Author

sachsmc commented Apr 8, 2016

Download the rreporttools package from here: http://sachsmc.github.io/rreporttools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment