2020-03-03 08:24:24
![]()
library(shiny)
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
\[Y \sim X * \beta + N(0,\sigma)\]
or
\[ Y \sim X_0*\beta_0 + X_1*\beta_1 + N(0,\sigma)\]
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
sliderInput() functionsliderInput(inputId = "points", label = "Number of points:", min = 10, max = 200, value = 50)
sliderInput(inputId = "slope",
  label = "Slope:",
  min = -10,
  max = 10,
  value = 1)
      
sliderInput(inputId = "error",
  label = "Error:",
  min = .0001,
  max = 5,
  value = 0.5)
sliderInput function converts the slider position into a number
inputId of the UI elementinputId equal to the variable name(s) gets created: input$points, input$slope, …server(input, output) function takes the ui inputs and creates an outputoutput and shows it on the screenserver <- function(input, output) {}input$points, input$slope, and input$error to grab values from UIoutput$scatterPlotinput$points as xinput$slope + error
rnorm(input$points, sd = input$error)output$scatterPlot <- renderPlot({
    # Calculate x, y, with slope and error
    x = runif(input$points)
    
    # Vectorize x for point-wise multiplication
    y = rep(input$slope, input$points) * as.vector(x) + 
        rnorm(input$points, sd = input$error)
        
    # draw the histogram with the specified number of bins
    scatter.smooth(x = x, y = y, xlab = "x", ylab = "y")
})
library(shiny)
# Define UI for application that draws a scatterplot
ui <- fluidPage(
  
  # Application title
  titlePanel("Correlation Demo"),
  
  # Sidebar with a slider input for number of points 
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "points",
                  label = "Number of points:",
                  min = 10,
                  max = 200,
                  value = 50),
      sliderInput(inputId = "slope",
                  label = "Slope:",
                  min = -10,
                  max = 10,
                  value = 1),
      sliderInput(inputId = "error",
                  label = "Error:",
                  min = .0001,
                  max = 5,
                  value = 0.5)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput(outputId = "scatterPlot")
    )
  )
)
# Define server logic
server <- function(input, output) {
  
  output$scatterPlot <- renderPlot({
    
    # Calculate x, y, with slope and error
    x = runif(input$points)
    y = rep(input$slope, input$points) * as.vector(x) + rnorm(input$points, sd = input$error)
    
    # draw the plot
    scatter.smooth(x = x, y = y, xlab = "x", ylab = "y")
  })
}
shiny-templates/shiny-work.Rmd to see what the various examples do using rmarkdown::render("shiny-templates/shiny-work.Rmd").