Korrelationsmatrix in Shiny

Alles über shiny web applicationen und plumber APIs

Moderatoren: EDi, jogo

Antworten
retep
Beiträge: 99
Registriert: Do Sep 06, 2018 7:50 pm

Korrelationsmatrix in Shiny

Beitrag von retep »

Hallo,

nachdem mir beim letzten Shiny Problem excellent geholfen wurde, wollte ich nochmal um Unterstützung bitten. Meine Idee ist, dass ich in Shiny mittels des Package corrmat ein Anwendung schreiben wollte. in dem der Benutzer sich die Matrix selbst zusammen stellen kann. Soll heißen, der Benutzer kann sich verschiedene Elmente aus dem Datensatz heraus suchen und sich hierfür die Matrix anzeigen lassen.

Beim implementieren in Shiny wird kein Fehler angezeigt! Aber der Plot erscheint einfach nicht. Verflixt! Kann mir vllt. jemand helfen und mir aufzeigen wo der "Hase im Pfeffer" liegt?

Die zweite Frage, betrifft die Auswahl. Ich habe immer noch nicht verstanden was der Unterschied zwischen "eventReactive" und/oder "observe" ist. Wahrscheinlich muss ich das hier anwenden um zum gewünschten Ergebnis zzu kommen?

Hier erstmal mein Ansatz:

Code: Alles auswählen

library(shiny)
library(shinyWidgets)
library(tidyverse) 
library(plotly)

set.seed(1234)
A <-  tibble (
  Horizon = ("Horizon A"),
  Depth     = seq(0, 2.6, 0.2),
  Element_1 =  abs(round(rnorm(14), 2)),
  Element_2 =  abs(round(rnorm(14), 2)),
  Element_3 =  abs(round(rnorm(14), 2)),
  Element_4 =  abs(round(rnorm(14), 2)),
  Element_5 =  abs(round(rnorm(14), 2)),
  Element_6 =  abs(round(rnorm(14), 2)),
  Element_7 =  abs(round(rnorm(14), 2)),
  Element_8 =  abs(round(rnorm(14), 2)),
  Element_9 =  abs(round(rnorm(14), 2)),
  Element_10 =  abs(round(rnorm(14), 2))
)

B <-  tibble (
  Horizon = ("Horizon B"),
  Depth     = seq(2.8, 43.8, 0.33),
  Element_1 =  abs(round(rnorm(125), 2)),
  Element_2 =  abs(round(rnorm(125), 2)),
  Element_3 =  abs(round(rnorm(125), 2)),
  Element_4 =  abs(round(rnorm(125), 2)),
  Element_5 =  abs(round(rnorm(125), 2)),
  Element_6 =  abs(round(rnorm(125), 2)),
  Element_7 =  abs(round(rnorm(125), 2)),
  Element_8 =  abs(round(rnorm(125), 2)),
  Element_9 =  abs(round(rnorm(125), 2)),
  Element_10 =  abs(round(rnorm(125), 2))
)

C <-  tibble (
  Horizon = ("Horizon C"),
  Depth     = seq(44, 50, 0.6),
  Element_1 =  abs(round(rnorm(11), 2)),
  Element_2 =  abs(round(rnorm(11), 2)),
  Element_3 =  abs(round(rnorm(11), 2)),
  Element_4 =  abs(round(rnorm(11), 2)),
  Element_5 =  abs(round(rnorm(11), 2)),
  Element_6 =  abs(round(rnorm(11), 2)),
  Element_7 =  abs(round(rnorm(11), 2)),
  Element_8 =  abs(round(rnorm(11), 2)),
  Element_9 =  abs(round(rnorm(11), 2)),
  Element_10 =  abs(round(rnorm(11), 2))
)

Analyse.corr = rbind (A, B, C)
Analyse.corrl = Analyse.corr%>%
  pivot_longer(
    cols = Element_1: Element_10,
    names_to = "Element",
    values_to = "Values")

# Scatter Plot
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # Korrelmatrix
        pickerInput(
        inputId = "corr",
        label = "Correlmatrix",
        choices = list(
          Main =  colnames(Analyse.corr) [c(3:6)],
          Minor = colnames(Analyse.corr) [c(7:10)]),
        multiple = TRUE,
        options = pickerOptions(
          actionsBox = TRUE,
          title = "Please select at least 4 item",
          header = "Main & Minor Elements")),
      
      br(),
      pickerInput(
        inputId = "lithcor",
        label = "Horizon",
        choices = unique(Analyse.corr$Horizon),
        selected = Analyse.corr$Horizon [1],
        multiple = TRUE,
        options =  list("max-options" = 1,
                        "max-options-text" = "No more!")),
      br(),
      selectInput("type", "Correlation Type", selected = NULL,
                  choices = c("parametric", "nonparametric"), multiple = FALSE), 
      
      br(),
      selectInput("p", "Choose a p-value", selected = NULL,
                  choices = c("0.01", "0.05"), multiple = FALSE), 
      
    ), 
    
    # end sidebar panel
    
    mainPanel(
      tabsetPanel(type = "tab",
                  tabPanel("Korrelmatrix", plotOutput("cormat"))
                  
      ))
    
  ))



# end main panel
server <- function(input, output, session) { 
  
  # correl matrix
  #filter
  reactivecorrel <- reactive({
    Analyse.corr%>%
      filter(Horizon %in% input$lithcor) %>%
      select(-Horizon, -Depth, input$corr) # entfernt Horizont & Tiefe für Matrix zum Schluss  
  })                                                        # bleiben nur noch numerische Werte übrig --> sollte eigentlich jetzt die Matrix sein (?)
  
  # correlogram
  
  output$cormat <- renderPlot({
    plot <- ggstatsplot::ggcorrmat(
      reactivecorrel (),
      type = input$type, # parametric for Pearson, nonparametric for Spearman's correlation
      sig.level = input$p,
      #output = "p", will return the p-value matrix
      hc.order = TRUE,
      matrix.type = "upper", # 	Character, "upper", # (default), "lower", or "full", display full matrix, lower triangular or upper triangular matrix.
      outline.col = "white",
      title = "Correl matrix",
      subtitle = "Subunit: ",
      colors = c("darkred", "white", "steelblue") # change default colors
      
    )
  
  })
  
}

shinyApp(ui, server)
Vielleicht hat jemand eine Idee? Würde mich sehr darüber freuen!

Vielen Dank für Eure Zeit & Hilfe,

retep
retep
Beiträge: 99
Registriert: Do Sep 06, 2018 7:50 pm

Re: Korrelationsmatrix in Shiny

Beitrag von retep »

Hallo,

ich habe nicht gemerkt, dass es ein eigenes Unterforum "R in Web" gibt. :D

Darf ich die Moderatoren bitten meinen Eintrag dorthin zu verschieben? Und ich bitte vielmals um Entschuldigung! ;)

retep
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: Korrelationsmatrix in Shiny

Beitrag von bigben »

Das Unterforum hatte EDi auf Deinen Vorschlag hin im November eingerichtet :lol: Ist verschoben. LG, Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
retep
Beiträge: 99
Registriert: Do Sep 06, 2018 7:50 pm

Re: Korrelationsmatrix in Shiny - Problem gelöst - Anleitung

Beitrag von retep »

Ich konnte das Problem lösen: Hier die Anleitung!

Code: Alles auswählen

library(shiny)
library(shinyWidgets)
library(tidyverse) 
library(plotly)

set.seed(1234)
A <-  tibble (
  Horizon = ("Horizon A"),
  Depth     = seq(0, 2.6, 0.2),
  Element_1 =  abs(round(rnorm(14), 2)),
  Element_2 =  abs(round(rnorm(14), 2)),
  Element_3 =  abs(round(rnorm(14), 2)),
  Element_4 =  abs(round(rnorm(14), 2)),
  Element_5 =  abs(round(rnorm(14), 2)),
  Element_6 =  abs(round(rnorm(14), 2)),
  Element_7 =  abs(round(rnorm(14), 2)),
  Element_8 =  abs(round(rnorm(14), 2)),
  Element_9 =  abs(round(rnorm(14), 2)),
  Element_10 =  abs(round(rnorm(14), 2))
)

B <-  tibble (
  Horizon = ("Horizon B"),
  Depth     = seq(2.8, 43.8, 0.33),
  Element_1 =  abs(round(rnorm(125), 2)),
  Element_2 =  abs(round(rnorm(125), 2)),
  Element_3 =  abs(round(rnorm(125), 2)),
  Element_4 =  abs(round(rnorm(125), 2)),
  Element_5 =  abs(round(rnorm(125), 2)),
  Element_6 =  abs(round(rnorm(125), 2)),
  Element_7 =  abs(round(rnorm(125), 2)),
  Element_8 =  abs(round(rnorm(125), 2)),
  Element_9 =  abs(round(rnorm(125), 2)),
  Element_10 =  abs(round(rnorm(125), 2))
)

C <-  tibble (
  Horizon = ("Horizon C"),
  Depth     = seq(44, 50, 0.6),
  Element_1 =  abs(round(rnorm(11), 2)),
  Element_2 =  abs(round(rnorm(11), 2)),
  Element_3 =  abs(round(rnorm(11), 2)),
  Element_4 =  abs(round(rnorm(11), 2)),
  Element_5 =  abs(round(rnorm(11), 2)),
  Element_6 =  abs(round(rnorm(11), 2)),
  Element_7 =  abs(round(rnorm(11), 2)),
  Element_8 =  abs(round(rnorm(11), 2)),
  Element_9 =  abs(round(rnorm(11), 2)),
  Element_10 =  abs(round(rnorm(11), 2))
)

Analyse.corr = rbind (A, B, C)


# Scatter Plot
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # Korrelmatrix
        pickerInput(
        inputId = "corr",
        label = "Element",
        choices = list(
          Main =  colnames(Analyse.corr) [c(3:7)],
          Minor = colnames(Analyse.corr) [c(8:12)]),
        multiple = TRUE,
        options = pickerOptions(
          actionsBox = TRUE,
          title = "Please select at least 4 item",
          header = "Main & Minor Elements")),
      
      br(),
      pickerInput(
        inputId = "lithcor",
        label = "Horizon",
        choices = unique(Analyse.corr$Horizon),
        selected = Analyse.corr$Horizon [1],
        multiple = TRUE,
        options =  list("max-options" = 1,
                        "max-options-text" = "No more!")),
      br(),
      selectInput("type", "Correlation Type", selected = NULL,
                  choices = c("parametric", "nonparametric"), multiple = FALSE), 
      
      br(),
      selectInput("p", "Choose a p-value", selected = NULL,
                  choices = c("0.01", "0.05"), multiple = FALSE), 
      br(),
      actionButton("clr", "Calculate matrix"), 
     
    ), 
    
    # end sidebar panel
    
    mainPanel(
      tabsetPanel(type = "tab",
                  tabPanel("Korrelmatrix", plotOutput("cormat"))
                  
      ))
    
  ))



# end main panel
server <- function(input, output, session) { 
  
  # correl matrix
  #filter
   reactivecorrel <- reactive({
     Analyse.corr%>%
       filter(Horizon %in% input$lithcor) %>%
       select(-Depth)
   })
   
   
   
  # correlogram
     observeEvent(input$clr,{
      output$cormat <- renderPlot({
      ggstatsplot::ggcorrmat(
      reactivecorrel (),
      cor.vars = input$corr, # Zugriff auf die Variablen Auswahl!
      type = input$type, # parametric for Pearson, nonparametric for Spearman's correlation
      sig.level = input$p,      #output = "p", will return the p-value matrix
      hc.order = TRUE,
      matrix.type = "upper", # 	Character, "upper", # (default), "lower", or "full", display full matrix, lower triangular or upper triangular matrix.
      outline.col = "white",
      title = "Correl matrix",
      lab = TRUE,
      subtitle = "Subunit: ",
      colors = c("darkred", "white", "steelblue") # change default colors
      
    )
       
      }, width = 1500, height = 1200)
  
  })
     
     
} 

shinyApp(ui, server)
Das Einzigste was mich noch stört, ist das in der Original Datei, die Korrelationen, welche signifikant sind in Shiny als non-siginfikant dargestellt werden. Hierfür weiß ich erstmal nicht weiter....... :(
Antworten