0

I am trying to download the final datatable and plot in shinydashboard. Although there is no error in the process, it doesn't work and the final output files (for table (csv) and plot (png)) are not created.

If anyone has any suggestions on how to download correctly table and plot using the shiny dashboard it would be much appreciated.

I have attached the code.

****************************ui******************************
library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
library(plotly)

header <- dashboardPage(skin = "green",
                        dashboardHeader(title = "TEST"),
                        dashboardSidebar(
                          sidebarMenu(dir="ltr",align="right",
                        menuItem("Households", tabName = "hhdemostats", icon = icon("users")))),
                        dashboardBody(
                          dir="ltr",

                          tabItems(
                          tabItem(tabName = "hhdemostats",
                                  fluidRow(
                                    tabsetPanel(
                                      tabPanel("Inputs",
                                     box(status = "info",solidHeader = TRUE,
                                        title = "Year",
                                        selectInput(inputId="slcT2Year",label="Year",
                                                    choices=list(1390,1391,1392,1393,
                                                                 1394,1395,1396,1397))),
                                    box(status = "info", solidHeader =TRUE,
                                        title = "Variables",
                                        selectInput("slcT2Var","Variables",
                                                    list("FoodExpenditure","Cloth_Exp"),
                                                    selected="FoodExpenditure")),
                                    box(status="info", solidHeader = TRUE,
                                        title = "Groups",
                                        selectInput("slcT2Grp","Groups",
                                                    list("Decile","Percentile"))),
                                    box(status="info", solidHeader = TRUE,
                                        title = "Statistics",
                                        selectInput("slcT2Stat","Satistics",
                                                    list("mean","median","min","max",
                                                         "var","sd","range","sum","IQR")))),
                                  tabPanel("Table",
                                    box(status = "info",solidHeader = TRUE,
                                        DT::dataTableOutput("tblT2Stats"))
                                    ,downloadButton("downloadData", "Download")),
                                  tabPanel("Plot",
                                    box(status = "info",solidHeader = TRUE,
                                       plotlyOutput("pltT2barchart"))
                                    ,downloadButton("downloadPlot", "Download"))))))))

and my server is as follows:

****************************server******************************
library(shiny)
library(shinydashboard)
library(data.table)
library(ggplot2)
library(DT)
library(plotly)

app_server <- function(input, output,session) {

  ##################### Table #########################
  output$tblT2Stats <- DT::renderDataTable({
    y <- input$slcT2Year
    vs <- input$slcT2Var
    gs <- input$slcT2Grp
    st <- input$slcT2Stat

    fn1 <- paste0("data/Y",substr(y,3,4),"Total8.rda")
    load(fn1)
    fnc1 <- get(st)
    Total[,lapply(.SD,fnc1,na.rm=TRUE), by=gs,.SDcols=vs]
  })

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("dataset", ".csv", sep="")
    },
    content = function(file) {
      write.csv(Total, file)
    })

  ##################### Plot #########################
  output$pltT2barchart <- renderPlotly({
    y <- input$slcT2Year
    vs <- input$slcT2Var
    gs <- input$slcT2Grp
    st <- input$slcT2Stat

    fn1 <- paste0("data/Y",substr(y,3,4),"Total8.rda")
    load(fn1)
    fnc1 <- get(st)

    SD <- Total[,lapply(.SD,fnc1, na.rm=TRUE), by=gs,.SDcols=vs]
    ggplot(SD) +  geom_col(aes_string(x=gs,y=vs,fill  = gs))

  })

  output$downloadPlot <- downloadHandler(
    filename = function() {
      paste("plot", ".csv", sep="")
    },
    content = function(file) {
      ggsave(file,SD(),device = "png")
    })


  session$onSessionEnded(function() {
    stopApp()
    #    q("no")
  })

}
  • In `ggsave(file,SD(),device = "png")`, `SD()` appears to be undefined. Are you thinking it's a reactive created in your `output$pltT2barchart` reactive? It isn't... Your code is not simple (too many irrelevant elements) nor self-contained (it references the `Total8.rda` file to which we don't have access). So it's going to be difficult to help you further. – Limey Jun 20 '20 at 09:24
  • As @Limey stated it is hard to help without a reprex. Please look at this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example , so others can help you. To downloadPlot, you need to have `paste("plot",".png",sep="")` as you are using png device and not csv. Apart from this syntax, I am not sure where you are defining a reactive plot `SD()` or `Total` data frame. Are you able to see onscreen the display from `plotlyOutput("pltT2barchart")` or `DT::dataTableOutput("tblT2Stats")`? If so, you should be able to download it, if named properly. – YBS Jun 23 '20 at 01:18

0 Answers0