下载R中闪亮的绘图和表格;从pickerInput中的表格/绘图选择中进行选择
我想在我闪亮的应用程序中添加一个下载功能,用户可以从下载R中闪亮的绘图和表格;从pickerInput中的表格/绘图选择中进行选择,r,plot,dplyr,shiny,download,R,Plot,Dplyr,Shiny,Download,我想在我闪亮的应用程序中添加一个下载功能,用户可以从pickerInput下载指定的表格(csv)或绘图(png)。也许有比pickerInput更合适的替代方案,这将使它更简单,但以下是我迄今为止的代码(使用mpg数据作为可复制的最小示例):- #用户界面 ui这是一种方法: #UI ui<-fluidPage( tabPanel("Minimal Example", sidebarLayout( sidebarPa
pickerInput
下载指定的表格(csv)或绘图(png)。也许有比pickerInput
更合适的替代方案,这将使它更简单,但以下是我迄今为止的代码(使用mpg
数据作为可复制的最小示例):-
#用户界面
ui这是一种方法:
#UI
ui<-fluidPage(
tabPanel("Minimal Example",
sidebarLayout(
sidebarPanel(width = 4,
pickerInput("manufacturer", "Select manufacturer",
choices = unique(mpg$manufacturer),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
pickerInput("model", "Select model",
choices = unique(mpg$model),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
pickerInput("eda_plotpick", "Select plot to save",
choices = c("Scatterplot",
"Barplot")),
pickerInput("eda_tablepick", "Select table to save",
choices = c("mpg",
"mpg_filtered")),
actionButton("run_eda", "Run analysis"),
downloadButton("downloadplot", "Download plot"),
downloadButton("downloadtable", "Download table")),
mainPanel(
column(width = 8, box("Selected plot", plotOutput("myplot"), width = "100%")),
column(width = 8, box("Selected table", tableOutput("mytable"), width = "100%"))
)
)
)#end of tabpanel
)#end of fluidpage
#SERVER
server<-function(input,output,session){
observeEvent(input$run_eda,{
plot<- reactive({
req(input$manufacturer,input$model)
if (input$eda_plotpick=="Scatterplot"){
plot<-ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(size=qsec))
}else plot<-ggplot(mpg,aes(y = class))+geom_bar()
plot
})
output$myplot <- renderPlot({
plot()
})
data <- reactive({
req(input$manufacturer,input$model)
if (input$eda_tablepick=="mpg_filtered"){
data <- mpg %>%
filter(manufacturer %in% input$manufacturer) %>%
filter(model %in% input$model)
}else data <- mpg
data
})
output$mytable <- renderTable({
data()
})
output$downloadtable <- downloadHandler(
filename = function() {
paste('data-', input$eda_tablepick, '.csv', sep='')
},
content = function(file) {
write.csv(data(), file)
}
)
output$downloadplot <- downloadHandler(
filename = function() {
paste('plot-', input$eda_plotpick,'.png', sep='')
},
content = function(con) {
png(con, units = "px")
print(plot())
dev.off()
}, contentType = 'image/png'
)
})#end of observe event
}#end of server
shinyApp(ui,server)
#用户界面
用户界面
#UI
ui<-fluidPage(
tabPanel("Minimal Example",
sidebarLayout(
sidebarPanel(width = 4,
pickerInput("manufacturer", "Select manufacturer",
choices = unique(mpg$manufacturer),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
pickerInput("model", "Select model",
choices = unique(mpg$model),options = list('actions-box'=TRUE, 'live-search'=TRUE), multiple = T),
pickerInput("eda_plotpick", "Select plot to save",
choices = c("Scatterplot",
"Barplot")),
pickerInput("eda_tablepick", "Select table to save",
choices = c("mpg",
"mpg_filtered")),
actionButton("run_eda", "Run analysis"),
downloadButton("downloadplot", "Download plot"),
downloadButton("downloadtable", "Download table")),
mainPanel(
column(width = 8, box("Selected plot", plotOutput("myplot"), width = "100%")),
column(width = 8, box("Selected table", tableOutput("mytable"), width = "100%"))
)
)
)#end of tabpanel
)#end of fluidpage
#SERVER
server<-function(input,output,session){
observeEvent(input$run_eda,{
plot<- reactive({
req(input$manufacturer,input$model)
if (input$eda_plotpick=="Scatterplot"){
plot<-ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(size=qsec))
}else plot<-ggplot(mpg,aes(y = class))+geom_bar()
plot
})
output$myplot <- renderPlot({
plot()
})
data <- reactive({
req(input$manufacturer,input$model)
if (input$eda_tablepick=="mpg_filtered"){
data <- mpg %>%
filter(manufacturer %in% input$manufacturer) %>%
filter(model %in% input$model)
}else data <- mpg
data
})
output$mytable <- renderTable({
data()
})
output$downloadtable <- downloadHandler(
filename = function() {
paste('data-', input$eda_tablepick, '.csv', sep='')
},
content = function(file) {
write.csv(data(), file)
}
)
output$downloadplot <- downloadHandler(
filename = function() {
paste('plot-', input$eda_plotpick,'.png', sep='')
},
content = function(con) {
png(con, units = "px")
print(plot())
dev.off()
}, contentType = 'image/png'
)
})#end of observe event
}#end of server
shinyApp(ui,server)