Chapter 7 Graphics
Exercise 7.6.1
Make a plot with click handle that shows all the data returned in the input.
Solution.
Solution
We can use the allRows
argument in nearPoints
to see the entire data and
add a boolean column that will be true TRUE
for the given point (i.e., row)
that was clicked.
library(shiny)
library(ggplot2)
<- fluidPage(
ui plotOutput("plot", click = "plot_click"),
tableOutput("data")
)
<- function(input, output, session) {
server $plot <- renderPlot({
outputggplot(mtcars, aes(wt, mpg)) + geom_point()
res = 96)
},
$data <- renderTable({
outputnearPoints(mtcars, input$plot_click, allRows = TRUE)
})
}
shinyApp(ui, server)
Exercise 7.6.2
Make a plot with click, dblclick, hover, and brush output handlers and nicely display the current selection in the sidebar. Plot the plot in the main panel.
Solution.
Solution
We can use the nearPoints
function to extract the data from plot_click
,
plot_dbl
, and plot_hover
. We need to use the function brushedPoints
to
extract the points within the plot_brush
area.
To ‘nicely’ display the current selection, we will use dataTableOutput
.
library(shiny)
library(ggplot2)
# Set options for rendering DataTables.
<- list(
options autoWidth = FALSE,
searching = FALSE,
ordering = FALSE,
lengthChange = FALSE,
lengthMenu = FALSE,
pageLength = 5, # Only show 5 rows per page.
paging = TRUE, # Enable pagination. Must be set for pageLength to work.
info = FALSE
)
<- fluidPage(
ui
sidebarLayout(
sidebarPanel(
width = 6,
h4("Selected Points"),
dataTableOutput("click"), br(),
h4("Double Clicked Points"),
dataTableOutput("dbl"), br(),
h4("Hovered Points"),
dataTableOutput("hover"), br(),
h4("Brushed Points"),
dataTableOutput("brush")
),
mainPanel(width = 6,
plotOutput("plot",
click = "plot_click",
dblclick = "plot_dbl",
hover = "plot_hover",
brush = "plot_brush")
)
)
)
<- function(input, output, session) {
server
$plot <- renderPlot({
outputggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
res = 96)
},
$click <- renderDataTable(
outputnearPoints(iris, input$plot_click),
options = options)
$hover <- renderDataTable(
outputnearPoints(iris, input$plot_hover),
options = options)
$dbl <- renderDataTable(
outputnearPoints(iris, input$plot_dbl),
options = options)
$brush <- renderDataTable(
outputbrushedPoints(iris, input$plot_brush),
options = options)
}
shinyApp(ui, server)
Exercise 7.6.3
Compute the limits of the distance scale using the size of the plot.
<- function(id) {
output_size reactive(c(
$clientData[[paste0("output_", id, "_width")]],
session$clientData[[paste0("output_", id, "_height")]]
session
)) }
Solution.
Solution
Let us use the plot’s width and height to estimate the scale limits for our plot.
To verify that the recommended limits are correct, click around the plot and watch how the distance scale changes on the legend. These values should oscillate between the recommended limits.
Resize the browser’s window to change the width and height reactives.
library(shiny)
library(ggplot2)
<- data.frame(x = rnorm(100), y = rnorm(100))
df
<- fluidPage(
ui plotOutput("plot", click = "plot_click"),
textOutput("width"),
textOutput("height"),
textOutput("scale")
)
<- function(input, output, session) {
server
# Save the plot's widht and height.
<- reactive(session$clientData[["output_plot_width"]])
width <- reactive(session$clientData[["output_plot_height"]])
height
# Print the plot's width, the plot's height, and the suggested scale limits.
$width <- renderText(paste0("Plot's width: ", width()))
output$height <- renderText(paste0("Plot's height: ", height()))
output$scale <- renderText({
outputpaste0("Recommended limits: (0, ", max(height(), width()), ")")
})
# Store the distance computed by the click event.
<- reactiveVal(rep(1, nrow(df)))
dist
# Update the dist reactive as needed.
observeEvent(input$plot_click, {
req(input$plot_click)
dist(nearPoints(df, input$plot_click, allRows = TRUE, addDist = TRUE)$dist_)
})
$plot <- renderPlot({
output$dist <- dist()
dfggplot(df, aes(x, y, size = dist)) +
geom_point()
})
}
shinyApp(ui, server)