是否可以调整Excel输出的输出? 我希望能够紧接着做下面的事情。
使用按钮扩展名为MWE。我找到了原始javascrtip DT here的一些信息,但这对我来说有点难以转移到R中。
rm(list=ls())
library(shiny)
library(datasets)
library(DT)
library(data.table)
DT<-data.table(iris)
server<-shinyServer(function(input, output) {
output$view <- DT::renderDataTable(
DT[Sepal.Width<=input$width,.SD],extensions = c( 'FixedHeader','Buttons'),
options=list(pageLength=60,fixedHeader = TRUE,dom = 'Bfrtip',buttons = c( 'csv', 'excel' )))
})
ui<-shinyUI(fluidPage(
titlePanel("Shiny MWE"),
sidebarLayout(
sidebarPanel(
sliderInput("width", label = h3("Min width"),
min=min(DT$Sepal.Width), max=max(DT$Sepal.Width), value=mean(DT$Sepal.Width),
)),
mainPanel(
DT::dataTableOutput("view")
)
)
))
runApp(list(ui=ui,server=server))
答案 0 :(得分:1)
我也意识到我不得不放弃按钮&#39;扩展,也有其他原因。例如,excel下载按钮仅导出应用程序上的视图,而不是整个数据集。 (可以使用选项server = FALSE来修复,这对于较大的数据集来说太慢了)
我选择了openxlsx软件包,需要安装Rtools,我遇到了一些困难(找到了将其添加到windows路径的解决方案([Error: zipping up workbook failed when trying to write.xlsx)
所以我发布的代码大部分都是我想要的,或者我可以继续使用openxlsx命令。有xlsx软件包或其他软件包可供选择,我也无法安装。
rm(list=ls())
library(shiny)
library(datasets)
library(DT)
library(data.table)
library(openxlsx)
DT<-data.table(iris)
# Style created for openxlsx see help
hs <- createStyle(textDecoration = "BOLD", fontColour = "#FFFFFF", fontSize=12,
fgFill = "#177B57",border="Bottom",borderStyle=c("thick"))
#Server
server<-shinyServer(function(input, output) {
output$view <- DT::renderDataTable(
DT[Sepal.Width<=input$width,.SD],extensions = c( 'FixedHeader'),
options=list(pageLength=20,fixedHeader = TRUE,dom = 'frtip'))
#Include DownloadHandler
output$downloadData <- downloadHandler(
filename = function() { paste0("test.xlsx") },
content = function(file) {
wb<-createWorkbook() # Create wb in R
addWorksheet(wb,sheetName="Output") #create sheet
#Creates a Data Table in Excel if you want, otherwhise only use write Data
writeDataTable(wb,1, DT[Sepal.Width<=input$width,.SD], colNames = TRUE, headerStyle = hs,startRow=2,tableStyle = "TableStyleLight1")
mergeCells(wb,sheet = "Output", cols=1:5, rows=1)
writeData(wb,1, "Include text also based on reactive function and in merged cells" )
saveWorkbook(wb, file = file, overwrite = TRUE)
},
contentType= "excel/xlsx")
})
ui<-shinyUI(fluidPage(
titlePanel("Shiny MWE"),
sidebarLayout(
sidebarPanel(
sliderInput("width", label = h3("Min width"),
min=min(DT$Sepal.Width), max=max(DT$Sepal.Width), value=mean(DT$Sepal.Width),
),
downloadButton('downloadData', 'Download')),
mainPanel(
DT::dataTableOutput("view")
)
)
))
runApp(list(ui=ui,server=server),launch.browser=T) # Download button only works in browser