Shiny application

Shiny is an R package that allows us to build interactive web applications directly from R. In Order to create a simple shiny app we need to have an `app.R` file that contains ui and server. The UI (User Interface) controls the looks and experience of the user and the server function defines the server logic of the Shiny app which controls what is being rendered in the application screen. To install it, type in R console the following:-

install.packages("shiny")

R




library(shiny)
library(plotly)
library(DT)
 
# Creating first plot_ly widget
widget1 <- plot_ly(data = morley, x = ~Run, y = ~Speed,
           color = ~factor(Expt), mode = "line") %>% layout
           (legend = list(title = list(text = "Expt no.")))
 
# Creating a second datatable widget
widget2 <- datatable(wind)
 
# Define the shiny app
ui <- fluidPage(
  fluidRow(
    column(width = 6, plotlyOutput("widget1")),
    column(width = 6, dataTableOutput("widget2"))
  )
)
 
# define the server function
server <- function(input, output) {
  output$widget1 <- renderPlotly({ widget1 })
  output$widget2 <- renderDataTable({ widget2 })
}
 
# Create the shiny app
shinyApp(ui = ui, server = server)


Output:

HTML widget using shiny in R

the

Note :- We can embed more than one html widget into a single document. Please save the above file as ‘app.r’ or ‘app.R‘.

In the above code, we have used an extra library DT which helps in creating interactive tables in HTML. We have used the data table () function of the DT library to convert the wind dataset provided by the Plotly package in R(it is not a preloaded dataset in R) to an interactive HTML table. The package name the

  • The UI code above creates a Shiny app layout with two columns. The left column will display the widget1 plot using the plotlyOutput() function and the right column will display the widget2 data table using the dataTableOutput() function from the DT package.
    • fluidPage() function creates a Shiny app UI layout with a fluid design that adjusts to the size of the browser window.
    • fluidRow() function creates a row within the UI layout. It is used to organize the elements horizontally.
    • The column() function creates a column within a row. It is used to allocate space for the UI elements horizontally. We have specified each column a width of 6, which means each widget will occupy the same amount of space within the row.
  • In the server() function the `input` parameter receives user interactions in the app and the app performs reactive operations based on them. The `output` parameter represents the output elements i.e. the widgets that are rendered in the UI.
    • output$widget1 binds the output element with the ID “widget1” to the reactive expression { widget1 } using renderPlotly(). The reactive expression will return the value of widget1 when accessed.
    • output$widget2 is similar to above and binds the output element with the ID “widget2” to a reactive expression { widget2 } using renderDataTable() and returns the value of widget2 when accessed.
  • shinyApp() function creates the Shiny application object by combining the user interface and server logic allowing us to run and interact with the app.

How to Embed htmlwidgets into Documents in R

HTML widgets in R Programming Launagauge are interactive web components, JavaScript-based visualizations that can be easily integrated into different documents. They are built on top of the HTML widgets package, which provides a framework for creating R bindings to JavaScript libraries. Examples of HTML widgets available for R include leaflet, dygraphs, Plotly, DiagrammeR, and others.

HTML widgets can be embedded directly into various types of documents created in R, such as R Markdown files, Shiny web applications, and standalone HTML pages, and even indirectly into documents like Word, Excel, PowerPoint, and PDF files. Embedding HTML widgets into documents ensures reproducibility, eliminating any discrepancies that may arise from the manual re-creation of the same document on different setups and environments.

The various implementations in different documents have been described below.

Similar Reads

1. Standalone HTML Pages

We can embed HTML widgets in standalone HTML pages using the saveWidget() function. This will create an HTML file that includes the necessary JavaScript and CSS dependencies. We can then include the generated HTML file in our HTML document using an iframe tag....

2. Shiny application

...

3. R Markdown Document

...

4. Word, PowerPoint, Excel, and PDF

Shiny is an R package that allows us to build interactive web applications directly from R. In Order to create a simple shiny app we need to have an `app.R` file that contains ui and server. The UI (User Interface) controls the looks and experience of the user and the server function defines the server logic of the Shiny app which controls what is being rendered in the application screen. To install it, type in R console the following:-...

Capturing Screenshots of HTML Output in R Code

...

Capturing Screenshots in the Shiny web application

R Markdown is a format for creating dynamic documents combining code, text, and visualizations. It is similar to IPython Notebooks in Python programming language though not exactly the same. R Markdown documents are rendered to HTML output by default since it allows for the inclusion of dynamic content like executing code, interactive visualization, and others that are best supported in HTML format....