2017-05-17 8 views
0

光沢のあるアプリケーションから同じシートに複数の(複数の)データセットを含む1つのExcelファイルにデータをダウンロードしたいという光り輝くアプリケーションを作成します。私は他の同様の質問を見ましたが、私はその助けを借りてコードを手に入れることができません。使用さ複数のデータセットを同じシートに入れてExcelをダウンロードR Shiny

データセット:

sample <- structure(list(type = structure(c(1L, 5L, 3L, 5L, 2L, 4L), .Label = c("add select multiple prompt using alphaOptions", 
"add select multiple prompt using imageOptions", "add select one prompt using alphaOptions", 
"add select one prompt using imageOptions", "add select one prompt using numOptions" 
), class = "factor"), name = structure(c(4L, 5L, 3L, 6L, 1L, 
2L), .Label = c("grid", "grid_two_columns", "quick_advance", 
"select", "select1", "spinner"), class = "factor"), caption = structure(c(4L, 
5L, 3L, 6L, 1L, 2L), .Label = c("grid widget", "grid with a maximum of two columns", 
"quick advance select widget", "select multiple widget", "select one widget", 
"spinner widget"), class = "factor"), hint = structure(c(4L, 
6L, 1L, 3L, 2L, 5L), .Label = c("click a choice to select it and advance to the next question", 
"click an image to select it (you must have the images on your sdcard to see them)", 
"click the button to provide a response", "don't pick c and d together", 
"regardless of screen size this widget will only show two columns of images", 
"scroll down to see default selection"), class = "factor")), .Names = c("type", 
"name", "caption", "hint"), class = "data.frame", row.names = c(NA, 
-6L)) 



profile <- structure(list(Company.Name = structure(c(1L, 3L, 4L, 2L, 5L), .Label = c("Address", 
"Assigned MB", "Contact Name", "Contact Phone", "Website"), class = "factor"), 
    ABC = structure(c(2L, 5L, 1L, 3L, 4L), .Label = c("(398) 657-8401", 
    "48,S St, Denver, CO, 80233", "Bob Harris, Active", "www.abc.com", 
    "John Gardner"), class = "factor")), .Names = c("Company.Name", 
"ABC"), class = "data.frame", row.names = c(NA, -5L)) 

私はcsvファイルと同じデータを持っています。これは私がして、両方のデータセットをダウンロードするためにしようとしていますものです

ui.R

shinyUI(pageWithSidebar(
    headerPanel('Download'), 
    sidebarPanel(
    downloadButton('downloadData', 'Download') 
), 
    mainPanel(
) 
)) 

server.R

sample <- read.csv("sample.csv") 
profile <- read.csv("profile.csv") 

shinyServer(function(input, output) { 

    output$downloadData <- downloadHandler(
    filename = "test.xlsx", 
    content = function(file) { 
     write.xlsx2(profile, file, sheetName = "Sheet1") 
     write.xlsx2(sample, file, sheetName = "Sheet2", append = TRUE) 
    } 
) 
}) 

:以下

は、2枚のExcelのための私のコードです同じシート。

shinyServer(function(input, output) { 

    output$downloadData <- downloadHandler(
    filename = "test.xlsx", 
    content = function(file) { 
     write.xlsx2(profile, file, sheetName = "Sheet1") 
     write.xlsx2(sample, file, sheetName = "Sheet1", append = TRUE) 
    } 
) 
}) 

これは私が取得エラーです:

Error : java.lang.IllegalArgumentException: The workbook already contains a sheet of this name 
Warning: Error in .jcall: java.lang.IllegalArgumentException: The workbook already contains a sheet of this name 
Stack trace (innermost first): 

私はそれらの間のいくつかの領域下の画像のようなもので1枚で、他の下の一つであることがprofilesampleデータセットを期待しています: enter image description here

答えて

1

私は与えられたシート内append()作品とは思わないが、あなたはaddDataFrame()機能を使用することができます。

data <- data.frame(a = 1:3, b = 4:6, c = 7:9) 

wb <- createWorkbook() 
sheet <- createSheet(wb, sheetName="addDataFrame1") 

addDataFrame(data, sheet, row.names = FALSE) 
addDataFrame(data, sheet, startRow = nrow(data) + 3, , row.names = FALSE) 

saveWorkbook(wb, file = "test.xlsx") 
+0

ありがとう@treysp。それはうまくいった! – krish

関連する問題