Типи виводу

Створення панелей керування з shinydashboard

Png Kee Seng

Researcher

Функції output і render

  • У shinyApp є вихідні елементи
  • Для кожного виводу потрібно викликати пару функцій

    • Перша — функція в UI
    • Вона визначає, де розмістити вивід
    • Зазвичай така функція має вигляд:
      <type>output(<identifying label>, ...)
      
  • Другий фрагмент коду визначається всередині функції server

    • Він має вигляд:
      output$<identifying label> <- render<type>(...)
      
Створення панелей керування з shinydashboard

Приклад: дослідження сну

  • Спочатку імпортуймо набір даних
    library(tidyverse)
    sleep <- read_csv("../data/Time Americans Spend Sleeping.csv")  
    
  • Ось стовпці
    $ index                   : num [1:945] 0 1 2 3 4  ...
    $ Year                    : num [1:945] 2003 2004 2005 2006 2007 ...
    $ Period                  : chr [1:945] "Annual" "Annual" "Annual" "Annual" ...
    $ Avg hrs per day sleeping: num [1:945] 8.57 8.55 8.62 8.63  ...
    $ Standard Error          : num [1:945] 0.018 0.026 0.023 0.024  ...
    $ Type of Days            : chr [1:945] "All days" "All days" "All days" ...
    $ Age Group               : chr [1:945] "15 years and over" "15 years and over" ...
    $ Activity                : chr [1:945] "Sleeping" "Sleeping" "Sleeping" ...
    $ Sex                     : chr [1:945] "Both" "Both" "Both" ...
    
Створення панелей керування з shinydashboard

textOutput

  • Спочатку розмістімо textOutput в UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) {
}

shinyApp(ui, server)
Створення панелей керування з shinydashboard

renderText

  • Щоб відобразити текст, додайте renderText у server
  • Знадобиться нотація output$
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) { output$textlabel <- renderText("Sleep deprivation is a serious health issue in many major cities around the world.") }
shinyApp(ui, server)
Створення панелей керування з shinydashboard

Відтворений textOutput

демо текстового виводу без взаємодій.

Створення панелей керування з shinydashboard

textOutput із взаємодіями

  • Щоб взаємодіяти між введеннями та виводами, звертайтесь до інпутів через input$
input$<type>
Створення панелей керування з shinydashboard

textOutput із взаємодіями

  • Для прикладу розгляньмо текстове поле введення.
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

textInput("textlabel", "Tell me your name", value = "", placeholder = "Don"),
textOutput("textoutlabel")) server <- function(input, output) { output$textoutlabel <- renderText(paste0("Hello ",
input$textlabel,
"Sleep deprivation is a serious health issue in many major cities around the world.")) } shinyApp(ui, server)
Створення панелей керування з shinydashboard

textOutput із взаємодіями

текстовий вивід із взаємодіями.

Створення панелей керування з shinydashboard

plotOutput

  • Розмістіть plotOutput() в UI
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

plotOutput("plotlabel"))
server <- function(input, output) { } shinyApp(ui, server)
Створення панелей керування з shinydashboard

renderPlot

  • Додайте renderPlot() у server()
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  plotOutput("plotlabel"))

server <- function(input, output) {

output$plotlabel <- renderPlot({ ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
Створення панелей керування з shinydashboard

Відтворений plotOutput

демо plotoutput.

Створення панелей керування з shinydashboard

plotOutput із selectInput

  • Введення користувача можуть давати різні результати
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  selectInput("selectlabel", 
              "Select an option",
              choices = c("Histogram", "Boxplot")),
  plotOutput("plotlabel"))
Створення панелей керування з shinydashboard

plotOutput із selectInput

server <- function(input, output) {
  output$plotlabel <- renderPlot({
    if (input$selectlabel == "Histogram"){

ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`))
} else if (input$selectlabel == "Boxplot"){
ggplot(sleep) + geom_boxplot(aes(x = `Avg hrs per day sleeping`))
} }) } shinyApp(ui, server)
Створення панелей керування з shinydashboard

plotOutput із selectInput

Демо plotOutput із selectInput

Створення панелей керування з shinydashboard

plotOutput із sliderInput

  • Розгляньмо інший приклад із sliderInput
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

sliderInput("sliderlabel", "Average hours of sleep", min = 7.5, max = 11, value = c(7.5, 11), step = 0.02),
plotOutput("plotlabel")) server <- function(input, output) {
output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
Створення панелей керування з shinydashboard

plotOutput із sliderInput

Демо plotoutput із sliderinput

Створення панелей керування з shinydashboard

Іконки в shiny

  • shiny також має іконки, які можна використовувати в shinyApp

Деякі іконки в shiny

1 https://fontawesome.com/icons
Створення панелей керування з shinydashboard

Іконки в shiny: приклад

  • Використовуйте icon("...")
  • Згадайте попередній приклад
  • Напр., icon("bed")
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  sliderInput("sliderlabel",

list(icon("bed"),
"Select an option:"), min = 7.5, max = 11, value = c(7.5, 11), step = 0.02), plotOutput("plotlabel")) server <- function(input, output) { output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) }) } shinyApp(ui, server)
Створення панелей керування з shinydashboard

Застосунок з іконкою

Демо іконки

Створення панелей керування з shinydashboard

Давайте потренуємось!

Створення панелей керування з shinydashboard

Preparing Video For Download...