Three main approaches we’ll take today:
But of course there is a universe of other stuff
xts
) inputgeom_polygon
) but also you
can just skip that with leafletggplotly()
function in
plotly to immediately turn your ggplot
interactive_interactive
version
and add a tooltip
aesthetic. That’s it!p <- ggplot2::economics %>% mutate(tooltip = paste0(date, '\n', scales::percent(uempmed/100, accuracy = .1))) %>%
ggplot(aes(x = date, y = uempmed)) + geom_line() + labs(x = 'Date', y = 'Unemployment Rate') +
scale_y_continuous(labels = function(x) scales::percent(x/100, accuracy = 1)) +
geom_point_interactive(aes(tooltip = tooltip), size = .5) +
theme_classic() + theme(text = element_text(family = 'serif', size = 13))
ggiraph(ggobj = p)
mutate
ing a new variable where you
paste0()
all the information you want together, using
\n
to break linesp <- iris %>% group_by(Species) %>% summarize(`Sepal Length` = mean(Sepal.Length)) %>%
mutate(tooltip = paste0('Species: ', stringr::str_to_title(Species), '\nAverage Sepal Length: ', `Sepal Length`)) %>%
ggplot(aes(x = Species, y = `Sepal Length`, tooltip = tooltip)) +
geom_col_interactive(fill = 'firebrick') +
theme_minimal()
ggiraph(ggobj = p)
geom_point_interactive
on top of a
geom_line
width
or
width_svg
and height_svg
in the
ggiraph()
function, but it’s not perfectruntime: shiny
at the toprender
or reactive()
functions, we can make our analysis update based on the controls!selectInput
), a slider
(sliderInput
), radio buttons (radioButtons
),
text (textInput
), numbers (numericInput
), a
checkbox (checkboxInput
), dates or date ranges
(dateInput
and dateRangeInput
) and file upload
(fileInput
)selectInput
selectInput(inputID, label, choices,
selected = NULL, multiple = FALSE,
selectize = TRUE, width = NULL,
size = NULL)
inputID
is the slot in input$
where
the result will be stored. so with inputID = 'subset'
, you
can later use input$subset
to know what was selected.
That’s not what the user sees for a title though, they see
label
choices
are the options, with default
selected
, in a standard vector format. So maybe to choose
whether to graph independent or chain restaurants,
choices = c('Choose Restaurant Type' = '',
'Independent','Chain')
multiple
determines whether multiple options can be
selectedrenderPlot()
(for plots), renderPrint()
(for
any object being printed / shown on its own), renderTable()
for tables of data, and renderText()
for actual text
output.input$inputID
, wrap that in {}
, and wrap THAT
in the appropriate render
function(Using fake data) in the global chunk:
library(tidyverse)
and data(RestaurantData)
.
In the sidebar column, an R chunk with:
And then in the next column,
This will show a table of all the data, letting you pick whether to show independent or chain restaurants.
mtcars
data we’ve used many times
beforegeom_bar(stat='summary',fun = 'mean')
graph summarizing the mean of a variable by the type of
transmissionselectInput
)
(Hint: use aes_string
to input a string variable to
ggplot2, this means everything must be a string in
it)textInput
). Note if you
put in something that’s not a color (which I check with
%in% colors()
) it will correct to black.global
chunk, and then (as we’ve covered) the sidebar
and the main partglobal
will only be run once,
very handy and speedy for when you change a control!params
argument in the YAML can take in inputsruntime: shiny
global
and load up
data(storms)
.selectInput()
to select a
status
status