Hello,
It was a bit of a challenge, but here is the result:
library(shiny)
library(ggplot2)
library(lubridate)
library(webshot)
ui <- fluidPage(
titlePanel(title=h3("XXXXXX")),
sidebarLayout(
sidebarPanel(
(h4(strong("Baseline Information:"))),
textInput("name", "Full Name", ""),
numericInput("age", "Age", ""),
numericInput("bmi", "BMI", ""),
submitButton("SUBMIT"),
p("Press SUBMIT to Calculated ")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Patient Data",
tableOutput("rtab"),
downloadButton("down_data","Download Data")
)
),
imageOutput("myImg")
)
)
)
server <- function(input, output) {
output$ptname <- renderText({
paste(input$name)
})
indata <- renderTable({
ddata <-data.frame(Question=c('Age', 'BMI'),
Answer=c(input$age, input$bmi))
})
output$rtab <- renderText({
HTML("<h3>",input$name,"</h3><h4>","PPPPPPP","</h4>","<br>",indata(),
"<br>",paste("[",format(now(),"%Y-%m-%d %H:%M"),"]"),"<br>","
<br>")
})
output$down_data <- downloadHandler(
filename = "odata.png",
content = function(file) {
write(HTML("<div class='toSave' style='background-color:white; border: 1px solid black;
padding: 10px 10px; display: inline-block;'><h3>",
input$name, "</h3><h4>","PPPPPPP","</h4>","<br>",indata(),"<br>",
paste("[",format(now(),"%Y-%m-%d %H:%M"),"]</div>")), file = "htmlImage.html")
webshot(url = "htmlImage.html", file = file, selector = ".toSave")
})
}
shinyApp(ui, server)
- I created a separate HTML file containing only the data needed in the image
- I used the
webshot
package to capture html output from that page and convert it to an image
- Note that I used some CSS styling to ensure the final image looks good
Hope this helps,
PJ