lundi 23 décembre 2019

Multiple IF if statements in shiny

I would like to ask how to handle multiple if, else statments in R shiny, When I try writting simple scripts that would plot distribution based on selection for some reason the third one is not working , I can quite tell if it is because of shiny or because of some error in syntax of statments.

code is as follows:

library(shiny)
library(ggplot2)


ui <- fluidPage(title = "zkoušíme shiny", 


  sliderInput(inputId = "slider",
              label = "Nastavené honot",
              value = 500, min = 250, max = 5000, step = 25),

  numericInput(inputId = "bin_nums", label = "Zadejte počet binu", 
              value = 15, min = 15, max = 40, step = 5, width = "100px"),

  selectInput(inputId = "colsel", label = "Hodnoty barev", 
               choices = c("red", "dodgerblue", "green"), selected = "red", width = "115px"),


  selectInput(inputId = "dist", label = "Select Distribution", 
              choices = c("Normal", "Exponecial", "Poisson"), selected = "Normal", width = "100px"),


  plotOutput("hist")

  )



server <- function(input, output) {
  output$hist <- renderPlot({
    title <- "Hodnoty histogramu"
    set.seed(101)
    DIST <- input$dist

    if (DIST == "Normal") {
    df <- rnorm(input$slider)
    df <- data.frame(df)
    ggplot(df, aes(df)) + geom_histogram(bins = input$bin_nums, fill = input$colsel, color = "white")
}

    else {
      if (DIST == "Exponecial") {
        df <- rexp(n = input$slider)
        df <- data.frame(df)
        ggplot(df, aes(df)) + geom_histogram(bins = input$bin_nums, fill = input$colsel, color = "white")
        }
      }

    else{
      df <- rexp(n = input$slider)
      df <- data.frame(df)
      ggplot(df, aes(df)) + geom_histogram(bins = input$bin_nums, fill = input$colsel, color = "red") 
    }
  }

    })
}

shinyApp(ui = ui, server = server)

Aucun commentaire:

Enregistrer un commentaire