0% found this document useful (0 votes)
16 views

R Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

R Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab exercises

1. Modify the app.R code to use a sliderInput() function for “Cluster count”.

library(shiny)
library(ggplot2)
library(DT)
ui <- fluidPage(
titlePanel("Enhanced Shiny App"),
sidebarLayout(
sidebarPanel(
sliderInput("num", "Choose a number", value = 25, min = 1, max = 100),
sliderInput("clusters", "Cluster count:", min = 1, max = 9, value = 3),
sliderInput("sd", "Standard Deviation:", min = 0.1, max = 3, value = 1),
checkboxGroupInput("xvars", "Choose X Variable:", choices = names(iris))
),
mainPanel(
plotOutput("hist"),
DTOutput("table")
)
)
)

server <- function(input, output) {


output$hist <- renderPlot({
x <- rnorm(input$num, sd = input$sd)
ggplot(data.frame(x = x), aes(x = x)) +
geom_histogram(bins = input$num, fill = "darkgray", color = "white") +
labs(title = "Histogram using ggplot2", x = "Value", y = "Frequency")
})

output$table <- renderDT({


data.frame(
Variable = names(iris),
Value = sapply(iris, function(x) mean(x, na.rm = TRUE))
)
})
}

shinyApp(ui = ui, server = server)

2. Modify the app.R code to use a checkboxGroupInput() function for the X Variable

Modified app.R Code

library(shiny)
library(ggplot2)
library(DT)

ui <- fluidPage(
titlePanel("Enhanced Shiny App"),
sidebarLayout(
sidebarPanel(
sliderInput("num", "Choose a number", value = 25, min = 1, max = 100),
sliderInput("clusters", "Cluster count:", min = 1, max = 9, value = 3),
sliderInput("sd", "Standard Deviation:", min = 0.1, max = 3, value = 1),
checkboxGroupInput("xvars", "Choose X Variable:", choices = names(iris), selected =
names(iris)[1])
),
mainPanel(
plotOutput("hist"),
DTOutput("table")
)
)
)

server <- function(input, output) {


output$hist <- renderPlot({
x <- rnorm(input$num, sd = input$sd)
ggplot(data.frame(x = x), aes(x = x)) +
geom_histogram(bins = input$num, fill = "darkgray", color = "white") +
labs(title = "Histogram using ggplot2", x = "Value", y = "Frequency")
})

output$table <- renderDT({


data.frame(
Variable = input$xvars,
Value = sapply(iris[, input$xvars, drop = FALSE], function(x) mean(x, na.rm = TRUE))
)
})
}

shinyApp(ui = ui, server = server)


Instructions
Open your app.R file in RStudio.
Replace the existing content with the modified code provided above.
Save the file and run it using the Run App button in RStudio or by using the shiny::runApp()
function.
3- Modify the 02-hist-app.R code to create a histogram from the gg plot2 library instead of
using hist() from base graphics

Modified Code
library(shiny)
library(ggplot2)

ui <- fluidPage(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
plotOutput("hist")
)

server <- function(input, output) {


output$hist <- renderPlot({
data <- data.frame(x = rnorm(input$num))
ggplot(data, aes(x = x)) +
geom_histogram(fill = "darkgray", color = "white", bins = 30) +
labs(title = "Histogram using ggplot2", x = "Value", y = "Frequency")
})
}

shinyApp(ui = ui, server = server)

Screenshot
Added `library(ggplot2)`: This ensures the `ggplot2` package is loaded.
Changed `renderPlot` to use `ggplot2`:
Created a data frame from the generated random numbers.
Used `ggplot()` to create the histogram with `geom_histogram()`.

1. Modify the 02-hist-app.R code by adding an additional sliderInput for the standard
deviation for your normal random variables (hint: ?rnorm).

2. Modify the 02-hist-app.R code to create a table of output values using


renderDataTable()

library(shiny)
library(ggplot2)
library(DT)

ui <- fluidPage(
titlePanel("Enhanced Histogram App"),
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
sliderInput(inputId = "sd",
label = "Standard Deviation",
value = 1, min = 0.1, max = 3, step = 0.1)
),
mainPanel(
plotOutput("hist"),
DTOutput("table")
)
)
)

server <- function(input, output) {


# Reactive expression to generate random data
data <- reactive({
rnorm(input$num, mean = 0, sd = input$sd)
})

output$hist <- renderPlot({


data_frame <- data.frame(x = data())
ggplot(data_frame, aes(x = x)) +
geom_histogram(fill = "darkgray", color = "white", bins = 30) +
labs(title = "Histogram using ggplot2", x = "Value", y = "Frequency")
})

output$table <- renderDT({


data_frame <- data.frame(Values = data())
datatable(data_frame)
})
}

shinyApp(ui = ui, server = server)

You might also like