Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# | ||
# This is a Shiny web application. You can run the application by clicking | ||
# the 'Run App' button above. | ||
# | ||
# Find out more about building applications with Shiny here: | ||
# | ||
# http://shiny.rstudio.com/ | ||
# | ||
|
||
library(shiny) | ||
library(tidyverse) | ||
|
||
load("movies.RData") | ||
|
||
# Define UI for application that draws a histogram | ||
ui <- fluidPage( | ||
titlePanel("Movie browser, 1970 to 2014", windowTitle = "Movies"), | ||
sidebarLayout( | ||
sidebarPanel( | ||
checkboxGroupInput( | ||
inputId = "type", | ||
label = "Title type:", | ||
choices = levels(movies$title_type), | ||
selected = "Feature Film" | ||
) | ||
), | ||
mainPanel( | ||
tabsetPanel( | ||
type = "tabs", | ||
tabPanel("Plot", plotOutput("plot")), | ||
tabPanel("Summary", tableOutput("summary")), | ||
tabPanel("Data", DT::dataTableOutput("data")), | ||
tabPanel( | ||
"Reference", tags$p( | ||
"There data were obtained from", | ||
tags$a("IMDB", href = "http://www.imdb.com/"), "and", | ||
tags$a("Rotten Tomatoes", href = "https://www.rottentomatoes.com/"), "." | ||
), | ||
tags$p("The data represent ", nrow(movies), "randomly sampled movies released between 1972 to 2014 in the Unites States.") | ||
) | ||
) | ||
) | ||
) | ||
) | ||
|
||
# Define server logic required to draw a histogram | ||
server <- function(input, output) { | ||
|
||
load("movies.RData") | ||
|
||
movies_subset <- reactive({ | ||
movies %>% | ||
filter(title_type %in% input$type) | ||
}) | ||
|
||
output$plot <- renderPlot({ | ||
ggplot(movies_subset(), aes(x = critics_score, y = audience_score, color = mpaa_rating)) + | ||
geom_point() | ||
}) | ||
output$summary <- renderTable( | ||
{ | ||
movies_subset() %>% | ||
group_by(mpaa_rating) %>% | ||
summarise( | ||
mean_as = mean(audience_score), sd_as = sd(audience_score), | ||
mean_cs = mean(critics_score), sd_cs = sd(critics_score), | ||
n = n(), cor = cor(audience_score, critics_score) | ||
) | ||
}, | ||
digits = 3 | ||
) | ||
output$data <- DT::renderDataTable({ | ||
movies_subset() %>% | ||
DT::datatable(options = list(pageLength = 10), rownames = FALSE) | ||
}) | ||
|
||
} | ||
|
||
# Run the application | ||
shinyApp(ui = ui, server = server) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# | ||
# This is a Shiny web application. You can run the application by clicking | ||
# the 'Run App' button above. | ||
# | ||
# Find out more about building applications with Shiny here: | ||
# | ||
# http://shiny.rstudio.com/ | ||
# | ||
|
||
library(shiny) | ||
library(tidyverse) | ||
|
||
load("movies.RData") | ||
|
||
# Define UI for application that draws a histogram | ||
ui <- fluidPage( | ||
titlePanel("Movie browser, 1970 to 2014", windowTitle = "Movies"), | ||
sidebarLayout( | ||
sidebarPanel( | ||
checkboxGroupInput( | ||
inputId = "type", | ||
label = "Title type:", | ||
choices = levels(movies$title_type), | ||
selected = "Feature Film" | ||
) | ||
), | ||
mainPanel( | ||
navlistPanel( | ||
tabPanel("Plot", plotOutput("plot2")), | ||
tabPanel("Summary", tableOutput("summary2")), | ||
tabPanel("Data", DT::dataTableOutput("data2")), | ||
tabPanel( | ||
"Reference", | ||
tags$p( | ||
"There data were obtained from", | ||
tags$a("IMDB", href = "http://www.imdb.com/"), "and", | ||
tags$a("Rotten Tomatoes", href = "https://www.rottentomatoes.com/"), "." | ||
), | ||
tags$p("The data represent", nrow(movies), "randomly sampled movies released between 1972 to 2014 in the United States.") | ||
) | ||
) | ||
) | ||
) | ||
) | ||
|
||
# Define server logic required to draw a histogram | ||
server <- function(input, output) { | ||
|
||
file <- "https://github.com/rstudio-education/shiny-course/raw/main/movies.RData" | ||
destfile <- "movies.RData" | ||
|
||
download.file(file, destfile) | ||
|
||
load("movies.RData") | ||
|
||
movies_subset <- reactive({ | ||
movies %>% | ||
filter(title_type %in% input$type) | ||
}) | ||
|
||
movies_subset2 <- reactive({ | ||
movies %>% | ||
filter(title_type %in% input$type) | ||
}) | ||
|
||
output$plot2 <- renderPlot({ | ||
ggplot(movies_subset(), aes(x = critics_score, y = audience_score, color = mpaa_rating)) + | ||
geom_point() | ||
}) | ||
output$summary2 <- renderTable( | ||
{ | ||
movies_subset2() %>% | ||
group_by(mpaa_rating) %>% | ||
summarise( | ||
mean_as = mean(audience_score), sd_as = sd(audience_score), | ||
mean_cs = mean(critics_score), sd_cs = sd(critics_score), | ||
n = n(), cor = cor(audience_score, critics_score) | ||
) | ||
}, | ||
digits = 3 | ||
) | ||
output$data2 <- DT::renderDataTable({ | ||
movies_subset2() %>% | ||
DT::datatable(options = list(pageLength = 10), rownames = FALSE) | ||
}) | ||
|
||
} | ||
|
||
# Run the application | ||
shinyApp(ui = ui, server = server) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# This is a Shiny web application. You can run the application by clicking | ||
# the 'Run App' button above. | ||
# | ||
# Find out more about building applications with Shiny here: | ||
# | ||
# http://shiny.rstudio.com/ | ||
# | ||
|
||
library(shiny) | ||
|
||
# Define UI for the app | ||
ui <- fluidPage( | ||
titlePanel("Random number generator"), | ||
sidebarLayout( | ||
sidebarPanel( | ||
selectInput( | ||
"digit_type", "Number of digits:", | ||
c("any", "selected") | ||
), | ||
conditionalPanel( | ||
condition = "input.digit_type == 'selected'", | ||
sliderInput("digit_count", "How many digits?", | ||
min = 1, max = 10, value = 4 | ||
) | ||
), | ||
actionButton("go", "Generate new random number"), | ||
width = 5 | ||
), | ||
mainPanel( | ||
br(), | ||
"Your random number is", | ||
h4(textOutput("random_number")), | ||
width = 7 | ||
) | ||
) | ||
) | ||
|
||
# Define server logic required to draw a histogram | ||
server <- function(input, output) { | ||
|
||
output$random_number <- renderText({ | ||
input$go | ||
raw <- runif(1) | ||
digits <- if (input$digit_type == "any") { | ||
sample(1:10, size = 1) | ||
} else { | ||
input$digit_count | ||
} | ||
round(raw * 10^digits) | ||
}) | ||
} | ||
|
||
# Run the application | ||
shinyApp(ui = ui, server = server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Hello Shiny: Server function | ||
# Shiny app | ||
# Practice: Matching inputs and outputs | ||
|
||
library(shiny) | ||
|
||
ui <- fluidPage( | ||
|
||
textInput( | ||
inputId = "custom_text", | ||
label = "Input some text here:" | ||
), | ||
|
||
strong("Text is shown below:"), | ||
|
||
textOutput(outputId = "user_text") | ||
|
||
) | ||
|
||
server <- function(input, output, session){ | ||
|
||
output$user_text <- renderText({ input$custom_text }) | ||
|
||
} | ||
|
||
shinyApp(ui = ui, server = server) |