LoslegenKostenlos loslegen

Adding placeholders for outputs

Outputs are any object that should be displayed to the user and is generated in R, such as a plot or a table.

To add an output to a Shiny app, the first thing you need to do is add a placeholder for the output that tells Shiny where to place the output.

There are several output placeholder functions provided by Shiny, one for each type of output. For example, plotOutput() is for displaying plots, tableOutput() is for outputting tables, and textOutput() is for dynamic text.

Diese Übung ist Teil des Kurses

Case Studies: Building Web Applications with Shiny in R

Kurs anzeigen

Anleitung zur Übung

  • Create a text input with an ID of "name" in the sidebar panel.
  • Add three output placeholders to the main panel:
    • A text output with ID "greeting" (line 14).
    • A plot output with ID "cars_plot" (line 16).
    • A table output with ID "iris_table" (line 18).

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

library(shiny)

# Define UI for the application
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # Create a text input with an ID of "name"
      ___(___, "What is your name?", "Dean"),
      numericInput("num", "Number of flowers to show data for",
                   10, 1, nrow(iris))
    ),
    mainPanel(
      # Add a placeholder for a text output with ID "greeting"
      textOutput(outputId = ___),
      # Add a placeholder for a plot with ID "cars_plot"
      ___("cars_plot"),
      # Add a placeholder for a table with ID "iris_table"
      ___(___)
    )
  )
)

# Define the server logic
server <- function(input, output) {}

# Run the application
shinyApp(ui = ui, server = server)
Code bearbeiten und ausführen