I am developing a Shiny app that takes an Excel document, changes its structure and format, and returns a new Excel doc with 10 unique sheets (based on regions of US). The code I wrote is fine in regards to changing the data but it does not return an Excel file. Instead it simply returns a blank file. If someone knows how to answer this, that would be awesome. Here is my code, abridged to only include some of the data manipulation (so that is easier to comprehend).
Let me know if this makes sense!
Excel doc link on GDrive: https://drive.google.com/file/d/1OHaYh0OcMDTbU5BhrL81EO7d5pgwfJju/view?usp=sharing
UI:
library(shiny)
library(xlsx)
library(dplyr)library(shinythemes)
shinyUI(fluidPage(theme = shinytheme("spacelab"),
titlePanel("Dataset Manipulation Example"),
tags$caption("Edited by S. Gouyet 2.21.2018"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Insert File',
accept = c(".xlsx")
),
downloadButton("downloadData", "Download updated Excel document")
)
,
mainPanel(
)
)
)
)
Server:
shinyServer(function(input, output) {
reacdata<-reactive({
inFile1 <- input$file1
if(is.null(inFile1))
return(NULL)
df <- read_excel(inFile1$datapath)
df <- df %>% select(`SALES REGION`, `Price`)
Northeast1 <- df %>% filter(`SALES REGION` == "Northeast 1")
Northeast2 <- df %>% filter(`SALES REGION` == "Northeast 2")
CapMetro <- df %>% filter(`SALES REGION` == "Cap-Metro")
Southern <- df %>% filter(`SALES REGION` == "Southern")
Eastern <- df %>% filter(`SALES REGION` == "Eastern")
GreatLakes <- df %>% filter(`SALES REGION` == "Great Lakes")
Western <- df %>% filter(`SALES REGION` == "Western")
Pacific <- df %>% filter(`SALES REGION` == "Pacific")
wb <- createWorkbook()
sheet <- createSheet(wb, sheetName="addDataFrame1")
addDataFrame(df, sheet)
sheet <- createSheet(wb, sheetName="Northeast1")
addDataFrame(Northeast1, sheet)
sheet <- createSheet(wb, sheetName = "Northeast 2")
addDataFrame(Northeast2, sheet)
sheet <- createSheet(wb, sheetName = "Cap-Metro")
addDataFrame(Northeast2, sheet)
sheet <- createSheet(wb, sheetName = "Southern")
addDataFrame(Southern, sheet)
sheet <- createSheet(wb, sheetName = "Eastern")
addDataFrame(Southern, sheet)
sheet <- createSheet(wb, sheetName = "Great Lakes")
addDataFrame(GreatLakes, sheet)
sheet <- createSheet(wb, sheetName = "Western")
addDataFrame(Western, sheet)
sheet <- createSheet(wb, sheetName = "Pacific")
addDataFrame(Pacific, sheet)
})
output$downloadData <- downloadHandler(
filename ="test.xlsx",
content = function(file) {
saveWorkbook(wb, file)
}
)
})