0

I want to create a Shiny App with a single tab navbar and the navbar has a logo and some download buttons. I used Shiny NavBar add additional info to create buttons using HTML, but I'd like the onClick function to be the same as the button I included as output$downloadData. Is it possible to mix and match R code and JS to have the button in the navbar be a downloadButton?

library(shiny)

# Define UI for application that draws a histogram
ui <- navbarPage(

   # Application title
   "Old Faithful Geyser Data",

   # Sidebar with a slider input for number of bins 

      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ), 

      # I included download buttons here for functionality,
      # these are the buttons I'd like in the top right
      # Can I use onclick=downloadData or something within my HTML?
      mainPanel(
        downloadButton("downloadData", "CSV"),
        downloadButton("downloadData2", "TXT"),
         tableOutput("dist")
      ),


   # Can I add R code to the HTML code so that onclick
   # by button uses output$Downloaddata below?
   tags$script(HTML("var header = $('.navbar> .container-fluid');
                   header.append('<div style=\"float:right; valign:middle\"><button onClick=downloadCSV(); style=\"valign:middle;\">Download CSV</button><button>Download SAS</button></div>');
                    console.log(header)"))
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  dummy <- data.frame(x = c(1,2,3), y = c(4,5,6))

   output$dist <- renderTable({
     dummy
   })

   output$downloadData <- downloadHandler(
     filename = function() {
       paste(dummy, ".csv", sep = "")
     },
     content = function(file) {
       write.csv(dummy, file, row.names = FALSE)
     }
   )

   output$downloadData2 <- downloadHandler(
     filename = function() {
       paste(dummy, ".csv", sep = "")
     },
     content = function(file) {
       write.csv(dummy, file, row.names = FALSE)
     }
   )

}

# Run the application 
shinyApp(ui = ui, server = server)
MayaGans
  • 1,815
  • 9
  • 30

0 Answers0