R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Shiny is an R package that makes it easy to build interactive web applications straight from R. With Shiny, you can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards.
Here's a basic tutorial on how to get started with Shiny in R:
If you haven't installed Shiny yet:
install.packages("shiny")
Load the Shiny package:
library(shiny)
A Shiny app has two main parts:
These components are then passed to the shinyApp()
function.
Here's a simple example of a Shiny app. This app will allow the user to input a number, and the server will compute its square and display it.
# Define UI ui <- fluidPage( titlePanel("Square Calculator"), sidebarLayout( sidebarPanel( numericInput("num", label = "Enter a number:", value = 1), actionButton("compute", "Compute Square") ), mainPanel( textOutput("result") ) ) ) # Define server logic server <- function(input, output) { observeEvent(input$compute, { output$result <- renderText({ paste("The square of", input$num, "is:", input$num^2) }) }) } # Combine the UI and server to make the app shinyApp(ui = ui, server = server)
If you've placed the above code in an R script, simply source the script to run the app:
source("path_to_your_script.R")
Alternatively, you can save the code in an app.R file and simply click the "Run App" button in RStudio.
For sharing your Shiny apps with others, consider the following:
Shiny has various advanced features like reactive expressions, dynamic UI generation, integration with leaflet
for maps, integration with DT
for data tables, and much more.
Remember, while Shiny is powerful, it also requires a good understanding of R's reactive programming principles to leverage its full power. Consider going through tutorials, workshops, and online courses to deepen your knowledge.
Shiny App Layout and Structure:
# UI ui <- fluidPage( titlePanel("My Shiny App"), sidebarLayout( sidebarPanel( # Sidebar content ), mainPanel( # Main content ) ) ) # Server server <- function(input, output) { # Server logic } shinyApp(ui, server)
Interactive Data Visualization with Shiny:
output$plot <- renderPlot({ # Code for generating dynamic plot based on user input })
Reactive Programming in Shiny:
reactive_data <- reactive({ # Reactive code based on user input })
Customizing Shiny App Appearance:
fluidPage( theme = shinytheme("cerulean"), # Other UI components )
Shiny Dashboard Design in R:
library(shinydashboard) dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody() )
Shiny Modules in R Programming:
# UI module my_ui_module <- function(id) { ns <- NS(id) # UI code } # Server module my_server_module <- function(input, output, session) { # Server code }
Shiny UI and Server Functions:
ui <- fluidPage( # UI components ) server <- function(input, output) { # Server logic }
Shiny Input and Output Widgets:
sliderInput("slider", "Select a value:", min = 1, max = 100, value = 50)
Handling User Input in Shiny Apps:
observe({ # Code to react to user input })