Reputation: 3
I am trying to buil a shiny app that returns a graph based on triplet (variable, min, max) selected by the user. I probably miss something in the war shint and dplyr evaluate statements, but my instinctive way of doing this does not work. A condition on the type : filter(iris, input$variable >= input$range[1] & input$variable <= input$range[2]
leads no an empty dataset.
Here is an example with the iris dataset:
library(shiny)
library(tidyverse)
ui <- fluidPage(
titlePanel("Filter example"),
sidebarLayout(
sidebarPanel(
sliderInput("dimension",
"Dimension:",
min = 1,
max = 8,
value = c(1,8)),
selectInput("Petal or Sepal", "type",
c("Sepal Length" = "Sepal.Length",
"Petal Length" = "Petal.length"))
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
plotdata <- reactive({
filter(iris, input$type <= input$dimension[1] & input$type >= input$dimension[2])
})
output$distPlot <- renderPlot({
MyPlot <- ggplot(data = plotdata(),
mapping = aes(x = Sepal.Width, fill = Species))
Myplot + geom_histogram()
})
}
shinyApp(ui = ui, server = server)
The issue is obviously in the
plotdata <- reactive({
filter(iris, input$type <= input$dimension[1] & input$type >= input$dimension[2])
})
statement, but after hours of trawling though StackOverflow, I could not find a solution.
Thans for your help
Upvotes: 0
Views: 44
Reputation: 2864
There are some parameter mismatches and typos in the example code. Your filtering implementation also has issues. The following should work:
library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
titlePanel("Filter example"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "dimension",
label = "Dimension:",
min = 1,
max = 8,
value = c(1,8)),
selectInput(
inputId = "type",
label = "Petal or Sepal",
choices = c(
"Sepal Length" = "Sepal.Length",
"Petal Length" = "Petal.Length"
)
)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
plotdata <- reactive({
iris[(iris[ , input$type] >= input$dimension[1] & iris[ , input$type] <= input$dimension[2]), ]
})
output$distPlot <- renderPlot({
ggplot(
data = plotdata(),
mapping = aes(x = Sepal.Width, fill = Species)
) +
geom_histogram()
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1