0

My first stackoverflow post...

I am relatively new to Shiny and running into some problems I just can't figure out.

I am writing GUI to show how various distributions change based on the entered parameters. Binomial, Poisson, and exponential all work as desired. I want to show a plot of the distribution along with a table that summarizes the results of the entered parameters.

The problem is with the normal distribution, which I have excerpted below and which should work (sorry for the extra required package, but it is very cool). The one-tail graph and reactive table work as intended.

When I select Two-tail graphs, I run into problems.

For the area inside it draws the graph, but it draws it down on the panel like the top part of the main panel is reserved for something that is not there.

Question 1: Why are the two-tail graphs plotting down the panel and not at the top?

In addition, the two-tail inside graph is missing its reactive table, whereas the same code (as far as I can tell) produces a table for the two-tail outside graph. I figure this is a "I am too close to it to see it" kind of thing.

Question 2: Why is the two-tail inside graph missing its reactive table?

Thanks for any help you can provide!

library(shiny)
library(lolcat)

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

    # Application title
               tabPanel(title = "Normal",
                        sidebarLayout(
                            sidebarPanel(
                                radioButtons(inputId = "tails",
                                             label = "One or Two Tails?",
                                             choices = c("One-Tail"=1,"Two-Tails"=2)),
                                conditionalPanel(condition = "input.tails==1",
                                                 numericInput(inputId = "tail",
                                                              label = "Point of Interest",
                                                              value = 9,
                                                              width = "100px")
                                ),#end conditional
                                conditionalPanel(condition = "input.tails==2",
                                                 radioButtons(inputId = "in.or.out",
                                                              label = "Area between or outside of points?",
                                                              choices = c("Inside"=1,"Outside"=2))
                                ),#end conditional
                        ),#end Normal tabpanel
                            #Show normal outputs here
                            mainPanel(
                                conditionalPanel(condition = "input.tails"==1,
                                                 plotOutput(outputId = "norm.plot"),
                                                 dataTableOutput(outputId = "norm.table")
                                ),#end conditional panel one tail
                                conditionalPanel(condition = "input.tails"==2,
                                                 plotOutput(outputId = "norm2.plot"),
                                                 dataTableOutput(outputId = "norm2.table")
                                )#end conditional panel two tail
                            )#end main panel
                        )),#end Normal panel
    )#end distributions drop down

# Define server logic required to draw a histogram
server <- function(input, output) {
    ####################################################
    #Normal
    #reactive data and R functions to create a normal table
    #One Tail
    plot.norm<- reactive({
        X1<- 9#Point of interest
        mu<-10
        sigma<-1
        tails<-input$tails
        R<-4

        if (tails==1){    
            
            #Graph of normal distribution with mu=mu, std=sigma, and lower tail up to X1
            x.or.lower<-pnorm(q=X1, mean=mu, sd =sigma, lower.tail = T)#X or lower
            x.or.higher<-pnorm(q=X1, mean=mu, sd =sigma, lower.tail = F)#X or higher
            x=seq(mu-6*sigma,mu+6*sigma,length=200)
            min.x=min(x)
            max.x=max(x)
            y=dnorm(x,mean=mu,sd=sigma)
            plot(x,y,
                 type="l",
                 )
            #Shade the lower tail area
            x=seq(min.x,X1,length=100)
            y=dnorm(x,mean=mu,sd=sigma)
        }#end if tails
    })#end one-tail plot
    
    #reactive data for numeric results one-tail
    data.norm <- reactive({
        X1<- 9 #Point of interest
        mu<-10
        sigma<-1
        R<-4
        tails<-input$tails
        
        if (tails==1){    
            #build the table with areas above and below X1
            x.or.lower<-pnorm(q=X1, mean=mu, sd =sigma, lower.tail = T)#X or lower
            x.or.higher<-pnorm(q=X1, mean=mu, sd =sigma, lower.tail = F)#X or higher
            #create the table
            data.labels <- c("\U00B5 = ","\U03C3 = ","X = ","Area above X","Area below X")
            results <- c(mu,sigma,X1,x.or.higher,x.or.lower)
            df.onetail <- data.frame(data.labels,results)
            df.onetail
        }#end if
    })#end reactive results table
    
    
    ####################end one-tail
    
    #Two-Tail
    plot.norm2<- reactive({
        X1<- 9 #First point of interest
        X2<-11 #second point of interest
        mu<-10
        sigma<-1
        tails<-input$tails
        in.or.out<-input$in.or.out

        
        if (tails==2){
            #Draw normal curve
            x=seq(mu-6*sigma,mu+6*sigma,length=200)
            min.x=min(x)
            max.x=max(x)
            y=dnorm(x,mean=mu,sd=sigma)
            plot(x,y
                 ,type="l"
                 ,col=col.plot.line)
            
            #Shade middle area
            if(in.or.out==1){
                between.x1.x2<-pnorm(q=max(X1,X2), mean=mu, sd =sigma, lower.tail = T)-pnorm(q=min(X1,X2), mean=mu, sd=sigma, lower.tail = T)#Between X and X2
                x=seq(min(X1,X2),max(X1,X2),length=100)
                y=dnorm(x,mean=mu,sd=sigma)
                polygon(c(min(X1,X2),x,max(X1,X2)),c(0,y,0),col="red")
            }#end if
            
            #Area outside of X
            if(in.or.out==2){
                outside.x1.x2<-ro(1-(pnorm(q=max(X1,X2), mean=mu, sd =sigma, lower.tail = T)-pnorm(q=min(X1,X2), mean=mu, sd=sigma, lower.tail = T)),R)#Outside of X and X2
                #Redraw normal curve
                x=seq(mu-6*sigma,mu+6*sigma,length=200)
                min.x=min(x)
                max.x=max(x)
                y=dnorm(x,mean=mu,sd=sigma)
                plot(x,y
                     ,type="l"
                     )
                
                # Shade the lower tail area
                x=seq(min.x,min(X1,X2),length=100)
                y=dnorm(x,mean=mu,sd=sigma)
                polygon(c(min.x,x,min(X1,X2)),c(0,y,0),col="red")
                
                # Shade the upper tail area
                x=seq(max(X1,X2),max.x,length=100)
                y=dnorm(x,mean=mu,sd=sigma)
                polygon(c(max(X1,X2),x,max.x),c(0,y,0),col="red")
                
            }#end if
            
        }#end if for two tails
        
    })#end two-tail plot
    
    #reactive data for numeric results two-tail
    data.norm2 <- reactive({
        X1<- 9 #Point of interest
        X2<-11 #second point of interest
        mu<-10
        sigma<-1
        R<-5
        tails<-input$tails
        in.or.out<-input$in.or.out
        
        if(tails==2){
            #areas inside of points
            if(in.or.out==1){
                #build the table with areas inside of X1 and X2
                between.x1.x2<-pnorm(q=max(X1,X2), mean=mu, sd =sigma, lower.tail = T)-pnorm(q=min(X1,X2), mean=mu, sd=sigma, lower.tail = T)#Between X and X2
                #create the table
                data.labels <- c("\U00B5 = ","\U03C3 = ","Lower X = ","Upper X = ","Area between tails =")
                results <- c(mu,sigma,X1,X2,between.x1.x2)
                df.in <- data.frame(data.labels,results)
                ro(df.in,R)
            }#end inside if
            
            #area outside points
            if(in.or.out==2){
                outside.x1.x2<-ro(1-(pnorm(q=max(X1,X2), mean=mu, sd =sigma, lower.tail = T)-pnorm(q=min(X1,X2), mean=mu, sd=sigma, lower.tail = T)),R)#Outside of X and X2
                data.labels <- c("\U00B5 = ","\U03C3 = ","Lower X = ","Upper X = ","Area of tails =")
                results <- c(mu,sigma,X1,X2,outside.x1.x2)
                df.out <- data.frame(data.labels,results)
                ro(df.out,R)
            }#end outside if
        }#end tails if
    })#end reactive results two tail table

    #Render the one-tail normal plot
    output$norm.plot<-renderPlot({
        plot.norm()
    })
    #Render the results from the one-tail normal plot
    output$norm.table <- renderDataTable({
        data.norm()
    })
    
    #Render the the two-tail normal plot
    output$norm2.plot <- renderPlot({
        plot.norm2()
    })
    
    #Render the results from the two-tail normal plot
    output$norm2.table <- renderDataTable({
        data.norm2()
    })
    
    
}

# Run the application 
shinyApp(ui = ui, server = server)
Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    welcome to SO! This is a lot of code... To help others help you, please try to have only one question per post, and to reduce the question to a [minimal reproducible example](https://stackoverflow.com/a/5963610/13513328) – Waldi May 12 '21 at 16:37
  • Look at the values of the `condition` arguments to the calls to `conditionalPanel`s in your `mainPanel` and compare them to the corresponding arguments in the `sidebarPanel` and the problem should be obvious. Taking the time to construct a minimum reproducible example, as @Waldi suggests, would have revealed what was wrong. – Limey May 12 '21 at 17:27
  • OK, thank you for the guidance. I have further reduced the code down to what I think is the minimum to allow the selections that are part of the issue. – Steven Ouellette May 12 '21 at 17:27
  • Aha, Limey that is just what I needed. Thank you for the response! – Steven Ouellette May 12 '21 at 17:34

0 Answers0