我閃亮的應用程式生成了許多有用的圖表。我想允許用戶下載各種格式的圖表。
我之前使用如何保存在閃亮的應用程式中制作的圖作為指南,對單個圖表進行了此操作。但是,我最終會為每個額外的情節創建更多重復的代碼。我不是程式員,但似乎我應該能夠撰寫一個函式來執行此操作,因為我只是將引數傳遞給 downloadHandler 和 ggsave,但我無法弄清楚。
下面的 MRE 代表一個頁面,其中包含十個不同的圖表。有沒有辦法撰寫一個函式來接收來自按鈕的繪圖 ID(如標簽或其他東西?)和來自 selectInput 的格式以將這些引數傳遞給 downloadHandler 和 ggsave 以將每個圖形保存為選定的格式?底部的功能顯示了我的想法,但我不知道從哪里開始,或者這是否是正確的方向。
謝謝!
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show plots and download buttons
mainPanel(
plotOutput("distPlot"),
fluidRow(
column(3,
downloadButton("dl_plot1")
),
column(3,
selectInput("plot1_format",label = "Format",choices = c("SVG","PDF","JPEG","PNG"),width = "75px")
)
),
plotOutput("scat_plot"),
column(3,
downloadButton("dl_plot2")
),
column(3,
selectInput("plot2_format",label = "Format",choices = c("SVG","PDF","JPEG","PNG"),width = "75px")
)
)
)
)
# Define server logic required to draw a histogram and scatterplot
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- faithful$waiting
binwidth<-(max(x)-min(x))/input$bins
p<-ggplot(faithful,aes(waiting))
geom_histogram(binwidth = binwidth)
p
})
output$scat_plot<-renderPlot({
p<-ggplot(faithful,aes(x=waiting,y=eruptions))
geom_point()
p
})
downloadPlot <- function(plot_name,file_name,file_format){#concept code
downloadHandler(
filename=function() { paste0(file_name,".",file_format)},
content=function(file){
ggsave(file,plot=plot_name,device=file_format)
}
)
}
}
# Run the application
shinyApp(ui = ui, server = server)
uj5u.com熱心網友回復:
為了在不復制代碼的情況下達到您想要的結果,您可以(或必須)使用 Shiny 模塊。基本上,一個模塊是一對 UI 功能和一個服務器功能。有關模塊的更多資訊,我建議看看例如Mastering shiny,ch。19 .
在下面的代碼中,我使用一個模塊來處理下載部分。downloadButtonUIand的作業downloadSelectUI是為檔案格式添加一個下載按鈕和一個selectInput。完成艱苦的downloadServer作業并以所需的格式保存繪圖。
注意:除了下載模塊之外,我將繪圖的代碼移動到reactives 以便可以將繪圖傳遞給downloadHandler或下載模塊。
library(shiny)
library(ggplot2)
# Download Module
downloaButtondUI <- function(id) {
downloadButton(NS(id, "dl_plot"))
}
downloadSelectUI <- function(id) {
selectInput(NS(id, "format"), label = "Format", choices = c("SVG", "PDF", "JPEG", "PNG"), width = "75px")
}
downloadServer <- function(id, plot) {
moduleServer(id, function(input, output, session) {
output$dl_plot <- downloadHandler(
filename = function() {
file_format <- tolower(input$format)
paste0(id, ".", file_format)
},
content = function(file) {
ggsave(file, plot = plot)
}
)
})
}
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
)
),
# Show plots and download buttons
mainPanel(
plotOutput("distPlot"),
fluidRow(
column(3, downloaButtondUI("distPlot")),
column(3, downloadSelectUI("distPlot"))
),
plotOutput("scat_plot"),
fluidRow(
column(3, downloaButtondUI("scatPlot")),
column(3, downloadSelectUI("scatPlot"))
),
)
)
)
server <- function(input, output) {
dist_plot <- reactive({
p <- ggplot(faithful, aes(waiting))
geom_histogram(bins = input$bins)
p
})
scat_plot <- reactive({
p <- ggplot(faithful, aes(x = waiting, y = eruptions))
geom_point()
p
})
output$distPlot <- renderPlot({
dist_plot()
})
output$scat_plot <- renderPlot({
scat_plot()
})
downloadServer("distPlot", dist_plot())
downloadServer("scatPlot", scat_plot())
}
# Run the application
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:4092

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462724.html
上一篇:組合多個直方圖ggplot
下一篇:為組設定特定的色階
