mercredi 26 avril 2017

How use RadioButton and checkboxGroup to create a graph R shiny

First, I start on R shiny, so I am interested in all your advice or explanations. In my project I want to create plot with several filters. I have data from several years and from several countries. For this I would like to use radionButton and checkboxGroup so that the user can select a country and a year for example and thus have the graph that uses only the data having its two parameters.

I have long search in forums and video to help me but I block

Here is what I have done, the Ui is good for me but I do not know how to program this in the server.

A big thanks in advance

  Ui :
  library(shiny)
  shinyUI(fluidPage(


titlePanel("test"),

sidebarLayout(
  sidebarPanel(
  h3("Filtering data"),
  selectInput("dataset","Choose a dataset (or a subset) :", 
              choices = c("Age","sex")),
  selectInput("Xvar", "X variable", 
              choices = c("weight","density")),
  selectInput("Yvar", "Y variable", 
              choices = c("hold","lenght")),
  checkboxGroupInput("Year", "Select the year:",
                     choices = list("All Years","2017","2016","2015","2014")),

  radioButtons("Country", label = ("select the country"),
               choices = list("Netherlands", "USA","Romania","India")) 
),

mainPanel(
  tabsetPanel(
    tabPanel(h6("Version"),h2("Boxplot"), plotOutput("boxPlot"))

        )
      )
  ))


  R : 
  library(shiny)
  # Other useful packages
  library(datasets)
  library(rpart)
  library(party)
  library(fpc)

  # Define colors
  palette(c("#E73032", "#377EB8", "#4DAF4A", "#984EA3",
      "#FF7F00", "#FFDD33", "#A65628", "#F781BF", "#999999"))



  # Define server logic 
  shinyServer(function(input, output, session) {

    datasetInput <- reactive({
      switch(input$dataset,
       "Age" = subset(test2, test2$character == "Age"),
       "Sex" = subset(test2, test2$character == "Sex")
      )
    })


    myColors <- reactive({
      df_test2 <- datasetInput()
      switch(input$dataset,

       "Age" = palette()[1],
       "Sex" = palette()[2],
               })

     # BOX PLOT
    output$boxPlot <- renderPlot({
      df_test2 <- datasetInput()

      boxplot(df_test2[,c(input$Xvar)] ~ df_test2[,c(input$Yvar)] , xlab =       input$Yvar, ylab = input$Xvar
        , border = "black", col = myColors())

    })  
  #CheckBox Group
    output$data <- renderTable({
      df_test2 <- datasetInput()
      df_test2[, c("Year", input$Year)]  
    })
  })




Aucun commentaire:

Enregistrer un commentaire