download button disappear when deselecting column or editing data table in shiny app

夙愿已清 提交于 2019-12-11 10:59:29

问题


I built a shiny app for downloading customized and editable data table. Here I use iris dataset as an example.

According to this post, I add a button to download the whole dataset as csv.

However, one issue came up. When I tried to uncheck some column OR edit table, the download button simply disappear. And it never show up again.

I spend hours trying to figure it out but was unsuccessful. Does anyone know why that happens? Thanks a lot in advance.

library(shiny)
library(DT)
library(dplyr)


    # UI
    ui = fluidPage(
                   downloadButton("download1","Download iris as csv"),
                   DT::dataTableOutput('tbl'),
                   checkboxGroupInput('datacols', 
                                      label='Select Columns:',
                                      choices= c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
                                      selected = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Specie'),
                                      inline=TRUE )

                   )

    # SERVER
    server = function(input, output) {



        df = reactiveValues()

        observe ({

            df$dat = iris %>% select(one_of(input$datacols))
        })
        # render DT
        output$tbl = renderDT({
                datatable(df$dat,
                editable = "cell",
                callback = JS("$('div.dwnld').append($('#download1'));"),
                extensions = "Buttons",
                options = list(
                    dom = 'B<"dwnld">frtip',
                    buttons = list(
                        "copy" ) ) )

        })


        observeEvent(input[["tbl_cell_edit"]], {
            cellinfo <- input[["tbl_cell_edit"]]
            df$dat  <- editData(df$dat,  input[["tbl_cell_edit"]] )
        })

        output$download1 <- downloadHandler(
            filename = function() {
                paste("data-", Sys.Date(), ".csv", sep="")
            },
            content = function(file) {
                write.csv(df$dat, file)
            }
        )

    }

shinyApp(ui, server)

回答1:


Very interesting case.

Each time you edit a cell or select/unselect a column, this changes df$dat, and then the table is rerendered. But then the element #download1 which was included in the table does not exist anymore in the DOM.

We have to find a way to select/unselect some columns and to edit some cells without rerendering the table. Here is one:

library(shiny)
library(DT)
library(dplyr)

# UI
ui = fluidPage(
  downloadButton("download1", "Download iris as csv"),
  DTOutput('tbl'),
  checkboxGroupInput(
    'datacols', 
    label='Select Columns:',
    choices= c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species'),
    selected = c('Sepal.Length', 'Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species'),
    inline=TRUE)

)

# SERVER
server = function(input, output) {

  dat <- iris

  # render DT
  output$tbl = renderDT({
    datatable(dat,
              editable = "cell",
              callback = JS(
                "$('div.dwnld').append($('#download1'));",
                "var checkboxes = $('input[name=datacols]');",
                "checkboxes.each(function(index,value){",
                "  var column = table.column(index+1);",
                "  $(this).on('click', function(){",
                "    if($(this).prop('checked')){",
                "      column.visible(true);",
                "    }else{",
                "      column.visible(false);",
                "    }",
                "  });",
                "});"
              ),
              extensions = "Buttons",
              options = list(
                dom = 'B<"dwnld">frtip',
                buttons = list("copy")
              ) 
    )

  })

  observeEvent(input[["tbl_cell_edit"]], {
    cellinfo <- input[["tbl_cell_edit"]]
    dat <<- editData(dat, cellinfo, "tbl")
  })

  output$download1 <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".csv", sep="")
    },
    content = function(file) {
      write.csv(dat %>% select(one_of(input$datacols)), file)
    }
  )

}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/58188784/download-button-disappear-when-deselecting-column-or-editing-data-table-in-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!