Davide Bottoli
Davide Bottoli

Reputation: 91

Applying selecting dataset and filtering in R shiny

I've this problem. I'm starting to learn shiny and I can't figure out how to use a filter on a dataset derived from another shiny selector. In this particular case I would like to have one filter that works for any dataset that is selected at the first step.
I want to filter the dataset according to the column C, in particular I want to visualize only the rows with C > 1.

I'll report the code:

library(shiny)

set.seed(1)
rock <- data.frame(unif = runif(100, -1, 1),
                   norm = rnorm(100, 0, 2),
                   pois = rpois(100, 1))
paper <- data.frame(unif = runif(100, -2, 2),
                    norm = rnorm(100, 0, 4),
                    pois = rpois(100, 2))
scissor <- data.frame(unif = runif(100, -3, 3),
                      norm = rnorm(100, 0, 6),
                      pois = rpois(100, 3))


# Define UI for dataset viewer application
ui <- shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Shiny Text"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "paper", "scissor")),

    sliderInput("range", "Range:",
               min = 0, max = 10,
               value = c(0.5,5)),

    numericInput("obs", "Number of observations to view:", 20)
  ),

  # Show an HTML table with the requested
  # number of observations
  mainPanel(

    tableOutput("view")
  )
))

# Define server logic required to summarize and view the selected dataset
server <- shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "paper" = paper,
           "scissor" = scissor)
  })

  #Creating a data frame based on inputs

  ####?????####

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})


shinyApp(ui = ui, server = server)

My problem is what to put instead of ####?????#### in the server part.

I hope you can help me.

Upvotes: 0

Views: 1369

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Something like this?

library(shiny)

set.seed(1)
rock <- data.frame(unif = runif(100, -1, 1),
                   norm = rnorm(100, 0, 2),
                   pois = rpois(100, 1))
paper <- data.frame(unif = runif(100, -2, 2),
                    norm = rnorm(100, 0, 4),
                    pois = rpois(100, 2))
scissor <- data.frame(unif = runif(100, -3, 3),
                      norm = rnorm(100, 0, 6),
                      pois = rpois(100, 3))


# Define UI for dataset viewer application
ui <- shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Shiny Text"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "paper", "scissor")),

    sliderInput("range", "Range:",
                min = 0, max = 10,
                value = c(0.5,5)),

    numericInput("obs", "Number of observations to view:", 20)
  ),

  # Show an HTML table with the requested
  # number of observations
  mainPanel(

    tableOutput("view")
  )
))

# Define server logic required to summarize and view the selected dataset
server <- shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "paper" = paper,
           "scissor" = scissor)
  })

  #Creating a data frame based on inputs

  seconddata <- reactive({
    d <- datasetInput()
    d[d[,3] >= input$range[1] & d[,3] <= input$range[2],]
  })

  ####?????####

  # Show the first "n" observations
  output$view <- renderTable({
    head(seconddata(), n = input$obs)
  })
})


shinyApp(ui = ui, server = server)

Upvotes: 3

Related Questions