
A plot whose bars and points explain where they came from
Source:R/output-plot.R
whencePlotOutput.RdwhencePlotOutput() shows a plot of a summary table. The viewer clicks a
bar or a point to open the panel of that row of the table: how many rows of
its group each step removed, the code that makes the row, and the rows of
the group. Under the plot, "Show the data" gives the same table, so a
keyboard user can open each panel too.
Usage
whencePlotOutput(outputId, label = NULL, width = "100%", height = "400px")
renderWhencePlot(
expr,
plot,
format = NULL,
width = "auto",
height = "auto",
res = 72,
alt = NA,
env = parent.frame(),
quoted = FALSE,
outputArgs = list()
)Arguments
- outputId
The output id.
- label
Text to show before the value, or
NULLfor none. The panel uses it in its heading too.- width, height
The size of the plot, as for
shiny::plotOutput().- expr
A pipeline that ends in a summary table.
- plot
A function that takes the summary table and gives a plot.
- format
As for
renderWhenceTable(): for the table under the plot, and the panels.- res, alt
As for
shiny::renderPlot().- env
The environment in which to evaluate
expr.- quoted
Is
expralready quoted? UseTRUEwhen you give an expression made withquote().- outputArgs
A list of arguments for
whenceOutput(), such aslabel, when the render is used in an R Markdown document withruntime: shiny.
Value
whencePlotOutput() gives an HTML tag for the UI.
renderWhencePlot() gives a render function for the server.
Details
renderWhencePlot() makes the plot. expr is a pipeline that ends in a
summary, as for whence_table(). plot is a function that takes that
summary and gives a 'ggplot2' plot. A click finds its row through the
aesthetics of the plot: on a discrete x axis, by the bar under the click;
otherwise, by the nearest point. The x (and y) aesthetics must be
columns of the summary.
Examples
whencePlotOutput("by_cyl", label = "Mean mpg by cylinders")
#> <div class="whence-plot" data-whence-id="by_cyl" data-label="Mean mpg by cylinders">
#> <div class="whence-label">Mean mpg by cylinders</div>
#> <div class="shiny-plot-output html-fill-item" data-click-clip="TRUE" data-click-id="by_cyl_whence_click" id="by_cyl" style="width:100%;height:400px;"></div>
#> <details class="whence-plot-data">
#> <summary>Show the data</summary>
#> <div class="whence-plot-table" id="by_cyl-whence-table"></div>
#> </details>
#> </div>
if (interactive() && requireNamespace("dplyr", quietly = TRUE) &&
requireNamespace("ggplot2", quietly = TRUE)) {
library(shiny)
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
ui <- fluidPage(whencePlotOutput("by_cyl", label = "Mean mpg by cylinders"))
server <- function(input, output, session) {
output$by_cyl <- renderWhencePlot(
mtcars |>
filter(hp > 90) |>
group_by(cyl = factor(cyl)) |>
summarise(mpg = mean(mpg)),
plot = function(data) ggplot(data, aes(cyl, mpg)) + geom_col()
)
}
shinyApp(ui, server)
}