R-Shiny-如何将整个选项卡面板导出为png/pdf/excel?
我已经创建了一个仪表板(使用shinyUI/shinydashboard包),它有一个使用GT包设计的表 我正在尝试将整个面板/格式化表导出为pdf/image/(excel中的数据),但不知道如何进行导出 有没有办法达到我的要求 我在下面提到了我正在使用的脚本的简化版本 谢谢 脚本:R-Shiny-如何将整个选项卡面板导出为png/pdf/excel?,r,shiny,shinydashboard,shinyapps,R,Shiny,Shinydashboard,Shinyapps,我已经创建了一个仪表板(使用shinyUI/shinydashboard包),它有一个使用GT包设计的表 我正在尝试将整个面板/格式化表导出为pdf/image/(excel中的数据),但不知道如何进行导出 有没有办法达到我的要求 我在下面提到了我正在使用的脚本的简化版本 谢谢 脚本: TableA = data.frame(Product = c('iPhone', 'Macbook', 'Airpod', 'Macbook', 'Airpod', 'Macbook', 'iPhone'),
TableA = data.frame(Product = c('iPhone', 'Macbook', 'Airpod', 'Macbook', 'Airpod', 'Macbook', 'iPhone'), East = c(1:7), West = c(5:11), North = c(15:21), South = c(24:30))
library(shiny)
library(shinythemes)
library(DT)
library(rhandsontable)
library(tidyverse)
library(tidyquant)
library(knitr)
library(gt)
library(shinycssloaders)
library(shinydashboard)
library(shinyWidgets)
header = dashboardHeader(title = 'Shiny Dashboard', titleWidth = 400)
sidebar = dashboardSidebar()
body <- dashboardBody(uiOutput("mainpanel"))
ui = dashboardPage(header, sidebar, body)
############
server = function(input, output, session)
{
output$mainpanel = renderUI({
fluidRow(tabBox(width = 250, height = 100,
tabPanel("Apple Sales", value = 'tab1', gt_output(outputId = "TableC")%>% withSpinner(color="#3483CA", type = 1, size = 2), downloadButton(outputId = "Downloadpng", label = "Download Png"))
))
})
TableB = as.data.frame(TableA) %>%
gt() %>%
grand_summary_rows(columns = 2:5, fns = list(TotalSales = "sum")) %>%
tab_options(grand_summary_row.background.color = "#DDEBF7") %>%
cols_width(columns = 1 ~ px(1), columns = 2 ~ px(300), everything() ~ px(100)) %>%
tab_spanner(label = "Sales", columns = 3:6)
output$TableC = render_gt(expr = TableB)
output$Downloadpng = downloadHandler(filename = "Apple Sales.png",
content = function(file)
{
png(file)
with(tabPanel == 'tab1')
dev.off()
}
)
}
############
shinyApp(ui = ui, server = server)
TableA=data.frame(产品=c('iPhone','Macbook','Airpod','Macbook','Airpod','Macbook','iPhone'),东=c(1:7),西=c(5:11),北=c(15:21),南=c(24:30))
图书馆(闪亮)
图书馆(shinythemes)
图书馆(DT)
图书馆(rhandsontable)
图书馆(tidyverse)
图书馆(tidyquant)
图书馆(knitr)
图书馆(gt)
图书馆(shinycssloaders)
图书馆(shinydashboard)
图书馆(shinyWidgets)
页眉=仪表板页眉(标题='Shining Dashboard',标题宽度=400)
侧栏=仪表板侧栏()
正文%带有旋转键(color=“#3483CA”,type=1,size=2),downloadButton(outputId=“Downloadpng”,label=“Downloadpng”))
))
})
TableB=as.data.frame(TableA)%>%
gt()%>%
汇总行(列=2:5,fns=list(TotalSales=“sum”))%>%
tab_选项(grand_summary_row.background.color=“#DDEBF7”)%>%
列宽度(列=1~px(1),列=2~px(300),所有内容()~px(100))%>%
tab_扳手(label=“Sales”,columns=3:6)
输出$TableC=render\u gt(expr=TableB)
输出$Downloadpng=downloadHandler(filename=“Apple Sales.png”,
内容=函数(文件)
{
png(文件)
带(tabPanel==“tab1”)
发展主任()
}
)
}
############
shinyApp(用户界面=用户界面,服务器=服务器)
输出:
这可能是
shinyscreenshot
软件包的一个很好的用例。您可以使用screenshot按钮
功能并传递您想要图像的区域的id
library(shinyscreenshot)
header = dashboardHeader(title = 'Shiny Dashboard', titleWidth = 400)
sidebar = dashboardSidebar()
body <- dashboardBody(uiOutput("mainpanel"))
ui = dashboardPage(header, sidebar, body)
############
server = function(input, output, session)
{
output$mainpanel = renderUI({
fluidRow(tabBox(width = 250, height = 100,
tabPanel("Apple Sales", value = 'tab1',
gt_output(outputId = "TableC")%>%
withSpinner(color="#3483CA", type = 1, size = 2),
screenshotButton(selector="#TableC", label = 'Download Png', filename = 'screenshot')
)))
})
TableB = as.data.frame(TableA) %>%
gt() %>%
grand_summary_rows(columns = 2:5, fns = list(TotalSales = "sum")) %>%
tab_options(grand_summary_row.background.color = "#DDEBF7") %>%
cols_width(columns = 1 ~ px(1), columns = 2 ~ px(300), everything() ~ px(100)) %>%
tab_spanner(label = "Sales", columns = 3:6)
output$TableC = render_gt(expr = TableB)
}
shinyApp(ui = ui, server = server)
图书馆(shinyscreenshot)
页眉=仪表板页眉(标题='Shining Dashboard',标题宽度=400)
侧栏=仪表板侧栏()
主体%
带旋转器(颜色=“#3483CA”,类型=1,大小=2),
屏幕截图按钮(选择器=“#TableC”,标签='下载Png',文件名='屏幕截图')
)))
})
TableB=as.data.frame(TableA)%>%
gt()%>%
汇总行(列=2:5,fns=list(TotalSales=“sum”))%>%
tab_选项(grand_summary_row.background.color=“#DDEBF7”)%>%
列宽度(列=1~px(1),列=2~px(300),所有内容()~px(100))%>%
tab_扳手(label=“Sales”,columns=3:6)
输出$TableC=render\u gt(expr=TableB)
}
shinyApp(用户界面=用户界面,服务器=服务器)
点击“下载PNG”
按钮后,返回下图
非常感谢你,罗纳克。有什么方法可以将仪表板导出为pdf和excel版本吗?我认为这需要一种非常不同的方法,这对于本文来说非常广泛。你能建议我从哪里获得帮助吗?你可以为此提出一个新问题。谢谢Ronak。我提出了一个新问题,下面给出了链接。如果你有时间请帮助我。。。