th_prop <- function(p_nul,alternatieve,alpha,n_stkp,datatype,mijndatakolom,prop_stkp,toon) {
n <- n_stkp
if(alternatieve %in% 1:3) {   
  if(alpha >=0 & alpha <=1) {
    if(all(n == as.integer(n)) & n >=2) { 
      if(p_nul*n >= 15 & (1-p_nul)*n >= 15) { 
        if(toon %in% 0:3) {      
          if(datatype %in% 0:2) {        
            library(ggplot2) 
            library(readxl)
            library(BSDA) 
            alpha1 <- round(alpha,3)
            p_nul1 <- round(p_nul,4)
            q_nul1 <- round(1-p_nul,4)
            ylbl <- bquote("dichtheidsfunctie  Z\u007e N (0,1)" )
            p <- ggplot(data.frame(x = c(-4.3, 4.3)), aes(x)) +
              coord_cartesian(expand = FALSE, ylim = c(0, 0.47)) + 
              labs( x = "z", y = ylbl) + 
              annotate("text", x = 0, y = 0.43, label = bquote("teststatistiek " ~ Z==frac(hat(P)- p[0] , sqrt(p[0]*q[0])/sqrt(n)) ~ "  \u007e  N (0,1)  als " ~ H[0] ~ "waar is" ), size = 4.5 )  +
              annotate("text", x = -2.6, y = 0.30, label = bquote(H[0]: p == .(p_nul1))) +
              annotate("text", x = -2.6, y = 0.23, label = bquote(n== .(n) ) ) + 
              annotate("text", x = -2.3, y = 0.36, label = bquote(frac(hat(P)- .(p_nul1) , sqrt(.(p_nul1) ~""~  .(q_nul1))/sqrt(.(n)) ) ~ " \u007e N (0,1)" )) + 
              annotate("text", x = 2.7, y = 0.21, label = bquote("signif.niveau" ~alpha== .( alpha1) ), color = "blue"  ) +
              annotate("text", x = 2.7, y = 0.35, label = "GROEN =", color = "darkgreen") + 
              annotate("text", x = 2.7, y = 0.335, label = "aanvaardingsgebied", color = "darkgreen") +      
              annotate("text", x = 2.7, y = 0.3, label = "ROOD =", color = "red") + 
              annotate("text", x = 2.7, y = 0.285, label= "verwerpingsgebied",color ="red")+ 
              theme(aspect.ratio = 1 ,
                    axis.title.x = element_text(hjust = 0.5),
                    axis.text.x= element_text( colour="black",size = 10))
            if (alternatieve == 1){altern <- "greater"
            lstaartkans <- 1 - alpha   
            H1lbl <- bquote(H[1]: p > .(p_nul1)) 
            b <- qnorm(lstaartkans)  
            b1 <- min(b , 4.3)
            ptn <- c(b1, 4.3, b1, -4.3)
            }else if (alternatieve == 2){altern <- "less"
            lstaartkans <- alpha 
            H1lbl <- bquote(H[1]: p < .(p_nul1)) 
            b <- qnorm(lstaartkans) 
            b1 <- max(b , -4.3)
            ptn <- c(b1, -4.3, b1, 4.3)
            }else if (alternatieve == 3){altern <- "two.sided"
            lstaartkans <- alpha / 2
            H1lbl <- bquote(H[1]: p ~ " \u2260 " ~ .(p_nul1)) 
            b <- qnorm(lstaartkans) 
            b1 <- max(b , -4.3)
            ptn <- c(b1, -4.3, b1, -b1, -b1, 4.3) }
            tkx <- c(0 , b)
            tkxlbl <- c("0", as.character(round(b,3)))
            p <- p + 
              annotate("text", x = -2.6, y = 0.265, label = H1lbl) +
              annotate("text", x = 2.8, y = 0.17, label = paste("kritisch punt ", as.character(round(b,3)) ), size = 4 ) +
              geom_segment(aes(x=ptn[1], y=0, xend=ptn[2], yend=0),color = "red", linewidth = 2.2) +  
              geom_segment(aes(x=ptn[3], y=0, xend=ptn[4], yend=0),color= "darkgreen", linewidth= 2.2) +
              stat_function(fun = dnorm, geom = "area", xlim = c(ptn[1], ptn[2]), fill = "blue",alpha = 0.3)    
            if (alternatieve == 3){
              tkx <- c(b, 0 , -b)
              tkxlbl <- c(as.character(round(b,3)), "0", as.character(round(-b,3)))   
              p <- p + 
                annotate("text", x = 3.4, y = 0.15, label = paste(" en ", as.character(round(-b,3)) ) , size= 4 )   +
                geom_segment(aes(x=ptn[5], y=0, xend=ptn[6], yend=0),color = "red", linewidth = 2.2) +
                stat_function(fun = dnorm, geom = "area", xlim = c(ptn[5], ptn[6]), fill = "blue",alpha = 0.3) }
            if (toon == 0 | datatype == 0){
              p <- p +
                stat_function(fun = dnorm, linewidth = 0.6) +
                scale_x_continuous(breaks =  tkx, label = tkxlbl)  
              p   
            }else{
              conflvl1 <- round(1-alpha , 3)
              if (datatype == 1) {
                skip <- 1    
                if(mijndatakolom %in% LETTERS[1:26]) {    
                  mdat <- read_excel("xcl/mijndata.xlsx", range = cell_cols(mijndatakolom))
                  if(!length(mdat) == 0  ) {
                    x <- unlist(as.vector(mdat[,1]), use.names = FALSE)
                    if (all(x %in% c(0, 1))) {
                      if(!( any(is.na(x)) | !is.numeric(x) | length(x) != n_stkp  )) {
                        skip <- 0
                        z_test <- z.test(x, mu = p_nul, sigma.x=sqrt(p_nul*(1-p_nul)), alternative = altern, conf.level = 1 - alpha)
                        phoed1 <- round(as.numeric(z_test$estimate), 4)
                        datalbl <- bquote(atop(NA, atop(textstyle("dataset"), textstyle("\"" ~ .(mijndatakolom) ~ "\""))))
                        sigm1 <- round(sqrt(p_nul*(1-p_nul)), 4)
                        ztestlbl <- paste0("z.test(x, mu = ", as.character(p_nul1), ", sigma.x = ", as.character(sigm1), ", alternative = ",  altern, ", conf.level = " , as.character(conflvl1) , ") \n" )
                      }else{cat(paste0("het aantal data in kolom \"", mijndatakolom, "\" verschilt van \nde opgegeven steekproefgrootte in n_stkp \nof \nde data in kolom \"", mijndatakolom, "\" zijn niet goed ingebracht \n \nPas aan. \n \nextra info vind je in de handleiding: \n\"Data bij toetsen en bij betrouwbaarheidsintervallen: van Excel naar R\"" ) ) }
                    }else{ cat(paste0("kolom \"", mijndatakolom, "\" mag alleen nullen en enen bevatten \nPas aan.") )}
                  }else{cat(paste0("kolom \"", mijndatakolom, "\" in mijndata.xlsx is leeg \nPas aan.")) } 
                }else{cat("in mijndatakolom <- ...  tik je een \nHOOFDLETTER TUSSEN AANHALINGSTEKENS \nLetters tussen A en Z. \nPas aan.") }
              }else if (datatype == 2) {skip <- 0 
              phoed1 <- round(prop_stkp, 4)
              sigm1 <- round(sqrt(p_nul*(1-p_nul)), 4)
              z_test <- zsum.test(mean.x = prop_stkp, sigma.x = sqrt(p_nul*(1-p_nul)), n.x = n, mu = p_nul, alternative = altern, conf.level = 1 - alpha)
              datalbl <- bquote(atop(NA, atop(textstyle("data"), textstyle("samengevat"))))
              ztestlbl <- paste0("zsum.test(mean.x = ", as.character(phoed1), ", sigma.x = ", as.character(sigm1), ", n.x = ", as.character(n),", \n          mu = ", as.character(p_nul1),", alternative = ",  altern, ", conf.level = " , as.character(conflvl1) , ") \n" )
              }
              if (skip == 0){options(scipen = 999)
                z_waarde1 <- round(as.numeric(z_test$statistic), 3)
                p_waarde1 <- round(as.numeric(z_test$p.value), 4)  
                zlbl <- bquote(atop(NA, atop(textstyle("z-waarde"), textstyle(.(z_waarde1)))))
                plbl <- bquote(atop(NA, atop(textstyle("p-waarde"), textstyle(.(p_waarde1)))))
                prop_stkplbl <- bquote(hat(p)~ "=" ~ .(phoed1))   
                p <- p +
                  stat_function(fun = dnorm, linewidth = 0.6) +
                  annotate("text", x = 0, y = 0.275, color= "brown4",label = datalbl) +
                  annotate("text", x = 0, y = 0.218, color= "blue",label = plbl) +  
                  annotate("text", x = 0, y = 0.17, label = zlbl) +  
                  annotate("text", x = -3, y = 0.19, color= "brown4", label = prop_stkplbl) +
                  geom_segment(x=-0, xend=z_waarde1, y=0.143,yend=0, linetype =2,color = "grey25", arrow = arrow(angle = 14,  type = "closed", length=unit(.42, 'cm')),linewidth =0.6) +
                  scale_x_continuous(breaks =  tkx, label = tkxlbl)
                if(toon == 3){cat (ztestlbl)
                  print (z_test)
                  print (p)
                }else if(toon ==2) {cat (ztestlbl)
                  print (z_test)
                }else if(toon ==1) {print (p)}  } 
            }  
          }else{cat("Voor datatype <-... mag je alleen maar \nhet cijfer 0 of 1 of 2 invullen. Pas aan.") }
        }else{cat("Voor toon <-... mag je alleen maar \nhet cijfer 0 of 1 of 2 of 3 invullen. Pas aan.") }
      }else{cat("Vuistregel voor de normale benadering is niet voldaan \n(n_stkp)x(p_nul) ≥ 15 en (n_stkp)x(1-p_nul) ≥ 15 \nPas aan.") }       
    }else{cat("Tik een geheel getal groter dan 1 voor \nde steekproefgrootte n_stkp. Pas aan.") }
  }else{cat("Het significantieniveau is een kans \ndus 0 ≤ alpha ≤ 1. Pas aan.") }
}else{cat("Voor de alternatieve mag je alleen maar \nhet cijfer 1 of 2 of 3 invullen. Pas aan.")  }
} 