2017-03-22 42 views
0

我写了一个Shiny应用程序,我从泊松分布中抽样。每次我想输出一个绘图或数据摘要时,我都重写了该函数。我宁愿只使用一次函数,然后将所有图表和摘要引用到该函数被调用的单次时间,否则结果在输出中将不相同。当我试图调用函数一次并将结果存储为ggplot使用的变量时,我得到了ggplot无法使用反应数据的错误,但我无法将它强制转换为数据框。如何避免在Shiny应用程序中多次重复相同的过程

我试过的变化:

mydata <- reactive({ 
    x <- input$x 
    y <- input$y 
    z <- input$z 
    Muts <- as.data.frame(rpois(100,(x*y*z))) 
    Muts 
}) 

但是他们没有工作,因为ggplot不能使用输入。我认为我没有正确使用反应函数。任何帮助减少代码冗余,并确保绘图和总结都使用相同的单个底层数据非常感谢。先谢谢你。

我当前的代码:

server.R

library(shiny) 
library(ggplot2) 

# Define server logic required to draw a histogram 
function(input, output) { 




    output$distPlot <- renderPlot({ 

    x <- input$x 
    y <- input$y 
    z <- input$z 
    Muts <- as.data.frame(rpois(100,(x*y*z))) 


    # draw the density plot 

    ggplot(Muts, aes(Muts)) + geom_density() 

    }) 

    output$distPlot2 <-renderPlot({ 

    x <- input$x 
    y <- input$y 
    z <- input$z 
    Muts <- as.data.frame(rpois(100,(x*y*z))) 



    Muts <- as.data.frame(Muts) 

    ggplot(Muts, aes(Muts)) + geom_histogram() 

    }) 

    output$summary <- renderPrint({ 
    x <- input$x 
    y <- input$y 
    z <- input$z 
    Muts <- as.data.frame(rpois(100,(x*y*z))) 

    summary(Muts) 
    }) 


} 
+0

您需要在您的'ggplot'调用中调用反应表达式,因为这是包含数据的内容。 'ggplot(myData(),aes(...))+ ...' – SymbolixAU

+0

另请参阅[此答案](http://stackoverflow.com/a/40336002/5977215) – SymbolixAU

回答

1

你的想法是正确的,它的工作与下面的代码:

library(shiny) 
library(ggplot2) 

# Define UI for application that draws a histogram 
ui <- fluidPage(

    # Application title 
    titlePanel(""), 

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
     sidebarPanel(
     sliderInput("x", 
        "Number of bins:", 
        min = 1, 
        max = 50, 
        value = 30), 
     sliderInput("y", 
        "Number of bins:", 
        min = 1, 
        max = 50, 
        value = 30), 
     sliderInput("z", 
        "Number of bins:", 
        min = 1, 
        max = 50, 
        value = 30) 
    ), 

     # Show a plot of the generated distribution 
     mainPanel(
     plotOutput("distPlot"), 
     plotOutput("distPlot2"), 
     verbatimTextOutput("summary") 
    ) 
    ) 
) 

# Define slibrary(shiny) 


# Define server logic required to draw a histogram 
server <- function(input, output) { 

    mydata <- reactive({ 
    x <- input$x 
    y <- input$y 
    z <- input$z 
    Muts <- as.data.frame(rpois(100,(x*y*z))) 
    Muts 
    }) 


    output$distPlot <- renderPlot({ 
    Muts <- mydata() 
    ggplot(Muts, aes(Muts)) + geom_density() 
    }) 

    output$distPlot2 <-renderPlot({ 
    Muts <- mydata() 
    ggplot(Muts, aes(Muts)) + geom_histogram() 
    }) 

    output$summary <- renderPrint({ 
    Muts <- mydata() 
    summary(Muts) 
    }) 


} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

谢谢你,这工作得很好。我一直在使用Rstudio GUI运行应用程序,是'shinyApp(ui = ui,server = server)'部分,用于从控制台启动它? – user964689

+0

你可以在app.R的末尾写上这个,然后RStudio会用“Run App”代替“Run/Source”,因为它知道你正在制作一个闪亮的应用程序。如果你没有将它添加到你的app.R中,那么你可以通过'shinyApp(ui,server)'来使用控制台来启动应用程序。 – shosaco

1

只要确保调用mydata()实际得到的您反应对象的价值。

library(shiny) 
library(ggplot2) 


ui <- fluidPage(
numericInput("x", "x", 2), numericInput("y", "y", 2), numericInput("z", "z", 2), 
plotOutput("distPlot"), plotOutput("distPlot2")) 

server <- function(input, output) { 

    mydata <- reactive({ 
    x <- input$x 
    y <- input$y 
    z <- input$z 
    data.frame(x=rpois(100,(x*y*z))) 
    }) 


    output$distPlot <- renderPlot({ 

    ggplot(mydata(), aes(x)) + geom_density() 

    }) 

    output$distPlot2 <-renderPlot({ 

    ggplot(mydata(), aes(x)) + geom_density() 

    }) 
} 

runApp(list(ui=ui, server=server)) 
相关问题