Rshiny basic functions (interactive web application)

Rshiny basic functions

  • introduction
  • Shiny package download
  • Composition of shinyApp
  • ui design function
    • HTML analogy
    • The img function cannot load the image
    • control function
    • output object of ui design
  • server
  • Summarize

Introduction

The R language shiny package can help us easily create interactive web applications, and can be built as an independent desktop executable program, which is very convenient to create and share. It can be used Data analysts carry out work reports, report making, etc.!

Download of shiny package

> install. packages('shiny')

Composition of shinyApp

The composition of shinyApp:
1. User interactive script-ui
2. Server running script-server

> library(shiny)
> ui <- ...

> server <- ...

> shinyApp(ui, server)

UI design function

HTML analogy

shiny function function
p paragraph text
h1 Level 1 heading
h2 Second level heading
h3 Third-level heading
h4 Level 4 heading
h5 Level 5 headings
h6 Sixth level heading
a hyperlink
br New Line
div Unified text division< /strong>
span Unity
pre Predefined Format Text
code Computer code block
img Image
strong Bold
em Italic
HTML Pass the string directly as HTML code
>ui <- fluidPage(
 + titlePanel("My Shiny App"),
 + sidebarLayout(
 + sidebarPanel(),
 + mainPanel(
 + h1("First level title"),
 + h2("Second level title"),
 + h3("Third level title"),
 + h4("Fourth level title"),
 + h5("Fifth level title"),
 + h6("Sixth level title")
 + )
 + )
 + )
>
> server = function(input, output){<!-- --> }
>
> shinyApp(ui, server)

>ui <- fluidPage(
 + titlePanel("My Shiny App"),
 + sidebarLayout(
 + sidebarPanel(),
 + mainPanel(
 + p("p creates a paragraph of text."),
 + p("A new p() command starts a new paragraph. Supply a style attribute
 + to change the format of the entire paragraph.", style = "font-family:
 + 'times'; font-si16pt"),
 + strong("strong() makes bold text."),
 + em("em() creates italicized (i.e, emphasized) text."),
 + br(),
 + code("code displays your text similar to computer code"),
 + div("div creates segments of text with a similar style. This division
 + of text is all blue because I passed the argument 'style = color:blue' to
 + div", style = "color:blue"),
 + br(),
 + p("span does the same thing as div, but it works with",
 + span("groups of words", style = "color:blue"),
 + "that appear inside a paragraph.")
 + )
 + )
 + )
>
> server = function(input, output){<!-- --> }
>
> shinyApp(ui, server)

The img function cannot load the image

img The common usage of the function is: img(src = '.png',height = ,width = ), After my own test, this method cannot be used in RGui, so I suggest you use it in Rstudio.

The following is how to use the img function in Rstudio.


Figure 1 Step 1

Picture 2 The second step

In the second step of the picture, the root directory E:\shinyApp is selected to create a folder know, which needs to be in know folder must add a folder named www, put the pictures to be displayed into Under the www folder
In the second step of the picture, we choose to create the app.R file, and write the ui, server, and shinyApp functions we need into app. R file, then click to save the runApp option, you will be prompted whether to save the app.R file, click Save Can.

According to Rstudio analysis of RGui the cause of the error, the operation in Rstudio is:

> runApp('~/know')

Successful screenshot:

Control function

function function
actionButton Operation options
checkboxInput single selection
dateInput date input
dateRangeInput date range
fileInput file selection td>
helpText Add instructions for other controls
numericInput Numerical input
radioButtons Radio buttons
checkboxGroupInput Multiple choice buttons
selectInput Box with options
sliderInput Smooth click Select box
submitButton Submit button
textInput Enter text Field

Picture display of each control function:

Output object of ui design

Add R object in ui.

output function in ui output type
dataTableOutput data table
htmlOutput native html
imageOutput Picture output
plotOutput Picture output in R
tableOutput Table output: small matrix and data section
textOutput text output
uiOutput Native html
verbatimTextOutput Text output
>ui <- fluidPage(
 + titlePanel("Hello Shiny!"),
 + sidebarLayout(
 + sidebarPanel(
 + sliderInput(inputId = "bins",
 + label = "Number of bins:",
 + min = 1,
 + max = 50,
 + value = 30)
 + ),
 + mainPanel(
 + plotOutput('plot'))
 + )
 + )

The above is an output example of the ui design object: plotOutput.

server

The server runs the script to provide R code to support the output object in the ui.
The render* function converts the object generated by the R code into the output object in the ui.

render* object
renderDataTable data table
renderImage Picture
renderPlot plot in R
renderPrint The output that needs to be printed
renderTable Small tables: data frames, matrices
renderText Text
renderUI HTML

Summary

This is the end of the basic and simple use of Rshiny. The basic functions listed in this article are all the basic functions of Rshiny to develop interactive web. Wanted web.
In addition, there are hundreds of functions about shiny, I can’t count them all in a blog, but I will show some functions according to my needs in future blogs use.
Finally, because I prefer RGui, I have been using it to make my own shinyApp, but in some cases It is extremely inconvenient, so I recommend everyone to use Rstudio here!
thank you all! ! !
Good night! ! !