I'm wondering if it's possible to have a shiny app being able to download a PDF of selected table content.
For example, if I have two tables, and I select one table to be downloaded via PDF, it will download a PDF that has the title content I want and the specified table in a PDF format. Not sure how I can change the downloadHandler portion so that the PDF download works and not give me the following error:
Warning: Error in FUN: non-numeric argument to binary operator [No stack trace available]
.
The code is as follows:
df1<- data.frame(c(1:4),c("Z","Y","X","A"))
df2<- data.frame(c("Apple","Orange"),c(6.99,4.99))
colnames(df1)<-c("Col1","Col2")
colnames(df2)<-c("ColA","ColB")
library(shiny)
library(Cairo)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title="Test"),
dashboardSidebar(sidebarMenu(
menuItem("Data Table", tabName = "dashboard", icon = icon("th")))),
dashboardBody(tabItems(
# First tab content
tabItem(tabName = "dashboard",
fluidRow(
box(downloadButton("download", "PDF Download"),
radioButtons(inputId="filter1", label="Table", choiceNames = c("One","Two"), choiceValues = c("df1","df2"),inline= TRUE))),
fluidRow(box(
column(8, align="center", offset = 2,tags$b(textOutput("text1"))),
br(),br(),br(),br(),
textOutput("text2"),
tableOutput("static1"),
width=12))
)))
)
server <- function(input, output) {
output$text1 <- renderText({ "This Table" })
output$text2 <- renderText({"PR"})
df02 <- reactive({
get(input$filter1)
})
output$static1 <- renderTable({
df02()
})
output$download <- downloadHandler(
filename = 'report.pdf',
content = function(file) {
cairo_pdf(filename = "file123.pdf",
df02())
}, contentType = "application/pdf"
)
}
shinyApp(ui, server)
It seems that the PDF download does not work. I want the downloaded PDF to look like this when I select Table One and click on the PDF download button:
I want the downloaded PDF to look like this when I select Table Two to be downloaded:

