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")
})
}