library(shiny)
Building a “Hello world shinyApp”
We begin to demonstrate the building blocks of a shinyApp.
An App needs a *User interface (ui)* and a server. The majic about the *shiny package* is that it can create both of this within R, plus run your app using an additionational shiny function.
First,
- load the library using the shiny.
- Create ui using the html function
<- fluidPage() ui
- Define a custom function to create the server
<- function(input,
server
output,
session){
}
- finally run your app.
shinyApp(ui = ui, server = server)
Example1 of shiny app
library(shiny)
library(widgetframe)
<- fluidPage(
ui
"Hello, world!!!!!!"
)
<- function(input,
server
output,
session){
}
shinyApp(ui = ui, server = server)
Example2: Add a question
We want to go an extra mile an add a text that asks a question. This is possible but adding *textinput* function that allows us to enter text. It has three arguments, a unique ID that will be used to refer to this input, a label that is displayed to the user and an optional default value.
Our full out put that is diplayed is contained in the server using the render text function. Inside of that you can use *paste* to create a longer character string. And if add *input$name* you can access the name added using text input. The text is assigned to an output object that will be used in the ui to display.
library(shiny)
library(widgetframe)
<- fluidPage(
ui
textInput("name", "Enter your name:"),
textOutput("r")
)
<- function(input, output){
server
$r <- renderText({
output
paste0("Do you prefer rain or sunshine,", input$name, "?")
})
}
shinyApp(ui = ui, server = server)
You did it a text that uses a text input!!