A Web server is needed to run shiny apps, shinyapps.io can be used for free (only up to 5 apps) or there is a way to create your own server called shiny server. https://rstudio.com/products/shiny/shiny-server/
You need to make a user interface (UI) and server instructions which tell the server what to do when inputs are changed. Both these instructions can be in the same document.
The UI generates html, for the web page, from R code
The two sections of code are then knitted together
Start with a template; in Rstudio you can choose ‘new file’, ‘Shiny Web App’
library(shiny)
ui <- fluidPage()
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Inputs: what people change
Outputs: what the user sees when they change inputs
To add elements to an app we add to fluidPage()
Adding input functions
sliderInput
for example generates some html code to display the code.
Shiny provides a few input functions which can be used
inputId = name
for the input
label =
the label to display, which can be used to instruct users how to use the input
We can use the values stored in our input using input$inputID
. This is useful for when we are formatting the output, we can make the output depend on the input
Save the output which we want to build to output$hist
for example
Build the output with the render function (helps assemble the code into html)
Access input values with input$
When inside a render function such as renderPlot the code is reactive (reacts to changes in the input variables)
Save into one directory with app.R as the app name, as well as datasets images etc needed to run the app
Sign up for an account on shinyapps.io. Follow all instructions their (install rsconnect
, etc.)
Create a shiny app in rstudio.
Then you can publish straight to shinyapps.io (see below image). Make sure to publish all the files needed to run the R script inside the app
Code for embedding video shiny:
### Code for embedding video
library(shiny)
ui <- fluidPage(
HTML('')
)
server <- function(input, output, session) {
}
#shinyApp(ui, server)
Code for question and answer shiny:
### Code for question and answer
library(shiny)
ui <- fluidPage(
radioButtons("rb", "What is quantity demanded equal to?",
choiceNames =
list("None selected", "Supply", "quantity demanded",
"Price", "quantity supplied"),
choiceValues =
list("", "supply", "quantity demanded", "price", "quantity"),
selected = ""
),
textOutput("txt")
)
server <- function(input, output) {
output$txt <- renderText({
if (input$rb == "supply"){
print("You are correct")
} else if (input$rb == ""){
print("")}
else{
print("You are incorrect")}
})
}
#shinyApp(ui, server)