如何根据闪亮的用户输入从同一分布生成两个图?

How to produce two plots from the same distribution based on user input in shiny?

我正在尝试根据用户提供的数据生成的同一分布生成两个图 - 可能会发生变化。我希望这两个图使用相同的分布,但我不知道如何使第一个分布对第二个 renderPlot 函数可见。显然,我不能只是重用代码并创建另一个分布,因为它不会是相同的数据。

ui.R

library(shiny)

shinyUI(fluidPage(
  headerPanel(title = "Test"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("input.a", "A", min = 0, max = 100, value = 50),
      sliderInput("input.b", "B", min = 0, max = 100, value = 50),
      sliderInput("input.c", "C", min = 0, max = 100, value = 50)
    ),
    
    mainPanel(
      
      tabsetPanel( type = "tabs", #Open panel
                   tabPanel("Distributions 1",plotOutput("hist1.plot"))
                   ),
      tabsetPanel( type = "tabs", #Open panel
                   tabPanel("Distributions 2",plotOutput("hist2.plot"))
      )
                
) # close mainPanel
) # close sidebarLayout
) # close fluidPage
) # close shinyUI

server.R

library(dplyr)
library(tidyr)
library(plyr)
library(ggplot2)

shinyServer(function(input,output){ # open shiny server
  
  output$hist1.plot = renderPlot({
  
     # open renderPlot
      
    a = runif(1000,1,(input$input.a))
    b = runif(1000,1,(input$input.b))
    c = runif(1000,1,(input$input.c))

    amount = c(a,b,c)

    cat = c(rep("a",1000), rep("b",1000), rep("c",1000))

    hist.data = data.frame(amount,cat)
    names(hist.data) = c("amount","cat")

    hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
    pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount),
                      plus.stdev = mean(amount) + sd(amount))
    pricedata = pricedata[order(pricedata$avg),]


    ggplot(hist.data, aes(x=amount, fill = cat))+
      geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
      theme_test()+
      geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+


      geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


      geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


      facet_grid(cat ~., scales = "free")+
      scale_y_continuous(expand = c(0,0),name = "Count")+
      scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))

  }) #close renderPlot
  
    
    output$hist2.plot = renderPlot({ # open renderPlot
      
      a = runif(1000,1,(input$input.a))
      b = runif(1000,1,(input$input.b))
      c = runif(1000,1,(input$input.c))
      
      amount = c(a,b,c)
      
      cat = c(rep("a",1000), rep("b",1000), rep("c",1000))
      
      hist.data = data.frame(amount,cat)
      names(hist.data) = c("amount","cat")
      
      hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
      pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount),
                        plus.stdev = mean(amount) + sd(amount))
      pricedata = pricedata[order(pricedata$avg),]
      
      
      ggplot(hist.data, aes(x=amount, fill = cat))+
        geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
        theme_test()+
        geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+
        
        
        geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
        
        
        geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
        
        
        facet_grid(cat ~., scales = "free")+
        scale_y_continuous(expand = c(0,0),name = "Count")+
        scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))
      
    }) #close renderPlot
 
}) # close shinyServer

使用 reactiveValues() 从输入创建分布,然后在观察者内部创建分布。这样,两个图都可以使用相同的分布。

server.R

library(dplyr)
library(tidyr)
library(plyr)
library(ggplot2)

shinyServer(function(input,output){ # open shiny server
  
  vals <- reactiveValues()
  
  observe({vals$a = runif(1000,1,(input$input.a))
           vals$b = runif(1000,1,(input$input.b))
           vals$c = runif(1000,1,(input$input.c))
           })
    
  
  output$hist1.plot = renderPlot({
    
    # open renderPlot

    amount = c(vals$a, vals$b, vals$c)
    
    cat = c(rep("a",1000), rep("b",1000), rep("c",1000))
    
    hist.data = data.frame(amount,cat)
    names(hist.data) = c("amount","cat")
    
    hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
    pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount),
                      plus.stdev = mean(amount) + sd(amount))
    pricedata = pricedata[order(pricedata$avg),]
    
    ggplot(hist.data, aes(x=amount, fill = cat))+
      geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
      theme_test()+
      geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+
      
      
      geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
      
      
      geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
      
      
      facet_grid(cat ~., scales = "free")+
      scale_y_continuous(expand = c(0,0),name = "Count")+
      scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))
    
  }) #close renderPlot
  
  
  output$hist2.plot = renderPlot({ # open renderPlot
    
    a = runif(1000,1,(input$input.a))
    b = runif(1000,1,(input$input.b))
    c = runif(1000,1,(input$input.c))
    
    amount = c(vals$a, vals$b, vals$c)
    
    cat = c(rep("a",1000), rep("b",1000), rep("c",1000))
    
    hist.data = data.frame(amount,cat)
    names(hist.data) = c("amount","cat")
    
    hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
    pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount),
                      plus.stdev = mean(amount) + sd(amount))
    pricedata = pricedata[order(pricedata$avg),]
    
    
    ggplot(hist.data, aes(x=amount, fill = cat))+
      geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
      theme_test()+
      geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+
      
      
      geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
      
      
      geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
      
      
      facet_grid(cat ~., scales = "free")+
      scale_y_continuous(expand = c(0,0),name = "Count")+
      scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))
    
  }) #close renderPlot
  
}) # close shinyServer