Inizia subitoInizia gratis

Aggiungi un output tabellare

Per aggiungere qualsiasi output a un'app Shiny, devi:

  1. Creare l'output (grafico, tabella, testo, ecc.).
  2. Eseguire il rendering dell'oggetto di output usando la funzione render___ appropriata.
  3. Assegnare l'oggetto renderizzato a output$x.
  4. Aggiungere l'output alla UI usando la funzione ___Output appropriata.

In questo esercizio, aggiungerai un output tabellare all'app di esplorazione dei nomi dei bebè che hai creato prima. Non dimenticare che il codice dentro una funzione render___ deve essere racchiuso tra parentesi graffe (ad es. renderPlot({...})).

Questo esercizio fa parte del corso

Creare applicazioni web con Shiny in R

Visualizza corso

Istruzioni dell'esercizio

  • Crea un output tabellare chiamato table_top_10_names, con i 10 nomi più popolari per sesso e anno. Puoi usare la funzione top_10_names() per generare un data frame da visualizzare.
  • Visualizza la tabella nella UI.

esercizio interattivo pratico

Prova questo esercizio completando questo codice di esempio.

ui <- fluidPage(
  titlePanel("What's in a Name?"),
  # Add select input named "sex" to choose between "M" and "F"
  selectInput('sex', 'Select Sex', choices = c("F", "M")),
  # Add slider input named "year" to select year between 1900 and 2010
  sliderInput('year', 'Select Year', min = 1900, max = 2010, value = 1900)
  # CODE BELOW: Add table output named "table_top_10_names"
  
)

server <- function(input, output, session){
  # Function to create a data frame of top 10 names by sex and year 
  top_10_names <- function(){
    babynames %>% 
      filter(sex == input$sex) %>% 
      filter(year == input$year) %>% 
      slice_max(prop, n = 10)
  }
  # CODE BELOW: Render a table output named "table_top_10_names"
  
  
}

shinyApp(ui = ui, server = server)
Modifica ed esegui il codice