Visual Analytics Flashcards

1
Q

What are geoms?

A

Visual marks representing data points

Geoms, short for “geometric objects” are the basic visual element used to create plots.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why does ggplot(…) do?

A

Initialises a ggplot object

  • Declare the input data frame for a graphic and specify the set of plot aesthetics (that will be common to all subsequent laters)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What layer creates a scatterplot?

A

geom_point()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are scatterplots useful for?

A

Displaying the relationship between two continuous variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you colour points by a variable?

A

Within aes()

color = factor(var)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you plot a line graph?

A

geom_line()

Useful for trend lines and time-series

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you plot a box plot?

A

geom_boxplot()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many geoms should you have?

A

Need to have at least one, there is not upper limit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does geom_smooth() do?

A

Adds a smoothed trend line to a plot, helping to visualise patterns in the data.

eg geom_smooth(method = lm_

geom_smooth(method = loess)

Default is loess for < 1000 and GAM for > 1000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What kinds of variables can you include in geom_point()

A

Map variables to aesthetics

  • colour
  • shape
  • size
  • alpha
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you display the definition of a function eg geom_bar and its default parameter values?

A

args() function

eg args(geom_bar)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What kinds of arguments can be customised in geom_histogram()?

A

+ geom_histogram(stat = “bin”, binwidth = 3)

stat = “bin” is the default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are “scales”?

A

Functions that control how the data is mapped to visual properties
eg position (x and y axis), colour, shape or zie

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does scale_color_brewer() do and how is it used?

A

Used to modify the color scheme of the points (based in colour scheme defined in ggplot aesthetics)

+ scale_color_brewer(type = “seq”, palette = 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you import ggplot?

A

library(ggplot2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If using shapes to represent a categorical variable, how do you change the shapes from the default?

A

scale_shape_manual(values = c(16, 15, 17))

passing in a list of chosen shapes corresponding to number of categories

ANOTHER EXAMPLE

+scale_color_manual(name=’Legend’, values=c(‘D’=”grey”,
‘E’=’red’…..))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What does adding scale_size(range = c(1, 10)) do?

A

This function scales the size of the points based on the size variable.

range = c(1, 10) means that the smallest value of size will be mapped to a point size of 1, and the largest value will be mapped to a point size of 10.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does facet do?

A

It is a way of displaying multiple plots that share the same structure and data, but differ in some aspect of their visual appearance, such as the values of a categorical variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you facet a plot?

A

+ facet_wrap(~var, ncol=3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is theme in ggplot?

A

Theme is a set of aesthetic attributes that can be used to customise the appearance of non-data elements.

Includes
- Axis labels
- Legend
- Title
- Background
- Font size and colour

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you make the shapes into line shapes?

A

+ scale_shape(solid = FALSE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you change the colour and size of plotted points?

A

+ geom_point(shape = 21, colour = “black”, fill = “white”, size = 5, stroke = 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you add a theme?

A

Add to the document

theme_set(theme_bw())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you set limits on the x or y axis?

A

xlim(0, 1)
ylim(0, 1)

To adjust them:
+coord_cartesian(xlim=c(0,3),ylim = c(0,5000))

Difference is xlim/ylim removes data points outside the range but coord_cartesian keeps them, just crops the range.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you add a theme to a graph?

A

plot + theme_bw()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How do you add labels to your plot?

A

+ labs()

can set eg
- title
- subtitle
- y
- x
- caption

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What does stroke control?

A

The thickness of the point boundary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you change the colour of text on a plot?

A

+ theme(text = element_text(colour = “blue”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you change the size of text of the title and axis labels?

A

+ theme(plot.title=element_text(size=25),
axis.title.x=element_text(size=20),
axis.title.y=element_text(size=20),
axis.text.x=element_text(size=15),
axis.text.y=element_text(size=15))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How can you change the x and y tick labels?

A

scale_x_continuous()

eg + scale_x_continuous(labels=c(“zero”, “one”, ….. ))
eg + scale_y_continuous(breaks= seq(0, 20000, 4000))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How do you rotate the angle of the axis?

A

+ theme(axis.text.x = element_text(angle = 45))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How do you flip the x and y axis?

A

+ coord_flip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you facet based on multiple categorical variables?

A

+ facet_grid(color ~ cut)

It arranges the subplots in a grid format, where:
color defines the rows.
cut defines the columns.

If you don’t need a strict row-column layout, you can use:
+ facet_wrap(~ color + cut)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What does setting scales = “free” mean?

A

Allows each facet (subplot) to have its own independent axis scales instead of using a shared scale across all facets.

Add it within facet_wrap / facet_grid

  • scales=’free_x’ to free only X-axis
  • scales=’free_y’ to free only Y-axis.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

How can you arrange multiple plots?

A

library(gridExtra)
grid.arrange(p1,p2,ncol=2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How can you match the colour of the smooth added to the categorical variable?

A

+ geom_smooth(aes(color=color))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you add a horizontal line to a plot?

A

geom_hline()

eg geom_hline(yintercept=5000, size=2, linetype=”dotted”, color=”blue”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

How do you add vertical line to a plot?

A

geom_vline()

eg + geom_vline(xintercept=4, size=2, color=”firebrick”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

How do you draw a line from x to y?

A

geom_segment()

eg geom_segment(aes(x=4, y=5000, xend=4, yend=10000, size=2), lineend=”round”)
In this example it is a vertical line (x and xend are the same)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

How do you plot a bar chart side by side vs stacked?

A

p1 <- gg + geom_bar(position=”dodge”, aes(fill=factor(vs))) # side-by-side

p2 <- gg + geom_bar(aes(fill=factor(vs))) # stacked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What does aggregate do?

A

Groups the data and applies a function

eg df <- aggregate(mtcars$mpg, by=list(mtcars$cyl), FUN=mean)

mtcars$mpg is the column being aggregated (Miles Per Gallon).
by = list(mtcars$cyl): This groups the data by the number of cylinders (cyl).
FUN = mean: It calculates the mean mpg for each cylinder group.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

If plotting a bar chart where the y data is “explicit” what do we need to do?

A

stat = “identity”

if the value is already calculated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

How do you ensure data is treated as categorical?

A

Eg in a dataframe, apply as.factor() to the column

eg df$cyl <- as.factor(df$cyl)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

How do you convert from Wide Format to Long Format using melt()?

A

reshape2::melt

eg reshape2::melt(economics[, c(“date”, “psavert”, “uempmed”)], id=”date”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

What is an area plot and how do we create it?

A

An area plot is a graph that uses color to show how data changes over time.

+ geom_area()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

What two ways can you plot multiple variables on an area plot?

A

Non-overlapping (stacked) or overlapping

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

How do you create the two versions of the area plot?

A

Non-overlapping / stacked - use the long format dataset
+ geom_area(aes(y=value, fill=variable))

Overlapping - use the wide format dataset
+ geom_area(aes(y = variable, fill = “cornflowerblue”)
Add a new layer for each variable to plot, may need to adjust alpha values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

What must app.R end with?

A

A call to shinyApp()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

How do you include a sliding select scale?

A

sliderInput(
inputId = “num”,
label = “Choose a number”,
value = 25,
min = 1,
max = 100
)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

How do you add a graph?

A

Use of plotOutput and renderPlot

UI side:
plotOutput(“hist”)

Server side:
output$hist <- renderPlot({
hist(rnorm(input$num))
)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

How can you incorporate a button?

A

actionButton()
submitButton()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

How do you include a single check box?

A

checkboxInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

How do you include multiple checkboxes in a group?

A

checkboxGroupInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How do you include a date?

A

dateInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

How do include a date range?

A

dateRangeInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

How do you include a file?

A

fileInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

How do you create a drop down of numbers?

A

numericInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

How do you include a Password field?

A

passwordInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

How do you include radio buttons?

A

radioButtons()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

How do you include a drop down selection?

A

selectInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

How do you include a slider?

A

sliderInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

How do you include a text field?

A

textInput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

How do you create an input?

A

with an *Input() function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the inputId used for?

A

For internal use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

What is the label used for?

A

Used to display

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

How do you insert an interactive table?

A

dataTableOutput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

How do you insert raw HTML?

A

htmlOutput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

How do you insert an image?

A

imageOutput()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

How do you insert a plot?

A

plotOutput()

70
Q

How do you insert a tableOutput()?

A

tableOutput()

71
Q

How do you insert a Shiny UI element?

A

uiOutput()

72
Q

How do you insert text?

A

verbatimTextOutput()

73
Q

How do you display an output?

A

Using *Output()

Where * is the type of output and the name to give to the output object goes in quotes between ()

eg plotOutput(“hist”)

74
Q

What do you need to have between arguments?

75
Q

What doe *Outpt() do and why is it not sufficient on its own?

A

*Output() is written in the UI and adds a space in the UI for an R object.

You have to build the object in the server function.

76
Q

What do we tell the server?

A

How to assemble inputs into outputs

77
Q

What are the 3 rules to write the server function?

A

1 - save objects to display to output$
eg output$hist
2 - build objects to display with render*()
eg renderPlot({})
3 - access input values with input$

78
Q

How do you create an interactive table (from a data frame, matrix or other table-like structure)?

A

renderDataTable()

79
Q

How do you create a code block of printed output?

A

renderPrint()

80
Q

How do you create a character string?

A

renderText()

81
Q

What does render*() do?

A

Builds a reactive output to display in the UI

Where* is the type of object to build and inside the parenthesis is the code block that builds the object ({})

82
Q

What is reactivity?

A

The application automatically updates its UI in response to changes in data or events, without requiring explicit manual updates.

It occurs whenever you use an input value to render an output object.

83
Q

Which part of the shiny app is an observer object?

A

The reactive endpoint

84
Q

What are inputs and outputs in terms of reactivity?

A

input - reactive value
output - reactive function

85
Q

When does the input value change?

A

Whenever a user changes the input control

86
Q

How do you call a reactive value?

A

Reactive values act as the data streams that flow through the app.

It has to be called in a reactive function. ie render*()

Otherwise will get an error message to tell you.

87
Q

If you think of reactivity in R as a two step process, what are the steps?

A

1 - reactive values notify the functions that use them when they become invalid

2 - the objects created by reactive functions respond

88
Q

How do you outputcomputed statistics?

A

verbatimTextOutput(“stats”) in UI

output$stats <- renderPrint ({
summary(rnorm(input$num))
})

89
Q

If we have multiple outputs what should we use?

A

reactive()

eg

Use reactive() to generate the random sample once
data <- reactive({ rnorm(input$num) })

  • This ensures consistency, that they are both using the same dataset
  • Modularise the code with reactive()
90
Q

What does reactive() do?

A

Builds a reactive object (reactive expression) to use (in downstream code), reactive expression

91
Q

Why are reactive expressions special?

A

1 - You call a reactive expression like a function eg data()

2 - reactive expressions cache their values (expressions will return its most recent value, unless it has become invalidated)

92
Q

Why might you want to prevent a reaction with isolate()?

A

Eg example of updating a title - want to prevent the title field from updating when the user enters different text

93
Q

What does isolate() do?

A

Turns an object into a non-reactive object

Use isolate() to treat reactive objects
like normal R objects

Allows you to access a reactive value or expression without triggering a reactive dependency.

It is mainly used inside observe(), observeEvent(), and eventReactive().

94
Q

How can we trigger code?

A

With observeEvent()

95
Q

What does acionButton() do?

A

Takes action in response to a specific action

UI:
actionButton(inputId = “clicks”,
label = “Click me”)

SERVER:
observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})

We create an observer object to respond to the action button

96
Q

How do you print to console the number of times a button has been clicked?

A

observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})

Need to specify precisely which reactive values should notify the observer ie input$clicks

97
Q

What does observeEvent() do?

A

Take action in response to a specific event

98
Q

What does observe() do?

A

Takes action in response to input changes

Creates an observer object which may not display anything

99
Q

How do we delay reactions?

A

with eventReactive()

This is a reactive expression that only responds to a specific event - the event mentioned before {} eg input$go

100
Q

Why would we want to use eventReactive()?

A

To delay a reaction eg prevent a graph from updating until we hit a button

101
Q

How do you implement event reactive?

A

eg actionButton(inputId = “
go”,
label = “Update”)

SERVER:
data <- eventReactive(input$go, {
rnorm(input$num)
})
output$hist <- renderPlot({
hist(data())
})

102
Q

How can you manage state?

A

reactiveValues()

103
Q

What does reactiveValues() do?

A

Creates a list of reactive values to manipulate programmatically.

eg
# Create a reactiveValues object with an initial count of 0
values <- reactiveValues(count = 0)

Can increment this count when a button is clicked etc.

THIS IS IN THE SERVER

104
Q

How do you manipulate reactiveValues()?

A

Usually with observeEvent()

105
Q

How can you create distributions?

A

rnorm or runoff

106
Q

What do you use render() for?

A

To make an object to display in the UI

107
Q

What do you use reactive() for?

A

To make an object to use in downstream code

108
Q

What do you use isolate() for?

A

To make an object to use in downstream code

109
Q

What do you use eventReactive () for?

A

To delay a reaction

110
Q

What do you use observeEvent() for?

A

To trigger code

111
Q

What do you use reactiveValues() for?

A

To make your own reactive values

112
Q

Discuss reactive expressions vs observers.

A

Reactive expressions are callable, return a value, are lazy (passive) and are cached.

Reactive expressions are used for calculations whereas observers are used for side effects.

113
Q

What builds the user interface for the Shiny app?

A

HTML

ui <- fluidPage() is container-fluid

114
Q

How do you create a heading in the Shiny App?

115
Q

How do you create an anchor element in the Shiny app?

116
Q

How can you see what html tags are available?

A

names(tags)

117
Q

How can you add a link to the code

A

tags$a(href = “url”, “Text to Appear”)

118
Q

How do you insert text with HTML?

A

Character strings do not need a tag, can just type “….”

Can neaten the content by adding a new paragraph

tags$p(“Text”)

119
Q

How can you put something in italics?

A

tags$em(“Text”)

120
Q

How can you put something in bold?

A

tags$strong(“Text”)

121
Q

How can you add monospaced text (code)?

A

tags$code(‘code”)

122
Q

How do you add a bold word within a sentence?

A

You can nest the functions inside of others

tags$p(“This is a”,
tags$strong(“Shiny”),
“app.”)

123
Q

How do you insert a line break?

124
Q

How can you add a horizontal line?

A

tags$hr()

for horizontal rule

125
Q

How can you add an image?

A

tags$img(src = “www”, width = 10, height = 10)

Use the source argument to point to the image URL

126
Q

To add an image from a saved location, how do you need to save it?

A

Create a folder called “www” in the same directory as app.R and place the image in this. Then call src = “filename.png”

127
Q

What function creates a hyperlink?

128
Q

How do you change the font of text?

A

Within the p() element

p(style = “font-family:Impact”,
“Text”)

129
Q

How can you pass in a character string as raw HTML?

A

HTML()

eg HTML(“<h1>My Shiny App</h1>”)

130
Q

How do you position elements within an app?

A

Using layout functions

This divides the UI into a grid of rows and columns

131
Q

Within the layout, how many columns are in each row?

132
Q

How do you divide the UI into a grid of rows and columns

A

fluidRow()

column(width = 2)

133
Q

What does fluidRow() do?

A

Adds rows to the grid.

Each new row goes below the previous rows (“fluid”)

134
Q

What does column do?

A

Adds columns within a row. Each new column goes to the right of the previous column

column(3)

135
Q

What do you specify within a column() call?

A

Specify the width and optionally the offset

eg column(4, offset = 8)

136
Q

How do you place an element in the grid?

A

Call it as an argument of a layout function

eg fluidRow(
“In the row”
}

137
Q

What is a panel?

A

A single unit with its own properties, which contains multiple elements

138
Q

What kinds of panels are there?

A
  • tabPanel()

We can combine tabPanel() with one of:

  • tabsetPanel()
  • navlistPanel()
139
Q

What is tabPanel()?

A

tabPanel() creates a stackable layer of elements, called a tab.

Each tab is like a small UI of its own.

tabPanel(“Title” …. [elements of the tab]….)

140
Q

What does tabsetPanel() do?

A

Combines tabs into a single panel. You can us the tab key to navigate between tabs.

tabsetPanel(
tabPanel(“Tab 1”, “Contents”),
tabPanel(“Tab 2”, “Contents”),
tabPanel(“Tab 3”, “Contents”)
)

141
Q

What is the difference between tabsetPanel() and navlistPanel()?

A
  • Tabset appears across top
  • Use tabs to navigate between tabs
  • Navlist appears on side
  • Uses links to navigate between tabs
142
Q

What does navlistPanel() do?

A

Combines tabs into a single panel. Use links to navigate between tabs.

Arrange tab panels into a stack with sidebar navigations

navlistPanel(
tabPanel(“Tab 1”, “contents”),
tabPanel(“Tab 2”, “contents”),
tabPanel(“Tab 3”, “contents”)
)

143
Q

How do you group elements into a grey box?

A

wellPanel()

Groups elements into a grey “well”

144
Q

What kinds of prepackaged layouts can we use?

A

flowLayout()
sidebarLayout()
splitLayout()

145
Q

How can we utilise sidebarLayout() to divide an app into two sections?

A

sidebarLayout(
sidebarPanel(),
mainPanel()
)

146
Q

What is an alternative to fluidPage()?

A

navbarPage(title = “Title”,
tabPanel(“tab 1”, “contents”)
)

navbarPage() combines tabs into a single page

147
Q

How can we combine tab links into a dropdown menu for navbarPage()?

A

navbarMenu()

navbarMenu(title = “More”,
tabPanel(“tab 1”, “contents”)
)

148
Q

What is a pre-made dashboard template we can use?

A

These come from the shinydashboard package

library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)

149
Q

How can we segment elements within a row?

A

box()

treat the same as column

150
Q

Within dashboardSidebar() how can we add a menu?

A

sidebarMenu(
)

and include menuItem()

151
Q

How can we add icons?

A

icon = icon(“dashboard”)

152
Q

How do you create a fixed (non-fluid) design?

A

Use fixedPanel() with fixedRow()

153
Q

If giving a numeric input, what should you include?

A

Minimum and maximum values

eg min = 1 and max = nrow(dataset)

154
Q

How can you process a dataset based on desired criteria?

A

Use a reactive function

eg movies_to_display <- reactive({
req(input$type, input$sample_size) # ensure availablity of value before proceeding
filter(movies, title_type %in% input$type) %>%
sample_n(input$sample_size)
})

155
Q

How do you include a data table output?

A

library(DT)

DT::dataTableOutput() in UI

DT::datatable in Server

156
Q

How do you need to pass variables into the aesthetic?

A

aes_string()

157
Q

How do you need to call the processed dataset?

A

Assuming it was processed with reactive(), need to call it as a function data()

158
Q

How do you print a statement about your processed data?

A

Need a dynamic UI element.

UI:
uiOutput(“n”)

SERVER:
output$n <- renderUI({
types <- movies_to_display()$title_type %>%
factor(levels = input$type)

counts <- table(types)

p("There are ", counts, input$type, " movies in this dataset.")   })
159
Q

In a sliderInput, how do you ensure that integers are treated as years, ie presented as XXXX rather than X,XXX?

A

sep = “”

160
Q

How do you print output from R functions (like summary(), str(), table())

A

renderPrint() - pass to verbatimTextOutput

161
Q

How do you add conditioning to the panel displayed, dependent on the panel you are on?

A

conditionalPanel() - wrap the “conditional” parts in here and add condition = “input.main ==1” at the start

within tabsetPanel in the main panel, add “id = “main””, and add “value = X” to each of the tabPanels(), where value = 1 for the tab you want to have the conditional elements displayed for.

162
Q

What library do you need to have to ensure you have the iris dataset?

A

library(dplyr) or library(ggplot2)

163
Q

How do you cluster the data for use in a plot?

A

data <- reactive({
req(input$x, input$y)
iris[, c(input$x, input$y)]
})

clusters <- reactive({
req(input$count)
kmeans(data(), input$count)
})

Then in the plot
geom_point(color = factor(clusters()$cluster))

164
Q

What can you use reactiveValues() and observeEvent() together for?

A

Eg update a value on a button click

rv <- reactiveValues(
norm = rnorm(500),
unif = runif(500),
chisq = rchisq(500, 2)
)

observeEvent(input$renorm, {
rv$norm <- rnorm(500)
})

165
Q

How do you have a multi-drop selection within a tab panel?

A

navbarMenu() - add a title and tabpanel() s for each drop down option

166
Q

When you have two reactive outputs, how could you restructure the code?

A

data <- reactive({
rnorm(input$num)
})

output$hist <- renderPlot({
hist(data())

})
output$stats <- renderPrint({
summary(data())
})

167
Q

Explain what isolate does in this context:

output$hist <- renderPlot({
hist(rnorm(input$num), main = isolate(input$title))
})

A

The title will only be updated if num changes.

168
Q

How do you print to console the number of times a button is clicked?

A

observeEvent(input$clicks, {
print(as.numeric(input$clicks))
})

169
Q

What is the difference between reactive() and eventReactive()?

A

Use reactive() if you want the histogram to update automatically when input$num changes.

Use eventReactive() if you want the user to click a button to trigger updates, preventing unnecessary re-computation.