r - 如何下载在 R Shiny 中绘制的数据?
问题描述
我有这段代码,允许您选择数据,然后在 R 闪亮中绘制它。我尝试了很多方法来保存它,但它总是空白?我认为问题可能是由于粘贴行中的输入,但我不知道用什么替换它?
对此的任何帮助都会很棒!代码:
library(shiny)
library(ggplot2)
library(shinythemes)
ui <- fluidPage(
navbarPage("Data Analysis Training:", id="navbar",
tabPanel("Upload Tab",
titlePanel("Upload your file here."),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
tags$hr(),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head"),
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"')),
mainPanel(
verbatimTextOutput("summary"),
tableOutput("contents")
))),
tabPanel("Graphing",
titlePanel("Plotting Graphs"),
sidebarLayout(
sidebarPanel( uiOutput("X_axis"),
uiOutput("Y_axis"),
),
mainPanel(
h3(textOutput("caption")),
plotOutput("plot"),
downloadButton("downloadData", "Download")
)
)),
tabPanel(title = "Quit", value = "stop", icon = icon("circle-o-notch"))
))
server <- function(input, output, session) {
onSessionEnded(stopApp)
data <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath, header = input$header, sep = input$sep, quote = input$quote)
return(df)
})
output$contents <- renderTable({
if (input$disp == "head") {
return(head(data()))
}
else {
return(data())
}
})
output$summary <- renderPrint({
summary(data())
})
output$X_axis <- renderUI({
selectInput("variableNames_x", label = "X_axis", choices = names(data()))
})
output$Y_axis <- renderUI({
selectInput("variableNames_y", label = "Y_axis", choices = names(data()) )
})
plotteddata <- function(){
selecteddata <- data.frame(data()[[input$variableNames_x]], data()[[input$variableNames_y]])
colnames(selecteddata) <- c("X", "Y")
return(selecteddata)
}
observe({
if (input$navbar == "stop")
stopApp()
})
output$plot <- renderPlot({
if (is.null(data)) { return(NULL)
} else {
ggplot(plotteddata(),aes(x = X,y = Y)) + geom_point(colour = 'red', pch=1) +
labs(y = input$variableNames_y,
x = input$variableNames_x,
title = "Data Analysis Plotting")
}
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$plot, ".png", sep = ".")
},
content = function(file){
png(file)
plotteddata()
dev.off()
}
)
}
shinyApp(ui, server)
对此的任何帮助都会很棒!
解决方案
推荐阅读
- sql - SQL 如何按地区名称对其进行 GROUP,并获取所有行程的数量?甲骨文数据库
- go - Go for protobuf 中的相对导入,找不到模块路径
- java - Java - 覆盖在构造函数中创建的对象?
- python - 部署在 Gcloud 上时获取 Flask 开发服务器的服务器错误,但由于许可,可以作为本地服务器正常工作
- elasticsearch - Jmxtrans Config 用于对同一个 Writer 的多个查询
- assembly - 如何在 x86 汇编中以视频模式 13h 显示整数?
- python-3.x - 在 python 中使用枚举和简单的类变量调用有什么区别?
- r - 检测具有不同组长度的分组 data.frame 列中的异常值
- php - 在 kubernetes 中使用 magento 1 清漆管理套接字超时
- php - 从 PHP 调用 Ajax