【问题标题】:Render multiple plots from from single dataset (shiny)从单个数据集中渲染多个图(闪亮)
【发布时间】:2020-07-25 05:02:27
【问题描述】:

我有一个闪亮的应用程序,我在其中上传数据集,然后操作数据集并从中构建多个图。现在我在每个 renderPlot 块中进行数据集操作,但在我看来应该有另一种方法。我应该将数据集操作放在哪里,以便我只能执行一次并传递到rendetPlot 块中?

我目前的情况示例:

library(shiny)
library(shinyTime)
library(plotly)
library(ggplot2)

ui <- fluidPage(
  navbarPage("STAR",
             tabPanel("PromptHits",
                      sidebarLayout(
                        sidebarPanel(
                          width = 2,
                          fileInput(inputId = "pHitsFile", label = "Upload Data File"),
                          actionButton("pHitsMake", "Analyze")
                        ),
                        mainPanel(
                          h3("Prompt Hits [all]"),
                          plotlyOutput("pHits1", width = 500, height = 500),
                          plotlyOutput("pHits2", width = 500, height = 500),
                          h3("Prompt Hits [outliers removed]"),
                          plotlyOutput("pHits3", width = 500, height = 500),
                          plotlyOutput("pHits4", width = 500, height = 500),
                          plotOutput("pHits5", width = 1000),
                          plotOutput("pHits6", width = 1000),
                          plotOutput("pHits7", width = 500, height = 500),
                          plotOutput("pHits8", width = 500, height = 500),
                          plotOutput("pHits9", width = 500, height = 500),
                          plotOutput("pHits10", width = 500, height = 500)
                        )
                      )
             )
  )
)

server <- function(input, output)
{
  options(shiny.maxRequestSize=30*1024^2)
  output$pHits1 <- renderPlotly(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqi
      }
    }
  )
  output$pHits2 <- renderPlotly(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqo
      }
    }
  )
  output$pHits3 <- renderPlotly(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqci
      }
    }
  )
  output$pHits4 <- renderPlotly(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqco
      }
    }
  )
  output$pHits5 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqci_padrow
      }
    }
  )
  output$pHits6 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqco_padrow
      }
    }
  )
  output$pHits7 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqci_agg
      }
    }
  )
  output$pHits8 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pqco_agg
      }
    }
  )
  output$pHits9 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pfitd0
      }
    }
  )
  output$pHits10 <- renderPlot(
    {
      if (input$pHitsMake) {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out$pfitd1
      }
    }
  )
}

shinyApp (ui = ui, server = server)

我不想做:

inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)

每次。提前致谢。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    看看eventReactive。这样,您可以为绘图数据创建一个反应式表达式,该表达式仅在单击 input$pHitsmake 时才会更新:

    library(shiny)
    library(shinyTime)
    library(plotly)
    library(ggplot2)
    
    ui <- fluidPage(
      navbarPage("STAR",
                 tabPanel("PromptHits",
                          sidebarLayout(
                            sidebarPanel(
                              width = 2,
                              fileInput(inputId = "pHitsFile", label = "Upload Data File"),
                              actionButton("pHitsMake", "Analyze")
                            ),
                            mainPanel(
                              h3("Prompt Hits [all]"),
                              plotlyOutput("pHits1", width = 500, height = 500),
                              plotlyOutput("pHits2", width = 500, height = 500),
                              h3("Prompt Hits [outliers removed]"),
                              plotlyOutput("pHits3", width = 500, height = 500),
                              plotlyOutput("pHits4", width = 500, height = 500),
                              plotOutput("pHits5", width = 1000),
                              plotOutput("pHits6", width = 1000),
                              plotOutput("pHits7", width = 500, height = 500),
                              plotOutput("pHits8", width = 500, height = 500),
                              plotOutput("pHits9", width = 500, height = 500),
                              plotOutput("pHits10", width = 500, height = 500)
                            )
                          )
                 )
      )
    )
    
    server <- function(input, output)
    {
      options(shiny.maxRequestSize=30*1024^2)
      
      plot_data <- eventReactive(input$pHitsMake, {
        inputData <- input$pHitsFile
        d <- read.csv(inputData$datapath)
        source("pHitsMaker.R")
        out <- pHits(d)
        out
      })
      output$pHits1 <- renderPlotly(
        {
          plot_data()$pqi
        }
      )
      output$pHits2 <- renderPlotly(
        {
          plot_data()$pqo
        }
      )
      output$pHits3 <- renderPlotly(
        {
          plot_data()$pqci
        }
      )
      output$pHits4 <- renderPlotly(
        {
          plot_data()$pqco
        }
      )
      output$pHits5 <- renderPlot(
        {
          plot_data()$pqci_padrow
          
        }
      )
      output$pHits6 <- renderPlot(
        {
          plot_data()$pqco_padrow
          
        }
      )
      output$pHits7 <- renderPlot(
        {
          plot_data()$pqci_agg
          
        }
      )
      output$pHits8 <- renderPlot(
        {
          plot_data()$pqco_agg
          
        }
      )
      output$pHits9 <- renderPlot(
        {
          plot_data()$pfitd0
          
        }
      )
      output$pHits10 <- renderPlot(
        {
          plot_data()$pfitd1
          
        }
      )
    }
    
    shinyApp (ui = ui, server = server)
    

    【讨论】:

    • 非常感谢。这正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2016-12-29
    • 2014-10-22
    • 2015-01-11
    • 2017-05-05
    • 2017-02-23
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多