Let's learn shiny together (3) --- add controls

What is shiny? Shiny is an R package that allows you to easily build interactive web applications (apps) directly from R. This series is a long tutorial, taking you to learn shiny from simple to deep.
insert image description here
In the previous section, we have introduced the page layout of shiny in the article "R Language Series Tutorial—Let's learn shiny together (2)". Today we will continue to introduce how to add controls in shiny and perform human machine interaction. Shiny provides many controls.
On the page side, all controls basically have a common character input+ID, and then connect to the front-end or back-end identifier. input+ID has two characteristics: 1. It must be unique in the same server and can not be repeated. 2. It must be a simple string containing only letters, numbers and underscores (spaces, dashes, periods or other special characters are not allowed!). Naming it is like naming a variable in R.
By type, shiny controls have text input type controls. Note that the three text types in the figure below are all different.

library(shiny)
## Warning: 程辑包'shiny'是用R版本4.2.2 来建造的
ui <- fluidPage(
  textInput("name", "What's your name?"),
  passwordInput("password", "What's your password?"),
  textAreaInput("story", "Tell me about yourself", rows = 3)
)

server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)
## 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.

insert image description here
For digital input type controls, the first numericInput is easy to understand, which is to input numbers. Although the second and third are both sliderInput, the second is to drag to display the number, and the third is to display a rate

ui <- fluidPage(
  numericInput("num", "Number one", value = 0, min = 0, max = 100),
  sliderInput("num2", "Number two", value = 50, min = 0, max = 100),
  sliderInput("rng", "Range", value = c(10, 20), min = 0, max = 100)
)

server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)

insert image description here
Date class input tags are generated by the function dateInput

ui <- fluidPage(
  dateInput("dob", "When were you born?"),
  dateRangeInput("holiday", "When do you want to go on vacation next?")
)

server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)

insert image description here
You can also fill in the data for the selected row, which must first list the options for selecting the data. radioButtons literally means radio buttons. The dropdown menus created by selectInput() take up the same space regardless of the number of options, which makes them better for longer options. Setting multiple = TRUE allows the user to select multiple elements.

ck <- c("列线图", "孟德尔随机化", "nhanes数据库", "时间序列分析", "其他")

ui <- fluidPage(
  selectInput("state", "What's your favourite state?", state.name),
  radioButtons("ck", "你喜欢公众号:零基础说科研哪些课程?", ck)
)

server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)

insert image description here
file upload control

ui <- fluidPage(
  fileInput("upload", NULL)
)
server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)

insert image description here
Operation control, click to go to a link

ui <- fluidPage(
  actionButton("click", "Click me!"),
  actionButton("drink", "Drink me!", icon = icon("cocktail"))
)

server <- function(input, output, session) {
    
    
  
}

shinyApp(ui, server)

insert image description here
This chapter briefly introduces some layouts of controls, and the next chapter will introduce how controls are output.

Guess you like

Origin blog.csdn.net/dege857/article/details/131969737