簡體   English   中英

R閃閃發光的反應性ggplots

[英]R Shiny saving reactive ggplots

我試圖弄清楚如何在我的R Shiny項目中保存反應性ggplots。 我遵循了指南以及R Shiny網站上的指南。 但是,我認為由於使用反應式繪圖,可能會遇到問題。

這是我到目前為止的代碼。

ui <- fluidPage(

    dashboardBody(
      fluidRow(uiOutput('topbox')),
      fluidRow(
        tabBox(
          id = 'tabset1',
          width = '100%',
          tabPanel('Grades Graph', downloadButton('Download'), plotOutput('individualGraph')),
        )
      )
    )
  )

server <- function(input, output, session) {

  grades <- reactive({
    req(input$file)
    inFile <- input$file
    if (endsWith(inFile$name, '.xlsx')){
      gradesTbl <- read_excel(inFile$datapath)
      gradesTbl <- gradesTbl %>% 
        arrange(Period, Student, Date, Type) %>% 
        mutate(Date = as.Date(Date))
      return(gradesTbl)
    } else if (endsWith(inFile$name, 'csv')){
      gradesTbl <- read_csv(inFile$datapath)
      gradesTbl <- gradesTbl %>% 
        arrange(Period, Student, Date, Type) %>% 
        mutate(Date = mdy(Date))
      return(gradesTbl)
    }
  })

  output$Download <- downloadHandler(
   filename = function(){
     paste('test', '.png', sep = '')
   },
   content = function(file){
     ggsave(file, plot = output$individualGraph, device = 'png')
   }
  )

  indivdf <- function(){
    data.frame(grades()) %>%
      filter((Student == input$studentVar) & (Period == input$periodVar) & (Type %in% input$typeVar) & (Unit %in% input$unitVar))
  }

  output$individualGraph <- renderPlot({
    req(input$periodVar)
    indivdf() %>%
      ggplot(aes(x = Date, y = Grade,
                 color = Type, shape = Unit)) +
      geom_point(size = 4) +
      ggtitle(paste(input$studentVar, "'s Individual Grades", sep = '')) +
      plotTheme() +
      scale_shape_manual(values = 1:10) +
      facet_wrap(Unit~.) +
      scale_color_manual(values = c('#E51A1D', '#377DB9', '#4EAE4A'))
  })

shinyApp(ui = ui, server = server)

完整的代碼在這里 ,但是我認為這是顯示我正在嘗試執行的所有操作。 我只是不知道如何保存這些反應性的表和圖。 我覺得這與使用'plot = output $ indididualGraph'有關,但是我真的不知道。

你需要采取的代碼生成的情節和其從移動renderPlotreactive 然后,您可以從renderPlot內部調用相同的reactive renderPlot以在UI中顯示圖形,也可以從downloadHandler調用以下載圖形。

請參見下面的示例。 使用相同的變量名作為反應式( individualGraph() )和輸出返回( output$individualGraph )可能不是最佳編碼實踐,但我發現它更方便。

server <- function(input, output, session) {

    individualGraph <- reactive({
        req(input$periodVar)
        indivdf() %>%
            ggplot(aes(x = Date, y = Grade,
                       color = Type, shape = Unit)) +
            geom_point(size = 4) +
            ggtitle(paste(input$studentVar, "'s Individual Grades", sep = '')) +
            plotTheme() +
            scale_shape_manual(values = 1:10) +
            facet_wrap(Unit~.) +
            scale_color_manual(values = c('#E51A1D', '#377DB9', '#4EAE4A'))
    })

    output$individualGraph <- renderPlot({
        req(individualGraph())
        individualGraph()
    })

    output$Download <- downloadHandler(
        filename = function(){
            paste('test', '.png', sep = '')
        },
        content = function(file){
            req(individualGraph())
            ggsave(file, plot = individualGraph(), device = 'png')
        }
    )

    grades <- reactive({
        req(input$file)
        inFile <- input$file
        if (endsWith(inFile$name, '.xlsx')){
            gradesTbl <- read_excel(inFile$datapath)
            gradesTbl <- gradesTbl %>% 
                arrange(Period, Student, Date, Type) %>% 
                mutate(Date = as.Date(Date))
            return(gradesTbl)
        } else if (endsWith(inFile$name, 'csv')){
            gradesTbl <- read_csv(inFile$datapath)
            gradesTbl <- gradesTbl %>% 
                arrange(Period, Student, Date, Type) %>% 
                mutate(Date = mdy(Date))
            return(gradesTbl)
        }
    })

    indivdf <- function(){
        data.frame(grades()) %>%
            filter((Student == input$studentVar) & (Period == input$periodVar) & (Type %in% input$typeVar) & (Unit %in% input$unitVar))
    }
}  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM