簡體   English   中英

R Shiny應用程序ggplot2超時

[英]R Shiny app ggplot2 times out

我正在構建一個應用程序,以更好地了解對數正態分布和正態分布的差異。 該應用程序應使用ggplot2顯示模擬數據的直方圖(正常或對數正態),並向假數據擬合法線,對數正態密度和核密度。 由於某些原因,下面的應用程序不會顯示ggplot2圖形。

# Define UI for application that draws a histogram

library(shiny)
library(ggplot2)
library(stats)
library(gridExtra)


set.seed(15)

ui <- fluidPage(

   # Application title
   titlePanel("Curve fit with different distributions"),

   # Sidebar with a slider input for number of bins
   sidebarLayout(
      sidebarPanel(

         sliderInput("mean",
                     "Mean value:",
                     min = 1,
                     max = 250,
                     value = 10)
      ,
      sliderInput("spread",
                  "Standard deviation:",
                  min = 0,
                  max = 25,
                  step=0.1,
                  value = 2.5)
   ,
   sliderInput("n",
               "How many datapoints:",
               min = 10,
               max = 10000,
               value = 2500)
,
selectInput("dist",
"Which data distribution?" ,
list("Normal"="dnorm"  ,
                  "Lognormal"="dlnorm"
                  )
                  )),
      # Show a plot of the generated distribution
      mainPanel(

  plotOutput("distPlot",  height = "80%"))

  )
)

# Define server logic required to draw a histogram with normal and log normal density

server <- function(input, output) {

  sim_data<-reactive({
    if(is.null(input$dist) |is.null(input$spread) | is.null(input$mean)) { 
      return(NULL)
    }

    mlog<-log(input$mean )
    lspread <- log(input$spread)
     dat <-    data.frame(xn = rnorm(input$n, mean = input$mean, sd = input$spread), ln=rlnorm(input$n, meanlog =mlog , sdlog = lspread))
       return(dat)  
  })
  output$distPlot <- renderPlot({
    if(is.null(sim_data()) |is.null(input$dist) ){
      return(NULL)
    }
    # generate bins based on input$bins from ui.R


    if(input$dist == "dnorm"){
    hist_plot<- ggplot(sim_data(), aes(x = xn)) +
      geom_histogram(aes(y =..density..),

                     colour = "black",
                     fill = "white") +

      stat_function(fun = dnorm, colour ="#377EB8", args = list(mean = mymean, sd = mysd))+
      stat_function(fun = dlnorm, colour ="#E41A1C", args = list(mean =  mylmean, sd = mylsd))+
      geom_density(colour="black")+

      theme_minimal()
    } 
    else{ 
      hist_plot<- ggplot(sim_data(), aes(x = ln)) +
        geom_histogram(aes(y =..density..),

                       colour = "black",
                       fill = "white") +
        labs(title=distname) +
        theme_minimal()+
        stat_function(fun = dnorm, colour ="#377EB8", args = list(mean = mymean, sd = mysd))+
        stat_function(fun = dlnorm, colour ="#E41A1C", args = list(mean =  mylmean, sd = mylsd))+
        geom_density(colour="black")+
      theme_minimal() 
      }

    if(input$dist == "dnorm"){
        box_plot<- ggplot(sim_data(), aes(x="",y = xn)) +
          geom_boxplot()+      
          theme_minimal()
      }
        else{
          box_plot<- ggplot(sim_data(), aes(x="",y = ln)) +
            geom_boxplot(

            )+      
      theme_minimal()
        }


    p=grid.arrange(hist_plot+      
                     theme_minimal(),box_plot+      
                     theme_minimal(), ncol=1,nrow=2, heights = c(4,2))
    plot(p)


  })


   }
# Run the application 
shinyApp(ui = ui, server = server)

嗨,你的問題在這里

plotOutput("distPlot",  height = "80%")

目前,您告訴情節的高度為“無”的80%,即“無”。 例如,將高度更改為400px

Shiny注意到該情節不是視覺的,因此即使在用餐時也沒有計算該情節。

暫無
暫無

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

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