To create a comprehensive guide on using PickModel, Campaigned, and DropList in R Shiny, we'll cover the key concepts, provide detailed context, and offer examples for each component.
PickModel
PickModel is a flexible Shiny module for creating user-friendly interactive models. It simplifies the process of creating complex models by breaking them down into manageable steps.
Installation
First, install the shinyPickModel package:
install.packages("shinyPickModel")
Usage
Here's a basic example of using PickModel:
# Load the necessary packages
library(shiny)
library(shinyPickModel)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Interactive Linear Regression"),
sidebarLayout(
sidebarPanel(
pickModelInput("x", "Select x variable:", choices = c("A", "B", "C")),
pickModelInput("y", "Select y variable:", choices = c("A", "B", "C"))
),
mainPanel(
plotOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
# Load data
data(mtcars)
# Select variables based on user input
x <- input$x
y <- input$y
# Fit linear regression model
model <- lm(as.formula(paste(y, "~", x)), data = mtcars)
# Plot data and model
plot(mtcars[[x]], mtcars[[y]], main = "Linear Regression", xlab = x, ylab = y)
abline(model, col = "red")
})
}
# Run the application
shinyApp(ui = ui, server = server)
In this example, users can select the x and y variables for a linear regression model.
Campaigned
Campaigned is a Shiny module for creating multi-page applications with ease. It provides a simple and consistent way to structure multi-page applications, making them more manageable and maintainable.
Installation
First, install the shinyCampaigned package:
install.packages("shinyCampaigned")
Usage
Here's a basic example of using Campaigned:
# Load the necessary packages
library(shiny)
library(shinyCampaigned)
# Define UI for application
ui <- campaignUI(
pageTitle = "My Multi-page App",
pages = list(
Page1 = pageWithSidebar(
header = "Page 1",
sidebarPanel(
textInput("text1", "Enter text 1:")
),
mainPanel(
textOutput("text1_output")
)
),
Page2 = pageWithSidebar(
header = "Page 2",
sidebarPanel(
textInput("text2", "Enter text 2:")
),
mainPanel(
textOutput("text2_output")
)
)
)
)
# Define server logic
server <- function(input, output, session) {
output$text1_output <- renderText({
input$text1
})
output$text2_output <- renderText({
input$text2
})
}
# Run the application
shinyApp(ui = ui, server = server)
In this example, users can navigate between two pages, each with a text input and output.
DropList
DropList is a Shiny module for creating interactive dropdown lists with customizable options. It allows for more flexible and visually appealing dropdown menus.
Installation
First, install the shinyDropList package:
install.packages("shinyDropList")
Usage
Here's a basic example of using DropList:
# Load the necessary packages
library(shiny)
library(shinyDropList)
# Define UI for application
ui <- fluidPage(
titlePanel("DropList Example"),
sidebarLayout(
sidebarPanel(
dropListInput("colors", "Select a color:", choices = c("Red", "Green", "Blue"))
),
mainPanel(
plotOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
# Load data
data(iris)
# Filter data based on user input
if (input$colors == "Red") {
iris_red <- iris[iris$Species == "setosa", ]
} else if (input$colors == "Green") {
iris_green <- iris[iris$Species == "versicolor", ]
} else {
iris_blue <- iris[iris$Species == "virginica", ]
}
# Plot data
plot(iris_red, main = "Iris Data - Setosa")
})
}
# Run the application
shinyApp(ui = ui, server = server)
In this example, users can select a color, and the application filters the iris dataset based on the selected color, displaying a plot of the corresponding species.