public bigdata

[datacamp] Building Web Application with Shiny in R 본문

R programming

[datacamp] Building Web Application with Shiny in R

public bigdata 2020. 4. 22. 23:37

Part1

 

1. Parts of Shiny app

library(shiny)

ui <- fluidPage()

server <- function(input,
				   output,
                   session){
                   }
                   
shinyApp(ui = ui, server = server)
  1. Load shiny
  2. Create the UI with a HTML function
  3. Define a custom function to create the server
  4. Run the app

간단한 예제 하나

library(shiny)

ui <- fluidPage(
  "Hello, world!!!"
)
server <- function(input, output, session){
  
}

shinyApp(ui = ui, server = server)

2. Ask a question(with an input!)

library(shiny)

ui <- fluidPage(
  textInput(inputId = "name", label = "Enter a name : "),
  textOutput("q")
)

server <- function(input, output){
  output$q <- renderText({
    paste("Do you prefer dogs or cats, ", input$name, "?")
  })
}

shinyApp(ui=ui, server = server)

3. Building a babynames explorer Shiny app

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel("Baby Name Explorer"), 
  sidebarLayout(                            
    sidebarPanel(
      textInput('name', 'Enter Name :'),
      textOutput('textoutput1')
    ),
    mainPanel({
      plotOutput('trend')
    })
  )
)

server <- function(input, output, session){
  output$textoutput1 <- renderText({
    paste0(input$name, ' is so SeXy Guy')
  })
  output$trend <- renderPlot({
    ggplot()
  })
}

shinyApp(ui=ui, server = server)

sidebarLayout이 있기 때문에 아래처럼 titlePanel 및에 sidebarPanel, mainPanel이 좌우로 배치된다.

위 예제에서 조금 변형하여 iris 데이터의 Species를 입력하면, 해당하는 아이리스 꽃의 Sepal 정보가 plot 되도록 구성해 보았다.

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel("ILIS Data Explorer"),
  sidebarLayout(
    sidebarPanel(
      textInput('species', 'Enter iris species'),
      textOutput('textoutput1')
    ),
    mainPanel({
      plotOutput('trend')
    })
  )
)

server <- function(input, output, session){
  output$textoutput1 <- renderText({
    paste0(input$species, ' is beautiful Flower!')
  })
  output$trend <- renderPlot({
    iris_data <- iris %>% filter(Species==input$species)
    ggplot(iris_data) +
      geom_point(aes(Sepal.Width, Sepal.Length))
  })
}


part2

 

1. Where to use inputs

library(shiny)
library(tidyverse)


help("checkboxInput")

ui <- fluidPage(
  textInput('name', 'Enter a name:'),
  selectInput('animal', 'Dogs or cats?', choices = c("dogs", "cats")),
  textOutput("greeting"),
  textOutput('answer')
)

server <- function(input, output, session){
  output$greeting <- renderText({
    paste('Do you prefer dogs or cats, ', input$name, "?")
  })
  output$answer <- renderText({
    paste('I prefer', input$animal, "!")
  })
  
}
shinyApp(ui = ui, server = server)

2. Other render functions

  • renderTable()
  • renderImage()
  • renderPlot()

3. Other output functions

  • tableOutput() or dataTableOutput
  • imageOutput()
  • plotOutput()

4. Non-shiny output and render functions

library(shiny)
library(tidyverse)


ui <- fluidPage(
  DT::DTOutput('babynames_table')
)

server <- function(input, output, session){
  output$babynames_table <- DT::renderDT({
    iris
  })
}

shinyApp(ui=ui, server = server)

4. Non-shiny output and render functions - Example

library(shiny)
library(tidyverse)


ui <- fluidPage(
  titlePanel("Select iris Species"),
  selectInput('species', 'Select species', choices = c("setosa", "versicolor", "virginica")),
  # CODE BELOW: Add table output named "table_top_10_names"
  DT::DTOutput('table_top_10_iris')
)

server <- function(input, output, session){
  # Function to create a data frame of top 10 names by sex and year 
  top_10_iris <- function(){
    top_10_iris <- iris %>% 
      filter(Species == input$species)
  }
  # CODE BELOW: Render a table output named "table_top_10_names"
  output$table_top_10_iris <-DT::renderDT({
    top_10_iris()
  })
}
shinyApp(ui = ui, server = server)

plotly 함수를 이용한 output도 가능하다.

 

5. Default Shiny app layout

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel('Histogram'),
  sliderInput('nb_bins', 'select ggplot bins', 5, 10, 5),
  plotOutput('hist')
)

server <- function(input, output, session){
  output$hist <- renderPlot({
    ggplot(iris, aes(x = Sepal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
}

shinyApp(ui = ui, server = server)

6. Sidebar layout

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel('Histogram'),
  sidebarLayout(
    sidebarPanel(sliderInput('nb_bins', 'select ggplot bins', 5, 10, 5)),
    mainPanel(plotOutput('hist'))
  )
)

server <- function(input, output, session){
  output$hist <- renderPlot({
    ggplot(iris, aes(x = Sepal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
}

shinyApp(ui = ui, server = server)

7. Tab layout

library(shiny)
library(tidyverse)

ui <- fluidPage(
  titlePanel("Histogram"),
  sidebarLayout(
    sidebarPanel(sliderInput('nb_bins', 'select bins', 5, 10, 5)),
    mainPanel(
      tabsetPanel(
        tabPanel('Waiting',
                 plotOutput('Wating_plot')),
        tabPanel('Eruptions',
                 plotOutput('Eruption_plot'))
      )
    )
  )
)

server <- function(input, output, session){
  output$Wating_plot <- renderPlot({
    ggplot(iris, aes(Sepal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
  output$Eruption_plot <- renderPlot({
    ggplot(iris, aes(Petal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
}

shinyApp(ui = ui, server = server)

8. Theme selector

library(shiny)
library(tidyverse)
library(shinythemes)
ui <- fluidPage(
  titlePanel("Histogram"),
  shinythemes::themeSelector(), # theme = shinytheme('superhero') : 이렇게도 지정 가능
  sidebarLayout(
    sidebarPanel(sliderInput('nb_bins', 'select bins', 5, 10, 5)),
    mainPanel(
      tabsetPanel(
        tabPanel('Waiting',
                 plotOutput('Wating_plot')),
        tabPanel('Eruptions',
                 plotOutput('Eruption_plot'))
      )
    )
  )
)

server <- function(input, output, session){
  output$Wating_plot <- renderPlot({
    ggplot(iris, aes(Sepal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
  output$Eruption_plot <- renderPlot({
    ggplot(iris, aes(Petal.Width))+
      geom_histogram(bins = input$nb_bins)
  })
}

shinyApp(ui = ui, server = server)

9. Building Shiny apps: 4 steps

  • 1. Add input(UI)
  • 2. Add output(ui/Server)
  • 3. Update layout(ui)
  • 4. Update outputs(Server)