The Shiny cheat sheet is a quick reference guide for building Shiny apps. Check out all of our cheat sheets here. Throw gameunblocked games. Please post on RStudio Community.
Key Concepts
Rstudio ide-cheatsheet 1. Documents and Apps Project System Write Code R Support RStudio Pro Features Debug Mode Version Control with Git or SVN Package Writing Project System Write Code R Support RStudio Pro Features Debug Mode Version Control with Git or SVN Package Writing Turn project into package, Enable roxygen documentation with Tools Project Options Build Tools Roxygen guide at. RStudio IDE Cheat Sheet The RStudio IDE is an Integrated Development Environment in R that comes in three versions Desktop IDE A local version of the IDE for your desktop learn more at www.rstudio.com. RStudio recognizes that files named app.R, server.R, ui.R, and global.R belong to a shiny app Run app Choose location to view app Publish to shinyapps.io or server Manage publish accounts Access markdown guide at Help Markdown Quick Reference RStudio IDE Cheat Sheet.
This chapter introduces RStudio, a Graphical User Interface (GUI) that makes it easier to use powerful features in R and manage large projects.
New vocabulary:
- Integrated Development Environment (IDE)
- RStudio Panes:
- Console
- Source
- Plots
- Viewer
- Environment
- History
- Workspace & Global Environment
What is RStudio?
Recall that R is both a language and an environment. RStudio is an Integrated Development Environment, or IDE, which is an enhanced, feature-rich programming environment with an easy-to-use graphical user interface, or GUI. While the base R environment is mostly text, RStudio has intuitive icons (hence, “graphical”) for point-and-click, automated operations.
RStudio’s layout is comprised of a menu, console, and a series of panes, or windows in the RStudio interface. Most panes are feature-rich and all panes serve a key purpose, but we’ll only focus on the most critical panes for getting started in RStudio.
The RStudio environment with four open panes: The Source, Console, Environment, and Viewer panes.
Downloading & Installing RStudio
RStudio requires R 3.0.1+. If you don’t already have R, download it here.
RStudio is free, open source, and easy to install. Select the Desktop edition on their download page:
A Tour of RStudio
RStudio is comprised of a main menu and a series of panes, each with their own purpose and features. We focus on the following:
- Console Pane
- Source Pane
- Plots Pane
- Viewer Pane
- Environment Pane
- History Pane
The Console Pane
The console pane is where R expressions are evaluated. In other words, this is where your code is executed. Recall that working in-console is also known as working interactively and, typically, working in-console is more often for “quick and dirty” tasks, like printing contents of your working directory.
The Console Pane
The console is where code is executed and is typically used for “quick and dirty” tasks.
The console panel lists your current working directory. Notably, even when using point-and-click mechanics to, e.g. import data or change directories, the code for such tasks will still execute in the console. Such click-to-code operations are called macros.
Pro Tip: When writing a script, especially when writing out new directory paths, it’s sometimes quicker to use use a click-to-code operation and simply copy and paste the macro code from the console to your script.
The Source Pane
The source pane contains any opened scripts. In starting a new R session, this pane isn’t visible until you’ve opened a new or preexisting script. Multiple scripts may be opened at one time and are navigable using tabs along the top of the source pane.
The Source Pane
The source pane contains opened scripts. It won’t appear until you’ve opened at least one.
Depending on the type of script, e.g. plain text scripts (.R
), publications (.Rmd
), presentations (.Rpres
), and apps, each script provides different options in the pane’s toolbar. Common options include:
- Show in New Window: Open the script in a separate window; valuable for two or more monitors
- Save Current Document: Update an existing script or title and save a new script
- Find/Replace: Both conventional and advanced options to find and replace code
- Run: Run the line of code where the cursor is, or multiple lines if highlighted
- Show Document Outline: View (and jump to) script’s table of contents
Plots, Viewer, & Help Panes
The Plots, Viewer, and Help panes are used to viewing data visualizations, HTML output, and helpful documentation.
The Plots Pane
The Plots pane allows users to view, export, and publish non-interactive data visualizations. R uses the built-in graphics
package by default, but a variety of packages exist such as lattice
and ggplot2
. While the output displayed is not interactive, it is responsive, i.e it will re-render its scales appropriately if you change the height or width of a plot. Notably, the “Zoom” option opens visualizations in a new window, while the “Export” option allows you to save the image with user-defined dimensions and in a variety of formats.
The Viewer Pane
The Viewer pane renders interactive graphics in HTML with the same options as the Plots pane. Brevity aside, it’s awesome.
The Plots & Viewer Panes
The Plots and Viewer panes visualize non-interactive and interactive graphics, respectively.
The Help Pane
The Help pane is one of the most valuable panes for any R user. By calling function help()
with a dataset, package, or bare function name (i.e. a function name without ()
), its documentation, if available, appears here.
Rstudio Ide Cheat Sheet Free
Note: Unless you’re using external data or custom functions, there’s almost always documentation. Whether it’s the unit of measurement for a variable in a dataset or the limits you can specify for a function argument, this little nook in RStudio is invaluable to new and advanced users, alike.
Pro Tip: Instead of the help()
function, you can use the ?
function before an object name, e.g. ?install.packages
.
The Help Pane
The invaluable Help pane displays documentation for packages, functions, and datasets.
The Environment and History Panes
The Environment and History panes display the objects in your environment and the history of your in-console commands.
The Environment Pane
Again, R is both a language and an environment. The Environment pane displays objects that are stored within your session’s workspace, or global environment, which must be recreated or reloaded with each new session. Note the following options:
- Environment: Opens a dropdown menu to select different environments, e.g. package environments
- Load Workspace: Opens a file explorer to load previously saved workspaces and their objects
- Import Datasets: Opens a dropdown menu to read in datasets that you can store in objects
- Clear Objects from Workspace: Removes all objects stored in the global environment
The Environment Pane
The environment pane displays any objects you’ve imported, loaded, or stored in your global environment.
The History Pane
The History pane documents every command you’ve executed in your session. When you select a line, you can paste it directly into the console pane with “To Console” or directly into the source pane with “To Source”.
The History Pane
The history pane records every command you’ve run in your session.
Customizing Your Pane Layout
In RStudio, you can customize both where panes are displayed as well as which panes to show by default.
Layouts for Beginners: Taking Great Panes
Panes cannot be removed entirely from the RStudio interface, you but can shuffle them by order of importance. Click the “Tools” dropdown in themenu, “Global Options…”, and “Pane Layout”. We recommend focusing on those discussed in this chapter.
Customizing Pane Layout
You can customize which panes appear, and where, in Global Options’ Pane Layout.
Less Important Panes, or Panes in the Rear
There are a couple of panes worth mentioning for new users. However, they are seldom used by advanced users:
- Files: Set working directories and create, copy, rename, and delete folders
- Packages: Install, load, update, unload, and uninstall added (“User Library”) and built-in (“System Library”) packages
Pro Tip: You probably won’t use these panes often. One of the benefits of scripted languages is that they can be reproduced by other users. As a rule, since much of your work will require loading packages, you should include the library()
function with script-dependent packages at the start of every work. The same applies to working directories with the setwd()
function.
Expanding to Fullscreen: Focus on the Pane
If you want to expand a pane, or “zoom”, to fullscreen mode, select “View” in the RStudio menu and “Panes”.
Toggling Fullscreen Panes
You can expand any pane to fullscreen mode in the “View” dropdown of the RStudio menu.
The RStudio Menu: File, Session, & Help
The RStudio menu allows you to do virtually everythign we’ve seen in each pane and more. The following tours a few key menu sections we’ve not yet seen, including how to open new scripts, handle sessions, and access R-related cheat sheets.
The File Submenu: Saving & Loading
The File submenu is the start of every scripted data product in RStudio. Just select “New File” and a litany of possible data products, about which we’ll learn more over time, are available to open. Remaining options relate to the saving and loading of scripts, projects, and datasets.
The File Submenu
“File” handles all new, saving, and loading operations.
The Session Submenu: Sessions, Directories, & Restarting R
The Session submenu is a critical part of any R session. While you can always recreate a session by recreating objects, you can save computing time by loading saved session file. This submenu also allows you to set your working directory.
Note: There are times when you just have to restart R - maybe you started an infinite recursion loop or maybe you attempted to read in the data from a month’s worth of Harrier Jet landings to local memory - if so, the “Restart R” option is here for you.
The Session Submenu
Save and load sessions, restart R, or handle directories in the session submenu.
The Help Submenu: Cheat Sheets
The help menu has one main draw (for now): cheat sheets. Selecting a cheat sheet will automatically download a cheat sheet on an R-related topic of your choosing. Typically, cheat sheets summarize RStudio related packages and data products, but there is one for base R. For example, you can find the RStudio IDE cheat sheet here.
The Help Submenu
Grab cheat sheets on the fly from the Help submenu.
Global Options: Aesthetic & Functional Preferences
Global options are accessed in the “Tools” submenu and allow users to modify their RStudio interface in myriad ways, both aesthetically and functionally. We recommend new users experiment with these options and visit a few notable modifications.
Accessing Global Options
Code: Autoformatting & Behavior
The “Code” section affects the way R automatically formats your code and how you choose to write and run it. It has sensible defaults, many of which you may not be prepared to appreciate quite yet. For now, consider the following:
- Indentating: In the “Editing” tab, consider a “tab width” that works best for you. When indenting, would you prefer two characters (i.e. spaces), or four? The former allows more compact code. The latter allows for more intepretable code.
- Guide Margin: In the “Display” tab, consider applying a “margin column” of 80 or 100 characters (i.e. spaces). This creates a subtle guide in your scripts that helps keep code concise and readable. Even basic code within basic code can create, what RStudio’s Chief Scientist Hadley Wickham refers to as, “Dagwood sandwich” code.
Appearance: Express Yourself Intepretably
The Appearance section allow you to customize the size, font, and color of your text as well as the “theme” colors of your RStudio interface. Here, “theme” is both functional and aesthetic. For example, darker themes are more conducive to night owls. For all themes, certain syntax uses different colors to make your code more interpretable - keep this in mind for each theme!
Notable Global Options: Code & Appearance
Global options allow you to set preferences that can significantly impact your experience with RStudio.
Further Resources
The following resources are helpful in learning more about RStudio and coding conventions:
I) Full Introductions to RStudio
- “What are R and RStudio?” (Ismay & Kim, 2019)
- “Intro to R: Nuts & Bolts” (Crawford, 2018)
II) About RStudio
- RStudio Homepage (RStudio, 2019)
- RStudio About Page (RStudio, 2019)
- RStudio Product Page (RStudio, 2019)
III) Conventions
- “The State of Naming Conventions in R” (Baath, 2012)
Startpoint
We will be styling the ever-familiar “Old Faithful Geyser Data” app; this is the app that you get whenever you request a new Shiny app in RStudio. We will be working with a single-file shiny app, so all the code is in this single app.R
file. (The line spacing and comments are changed to make the code more compact.)
Our custom styles
To update our app’s style, we will implement a pseudo-dark-mode and change the app title’s font using a font from Google Fonts. To do this, we will add the following CSS:
So, while it’s not a proper dark mode, our app does look a lot different.
Getting our CSS into the app
So how do we go about getting the above CSS into our Shiny app? There are many ways to do this, but they revolve around main options: inline CSS or file-based CSS. “Inline” CSS in the case of a Shiny app is where we write our preferred styles using character strings right in our UI declaration. “File-based” is when we write the styles in their own separate .css
file and point our app to that file. There are multiple ways to do both options, but we will show the best way to do both for 99% of use-cases in this post. At the end, we will briefly cover the other methods and explain their pros and cons.
Inline CSS
The quickest and easiest way to get CSS into your app is by “inlining” it. The way to do this in Shiny is using the head
and styles
tags
:
Rmarkdown Cheat Sheet 2.0
Inlining Pros
A significant plus inlining is the CSS sits right in the main app script, and the developer does not need to go far to make changes, reducing the amount of flipping back and forth between files required to make changes. Adding inlined styles is also super quick: no files need to be created and then linked; just type your code and reload the app.
Inlining Cons
A downside of your styles living in the same file as your app logic is your UI function can get cumbersome with anything more than a few lines of CSS. In terms of developer convenience, you also lose great editor features like syntax highlighting and auto-complete, as the editor won’t know your writing CSS inside that string.
When to use inlining
When developing a Shiny app with custom styles, a balance needs to be struck between easy access to custom styling afforded by inline CSS and having an app script of manageable length. A typical development workflow will involve initial style work being done inline. Once the CSS gets to longer than a few rules, the app is refactored into the file-based workflow.
File-based CSS
R Code Cheat Sheet
A note about www/
One thing you may notice is that we placed our CSS file in the subfolder www/
, but we only specified the CSS file’s name (dark_mode.css
) in our href
or “hyperlink reference” argument. The www/
folder is a special one for Shiny. Resources your app may link to, such as images—or in this case, scripts—are placed in the www/
folder. Shiny then knows to make these files available for access from the web browser. If we had placed dark_mode.css
at the same file hierarchy next as app.R, Shiny would not know that it needs to host it, and your app would tell the browser to look for a file that was not available to it.
Pros of file-based CSS
The benefits of file-based CSS inclusion follow naturally from the cons of the inline approach. You can encapsulate all your styling logic in its own file. This way, style declarations do not clutter your app logic, and—if your editor supports it—you can use proper syntax highlighting and auto-complete. Another big positive is the ability to use tools like SASS or LESS to build your styles and have them transpile to your external CSS file instead of having to copy and paste the results right into a string within your app script.
Cons of file-based CSS
While a modular workflow can make managing large and complex apps easier, it can also make managing small and simple apps more complicated. As mentioned in the pros of the inline section, the developer needs to flip between files and keep track of class or id dependencies between the UI declaration and the custom CSS. File-based CSS workflows can make sharing your code more difficult. It’s a lot easier to copy and paste the contents of a single R script rather than layout the creation of files (of course, you could use a service like GitHub to avoid these issues.)
When to use file-based
Reach for file-based CSS when your CSS is more complicated than a few basic rules.
Other methods
Those familiar with Shiny may have noticed I left out a couple of ways of getting CSS into your app.
theme = 'styles.css'
You can pass a CSS file directly to your app using the theme argument in your UI function, much the same as the tags$link()
method. However, it’s not recommended anymore because the theme
argument is now commonly used by the bslib
package to pass in custom bootstrap theming options. (bslib
also provides its own functionality for adding additional CSS to a given bootstrap theme with the function bslib::bs_add_rules()
.)
includeCSS()
The function includeCSS()
is an amalgamation of inline and file-based CSS. It takes as its argument a file path—this time not necessarily in the www/
folder—and pastes that file’s contents directly into the HTML of your app instead of using a file link. This means
makes Shiny build the same HTML as doing
As a bonus, you can verify this by viewing your app in a browser and right-clicking and selecting “view source.” You’ll see the same source for both approaches above, but not with the original file-based method.
For almost every Shiny app, the difference between including CSS via inlining and with a link is negligible, and there is no need to worry about performance implications. However, when you have a large amount of CSS (which can sometimes occur when using CSS generating languages like SCSS) it’s better to link to the styles rather than directly place them in the HTML.
Element style
argument
When dealing with plain tag objects in Shiny, such as is we had declared the title of the app with an h2()
instead of titlePanel()
you can place any custom CSS you want in the style argument. These styles just apply to that specific element. Sales guy danielthe initials game.
Note: If you wanted to use the nice Google font we used before, you’d have to still import that in a chunk of CSS added somewhere else.
This type of CSS is good for simple bespoke styling modifications, but, like inlining CSS in general, can quickly get out of hand if styles become too complicated. When this happens it’s often better to give the element to be styled a unique id and target that id in your general CSS declarations.