When you are ready, continue to Lesson 2, where you will learn how to build the layout and appearance of your Shiny apps. You’ll learn about each part of a Shiny app, and finish by deploying your own Shiny app online. All Shiny example apps open in “showcase” mode (with the app.R script in the display).īut why limit yourself to copying other apps? The next few lessons will show you how to build your own Shiny apps from scratch. runExample ( "01_hello" ) # a histogram runExample ( "02_text" ) # tables and data frames runExample ( "03_reactivity" ) # a reactive expression runExample ( "04_mpg" ) # global variables runExample ( "05_sliders" ) # slider bars runExample ( "06_tabsets" ) # tabbed panels runExample ( "07_widgets" ) # help text and submit buttons runExample ( "08_html" ) # Shiny app built from HTML runExample ( "09_upload" ) # file upload wizard runExample ( "10_download" ) # file download wizard runExample ( "11_timer" ) # an automated timerĮach demonstrates a feature of Shiny apps. The Shiny gallery provides some good examples, or use the eleven pre-built Shiny examples listed below. You can create Shiny apps by copying and modifying existing Shiny apps. Launch the app with runApp or RStudio’s keyboard shortcuts. Save your app.R script inside that directory. Make a directory named myapp/ for your app. Make your selection by clicking the icon next to Run App. RStudio will launch the app in a new window by default, but you can also choose to have the app launch in a dedicated viewer pane, or in your external web browser. Either click this button to launch your app or use the keyboard shortcut: Command+Shift+Enter (Control+Shift+Enter on Windows). RStudio will recognize the Shiny script and provide a Run App button (at the top of the editor). Open the app.R script in your RStudio editor. # Define server logic required to draw a histogram - server <- function ( input, output ) Relaunching Apps Here is the server function for the Hello Shiny example. library ( shiny ) # Define UI for app that draws a histogram - ui <- fluidPage ( # App title - titlePanel ( "Hello Shiny!" ), # Sidebar layout with input and output definitions - sidebarLayout ( # Sidebar panel for inputs - sidebarPanel ( # Input: Slider for the number of bins - sliderInput ( inputId = "bins", label = "Number of bins:", min = 1, max = 50, value = 30 ) ), # Main panel for displaying outputs - mainPanel ( # Output: Histogram - plotOutput ( outputId = "distPlot" ) ) ) ) server Here is the ui object for the Hello Shiny example. For example, if you copy and paste the code above into the R command line, it will start a Shiny app. One nice feature about single-file apps is that you can copy and paste the entire app into the R console, which makes it easy to quickly share code for others to experiment with. This functionality is still supported in Shiny, however the tutorial and much of the supporting documentation focus on single-file apps. Note: Prior to version 0.10.2, Shiny did not support single-file apps and the ui object and server function needed to be contained in separate scripts called ui.R and server.R, respectively. Finally the shinyApp function creates Shiny app objects from an explicit UI/server pair. The server function contains the instructions that your computer needs to build your app. The user interface ( ui) object controls the layout and appearance of your app. The script app.R lives in a directory (for example, newdir/) and the app can be run with runApp("newdir"). Shiny apps are contained in a single script called app.R. To run Hello Shiny, type: library ( shiny ) runExample ( "01_hello" ) Structure of a Shiny App You’ll use Hello Shiny to explore the structure of a Shiny app and to create your first app. Users can change the number of bins with a slider bar, and the app will immediately respond to their input. The Hello Shiny example plots a histogram of R’s faithful dataset with a configurable number of bins. Each example is a self-contained Shiny app. The Shiny package has eleven built-in examples that each demonstrate how Shiny works. If you still haven’t installed the Shiny package, open an R session, connect to the internet, and run install.packages ( "shiny" ) Examples This lesson will get you started building Shiny apps right away. Shiny is an R package that makes it easy to build interactive web applications (apps) straight from R.
0 Comments
Leave a Reply. |