首页 > 解决方案 > 如何下载在 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)

对此的任何帮助都会很棒!

标签: rshiny

解决方案


推荐阅读