---
title: "Correlation"
output: html_document
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(corrplot)
library(Hmisc)
library(magrittr)
library(kableExtra)
```
## RESULT
CORRELATION ANALYSIS 

```{r, echo = FALSE}
csvfile = reactive({
  csvfile = input$file1
  if (is.null(csvfile)) {
    return(NULL)
  }
  dt = read.csv(csvfile$datapath, header = input$header, sep = ",", check.names = FALSE)
  dt
})

# output
if (input$req1 == "correlation") {
  if (input$submit > 0) {
    a = as.vector(csvfile()[, input$dvar])
    y = as.vector(csvfile()[, input$ivar])
    x = cor.test(a, y,
      method = input$req, conf.level = as.numeric(input$ci),
      alternative = input$alt, exact = FALSE
    )
    t_value = round(x$statistic, 3)
    correlation = round(x$estimate, 3)
    df = x$parameter
    pvalue = round(x$p.value, 3)
    alt.Hypothesis = x$alternative
    result = cbind(correlation, t_value, df, pvalue, alt.Hypothesis)
    nam = x$method
    rownames(result) = nam
    result = as.data.frame(result)
    kable(result, caption = "Correlation Analysis", row.names = FALSE) %>%
      kable_styling() %>%
      kable_paper("hover", full_width = F)
  }
}

if (input$req1 == "correlation") {
  if (input$submit > 0) {
    if (input$req == "pearson") {
      a = as.vector(csvfile()[, input$dvar])
      y = as.vector(csvfile()[, input$ivar])
      x = cor.test(a, y,
        method = input$req, conf.level = as.numeric(input$ci),
        alternative = input$alt, exact = FALSE
      )
      ci = x$conf.int
      ci_nw = melt(ci, value.name = "Lower Limit and Upper limit")
      kable(round(ci_nw, 3), caption = "Confidence Interval", row.names = FALSE) %>%
        kable_styling() %>%
        kable_paper("hover", full_width = F)
    }
  }
}



if (input$req1 == "corrmat") {
  if (input$submit2 > 0) {
    x = as.data.frame(csvfile()[, input$selvar])
    cormat = rcorr(as.matrix(x), type = input$req)
    R = round(cormat$r, 3)
    p = cormat$P
    ## Define notions for significance levels; spacing is important.
    mystars = ifelse(p < .001, "*** ", ifelse(p < .01, "**  ", ifelse(p < .05, "*   ", "    ")))
    Rnew = matrix(paste(R, mystars, sep = ""), ncol = ncol(x))
    diag(Rnew) = paste(diag(R), " ", sep = "")
    row.names(Rnew) = names(x)
    colnames(Rnew) = names(x)
    kable(Rnew, caption = "Correlation Matrix") %>%
      kable_styling() %>%
      kable_paper("hover", full_width = F)
  }
}
tags$br()
if (input$req1 == "corrmat") {
  if (input$submit2 > 0) {
    cat("*** Correlation is significant at 0.001 level (two tailed) \n** Correlation is significant at 0.01 level (two tailed)\n* Correlation is significant at 0.05 level (two tailed)")
  }
}
tags$br()

if (input$req1 == "corrmat") {
  if (input$submit2 > 0) {
    x = as.data.frame(csvfile()[, input$selvar])
    cormat = rcorr(as.matrix(x), type = input$req)
    correlmat1 = cormat$P
    row.names(correlmat1) = names(x)
    kable(round(correlmat1, 3), caption = "Matrix of P-values") %>%
      kable_styling() %>%
      kable_paper("hover", full_width = F)
  }
}



h3("")



```








**package: grapesAgri1, Version; 1.0.0**



