2

Update

renderdatatable doesnt show actionbutton, renderDT shows but not able to just download the table although i can see the actionbutton being triggered with cat statement


I'm new to markdown trying to build a markdown application which needs to download data depending on the action/download button. In my example below, id like to have a downloadButton or downloadLink to download the row contents of the download button row, if i click on the first action button then id like to download mtcars 1st row values for mpg, cyl, disp to a csv or excel.

I have a fairly large subset in the actual application so i can filter accordingly.

The problem is i am not getting an action button but just raw html in my DT, not sure if i am missing small details.

    ---    
title: "Download data with download button"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
library(downloadthis)
library(dplyr)
```


```{r, echo=FALSE}

shinyInput <- function(FUN, n, id, ...) {
      vapply(seq_len(n), function(i){
        as.character(FUN(paste0(id, i), ...))
      }, character(1))
      
}

downloadButtonRmd <- function (outputId, label = "Download", class = NULL, ...)  {
     tags$a(id = outputId, class = paste("btn btn-default shiny-download-link", 
        class), href = "", target = "_blank", download = NA, 
        icon("download"), label, ...)
}

tab <- data.frame(head(mtcars[1:3]))
tab <- tab %>% mutate(
    dl1 = shinyInput(actionButton, nrow(.), 'button_', label = "Download", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
    dl2 = shinyInput(downloadButtonRmd, nrow(.), 'button_', label = "Download",onclick = 'Shiny.onInputChange(\"select_button1\", this.id)' ))
                 

# renderDataTable({
#   tab %>%
#     datatable(extensions = 'Buttons',
#             options = list(dom = 'Blfrtip',
#                            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
#                            lengthMenu = list(c(10,25,50,-1),
#                                              c(10,25,50,"All"))))
#   })

renderDT({
  datatable(tab,
                  options = list(pageLength = 25,
                                 dom        = "rt"),
                  rownames = FALSE,
                  escape   = FALSE)})  


observeEvent(input$select_button1, {
selectedRow <<- as.numeric(strsplit(input$select_button1, "_")[[1]][2])
      cat(input$select_button1)
  
  downloadHandler(filename = "Academic Report.csv",
                  content = function(file) {write.csv(tab[selectedRow,1:3], file, row.names = FALSE)},
                  contentType = "text/csv")
})
```

I've read through these links and many others but i'm unable to get what I intended

RShiny Download Button Within RMarkdown
R Shiny: Handle Action Buttons in Data Table
Thanks

Santhosh N
  • 25
  • 6
  • renderdatatable doesnt show actionbutton, renderDT shows but not able to just download the table although i can see the actionbutton being triggered with cat statement – Santhosh N Mar 21 '22 at 06:42

1 Answers1

2

Here is a way with the downloadthis package.

---
title: "DT download row"
author: "Stéphane Laurent"
date: "21/03/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DT)
library(downloadthis)
htmltools::tagList( # for the icons
  rmarkdown::html_dependency_font_awesome()
)
```

```{r}
dat <- mtcars
dat[["Download"]] <- vapply(1L:nrow(mtcars), function(i){
  as.character(
    download_this(
      .data = mtcars[i, ],
      output_name = paste0("mtcars - row ", i),
      output_extension = ".csv",
      button_label = "Download",
      button_type = "primary",
      icon = "fa fa-save",
      csv2 = FALSE,
      self_contained = TRUE
    )
  )
}, character(1L))
```

```{r}
datatable(
  dat,
  escape = FALSE,
  options = list(
    columnDefs = list(
      list(targets = ncol(dat), orderable = FALSE),
      list(targets = "_all", className = "dt-center")
    )
  )
)
```

enter image description here

Of course that won't work if you edit the table.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • thanks a lot, it works exactly the way I asked. I'll accept this as an answer. On the otherhand, could you please explain if it is possible the way that I had written before, if not, may be why... – Santhosh N Mar 21 '22 at 17:55