0

I want to build a web app that can download excel files. BUT I have a problem when I use the web app: After I click the "Download" button, it shows that the file name is in quotes like the picture I uploaded, and I can't find anything in my documents after I click the "Save" button, whether I delete the quotes or not.

enter image description here

Does anyone have a good suggestion to solve this problem? Thank you for your help.

library(shiny)
library(shinydashboard)
library(XLConnect)

ui <- dashboardPage(
  dashboardHeader(title = "excel download"),
  dashboardSidebar(downloadLink("downloadData", "Download")),
  dashboardBody(),
  skin = "purple"
)

server <- function(input, output) {
  data1 <- mtcars
  output$downloadData <- downloadHandler(
    filename = function(){"mtcars.xlsx"},
    content = function(file) {
      fname <- paste(file,"xlsx",sep=".")
      wb <- loadWorkbook(fname,create = TRUE)
      createSheet(wb,"cars")
      writeWorksheet(wb,data = data1,sheet = "cars")
      saveWorkbook(wb)
      file.rename(fname,file)
    },
    contentType="application/xlsx" 
  )
}

shinyApp(ui = ui, server = server)
MFR
  • 2,049
  • 3
  • 29
  • 53
ghoost2010
  • 3
  • 1
  • 3
  • What is the working directory of that code? Maybe you (or the program) don't have the rights to download data there. – llrs Nov 10 '16 at 07:48
  • Maybe it because my system is Windows 7 not OS. – ghoost2010 Nov 10 '16 at 09:32
  • The OS shouldn't make a difference but the right to save and store data in a directory could – llrs Nov 10 '16 at 09:57
  • This problem happens when you run the app in RStudio. It should go away when you open the app in your browser. – oshun Nov 11 '16 at 07:19
  • Yes, you are right. I published the code on Rstudio Shiny Server, and It works well.I thought it may because the file path in windows has “\” not "/". – ghoost2010 Nov 11 '16 at 09:52

2 Answers2

0

Weird, your code works perfectly on my system:

> R.version                                  
platform       x86_64-apple-darwin13.4.0   
arch           x86_64                      
os             darwin13.4.0                
system         x86_64, darwin13.4.0        
status                                     
major          3                           
minor          3.1                         
year           2016                        
month          06                          
day            21                          
svn rev        70800                       
language       R                           
version.string R version 3.3.1 (2016-06-21)
nickname       Bug in Your Hair

What are you using?

Edit:

Using

filename = "mtcars.xlsx",

Also works, you should try that.

  • It may because that my system is windows 7.By the way I published the code on Rstudio Shiny Server, and It works well. – ghoost2010 Nov 10 '16 at 09:29
0

As mentioned here, you should run your app in a browser rather than in RStudio

Adriana Huante
  • 352
  • 2
  • 11