input <- list( displ = 1.8 )
library(tidyverse) library(shiny) #renderPlot, renderTable, sliderInput library(shinyWidgets) #radioGroupButtons get_data <- mpg
Filter the data:
#You can make this sidebar global by putting it ahead of Tab 1 #below are 2 input options: a gorup of buttons, and a slider radioGroupButtons(#for categorical variables inputId = "year", #this will allow the selection to be referenced as input$cyl label = "Select Year of Vehicle", #NULL if you don't want a header choiceNames = c("All", sort(unique(get_data$year))), #can use paste, etc to make nicer display choiceValues = c("All", sort(unique(get_data$year))), #values in the data, need to line up with above justified = T, #will fill the width of the container it sits in (sidebar) status = "primary") sliderInput( inputId = "displ", #referenced as input$displ label = "Select Weight:", value = range(get_data$displ), #default selection, you can move the range around min = (min(get_data$displ)), max = (max(get_data$displ)), step = .1) #This will build a dataframe to use throughout the dashboard. To reference this dataframe, you will need to be in some reactive element ex: renderText({nrow(use_data())}). This is essentially a function and so you will need to use parentheses at the end like this: use_data() use_data <- reactive({ df <- get_data %>% filter(between(displ, min(input$displ), max(input$displ))) #this if statements will filter for the selection of the radioGroupButtons if (input$year != "All") { df <- df %>% filter(year == input$year) } df })
renderPlot({ #renderPlot is only required becuase we are referencing someting reactive: use_data(). Otherwise we could just use ggplot(get_data,...) ggplot(use_data(), aes(displ, hwy)) + geom_smooth(color = "grey65") + geom_point(aes(color = factor(cyl))) + ylim(0, 45) + labs(color = "Cylinder") + theme(legend.position = "bottom") })
#this will display a top 10 table that is filtered based on the selections above renderTable(expr = {# use {...} section to create table use_data() %>% arrange(desc(hwy)) %>% slice(1:10) %>% mutate(Rank = row_number()) %>% select(Rank, year, manufacturer, model, trans, hwy, fl, class) }, spacing = "xs", align = "l", bordered = T)
r renderText({paste("Max Highway MPG by Class in Year:", input$year)})
renderTable( {#you can make separate objects (similar to in a function) count_n <- use_data() %>% count(class) max_value <- #this could have been done above, broken into different steps to show how it works use_data() %>% group_by(class) %>% summarise(hwy = max(hwy)) %>% ungroup() #and then add them together for the final product count_n %>% left_join(max_value) %>% arrange(desc(hwy)) %>% select(hwy, class, `#` = n) }, spacing = "xs", align = "l", bordered = T)
shinyWidgets
gallery has some nice widgets for filtering:
https://dreamrs-vic.shinyapps.io/shinyWidgets/
The gallery isn't always available. Another overview can be found here: Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.