Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of R language RStudio

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about the use of the R language RStudio, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

RStudio is an open source tool for R programming. If you are interested in programming with R, it is worth learning about the capabilities of RStudio. It is a flexible tool that helps you create readable analysis and keep your code, images, comments and diagrams together.

In this tutorial article on big data's analysis of the use of the R language RStudio, we will introduce some of the best features of the free version of RStudio: RStudio Desktop. We have collected some important RStudio tips, tips and shortcuts to quickly turn you into an advanced user of RStudio!

1. Quickly navigate between window panes

The RStudio pane gives you access to important information about the project. Knowing how to switch between panes without touching the mouse to move the cursor will save time and improve workflow. Use these shortcuts to move instantly between panes:

Control/Ctrl + 1: source code editor (your script)

Control/Ctrl + 2: comfort

Control/Ctrl + 3: help

Control/Ctrl + 4: history

Control/Ctrl + 5: files

Control/Ctrl + 6: plot

Control/Ctrl + 7: set meal

Control/Ctrl + 8: environment

Control/Ctrl + 9: viewer

If you want to see only one pane at a time, add Shift to any of the above commands to maximize the pane. For example, enter Control/Ctrl + Shift + 1 to maximize the R script, notebook, or R Markdown file you are using.

(side note: + what we show in the shortcut means "and", so there is no need to actually type the + key.)

But what if you want to return to the standard four-pane view? No problem! Enter Control/Ctrl + Shift + 0:

two。 Keyboard shortcuts

Learning about RStudio keyboard shortcuts will save you a lot of time when programming. RStudio provides many useful shortcuts that you can access Tools > Keyboard Shortcuts Help from the menu at the top.

Another way to access RStudio keyboard shortcuts is to use keyboard shortcuts! To access shortcuts, enter Option + Shift + K on Mac or Alt + Shift + KLinux and Windows.

Here are some of our favorite RStudio shortcuts:

On Mac or Linux and Windows, put% with Command + Shift + M on Mac, or Ctrl + Shift + M on Linux and Windows.

Command + Enter runs the current line of code on Mac or Control + EnterLinux and Windows.

Command + A + Enter uses to run all lines of code on Mac or Control + A + EnterLinux and Windows.

Restart the current R session, and then Command + Shift + F10 starts again on Mac or Control + Shift + F10Linux and Windows.

Command + Shift + C uses or uncomments lines on Mac or Control + Shift + CLinux and Windows.

Try to remember the command you submitted earlier? Command + [up arrow] search for command history from the console on Mac or Control + [up arrow] Linux and Windows.

There are many useful shortcuts available, but by mastering the above shortcuts, you will become an advanced user of RStudio!

Another important resource for RStudio shortcuts is the official RStudio cheat sheet provided here.

3. Save time through code completion

When you start typing, a suggestion window pops up with matching function, object, and snippet names. You can use the up or down arrows to switch in the list, and then click return/Enter to make a selection.

In addition, you can take advantage of a very cool feature called fuzzy matching, which allows you to narrow your search by entering letters unique to the match. You don't need to enter all the letters, as long as what you enter matches the order of the strings.

Let's take a look at how these code completion methods work. First, we will installed.packages () select the function by typing part of the function name, and then use the arrow to select it. Next, we will use fuzzy matching to enter only instd to further narrow the selection:

4. Quickly find files and features

In RStudio, you do not need to explore the folder structure to find files, nor do you need the mining function! Enter the shortcut control/ctrl +. To open the Go to File/Function window and use the fuzzy matching skill to narrow the selection:

5. Custom appearance

RStudio provides a number of options to customize the appearance to your liking. Under the RStudio tab, navigate through many of the available options as Preferences > Appearance. A nice feature of RStudio is that you can quickly click on the Editor theme window to preview each theme.

6. Easily link to a document

Under the tab of the lower-right window of Help, you will find convenient links to R functions and R package online documentation. For example, if we install.packages () use the search bar to search for information about the feature, we return the official document:

We can also access the documentation in the Help tab by adding? before the package or function, for example,? install.packages and then running the command in the console. No matter which method you use, RStudio automatically populates the matching function name as you type!

7. Preview and save your drawing

The diagram generated during the RStudio session is displayed under the tab in the lower-right window of the Plots. In this window, you can check the diagram by zooming in and out. If you want to save the drawing, you can save the drawing as a PDF or image file.

8. Import and preview datasets

RStudio makes it easy to import and preview datasets without coding! Under the tabs of the upper-right window of the Environment, you have the ability to import datasets. This feature supports multiple formats:

You can even preview the dataset before loading it:

After loading the dataset into RStudio, you can use the View () command or click the name of the dataset to view it:

9. Click to view command history

Previously, we learned about shortcuts to command history from the console. RStudio also allows you to view the entire command history in the upper-right window by clicking the History tab:

10. Save your "real" work. Delete the rest.

Practice good housekeeping to avoid unexpected challenges in the future. If you create an R object worth saving, capture the R code that generated the object in the R foot big data Analysis R language RStudio usage tutorial. Save the R script, but do not save the environment or workspace where the object was created.

To prevent RStudio from saving the workspace, open Preferences > General and deselect the option for .RData to restore to the workspace at startup. Be sure to specify that you never save the workspace, as follows:

Now, every time you open RStudio, it starts with an empty session. None of the code generated by your previous session will be remembered. R scripts and datasets can be used to recreate the environment from scratch.

11. Organize project work

RStudio provides powerful features that keep you organized; projects. It is important to be organized when conducting multiple analyses. RStudio's project allows you to put all your important work in one place, including code scripts, drawings, graphics, results, and datasets.

Navigate to the tab in FileRStudio and select to create a new project New Project.... You can choose to create a new project in a new directory or an existing directory. If you are using an R package or a Shiny Web application, RStudio provides a dedicated project type.

RStudio projects are useful when you need to share your work with colleagues. You can send the project file (ending with .Rproj) with all support files, which will make it easier for your colleagues to recreate the work environment and recreate the results.

However, if you want to collaborate seamlessly, you may need to introduce package management into the workflow. Fortunately, RStudio provides a useful package management tool, renv, which is now compatible with the RStudio project. Renv next we will introduce.

twelve。 Use renv to manage package versions

We love the R of AAA Education, but managing the package version can be a challenge! Fortunately, thanks to RStudio's renv ("replicable environment") package, R package management is easier than ever. RStudio now includes built-in support for renv.

Renv in this tutorial on the use of RStudio in big data's analysis of the R language, we will not elaborate on how to use it with the RStudio project, because RStudio provides you with the information you need in the links and illustrations we provide. However, using renv with RStudio makes R package management easier, so we want to tell you!

This renv package will replace the Packrat package that RStudio has maintained.

To use renv with your RStudio project package to upgrade to the latest version of RStudio, then install renv with package library ("renv"). From there, you can choose renv to use with all new projects:

If you want renv to work with existing projects, browse to Tools > Project Options > Environments and check the appropriate box to enable renv:

13. Using GitHub to manage versioning in RStudio

In addition to managing packages in RStudio, you can also use GitHub with RStudio to maintain version control of projects and R scripts. Take a look at this article from GitHub and this article from RStudio to integrate all the Git you need into your workflow RStudio.

14. Code segment

RStudio provides a very useful feature for inserting common blocks of code called code snippets. One of our favorites is this lib snippet, which saves you some typing time when you load the R package by calling the library () function:

When you press the return key to select a code snippet, the library () function is loaded and the cursor is positioned, so you can immediately start entering the name of the package to load:

Another fun snippet we like is one that provides a basic template for writing custom functions. You can even add your own code snippets! For more information, check out this article from the RStudio code snippet.

15. In-depth study of the source code of the function

If you want to study the source code of a function, move the cursor to the desired function and enter F2 (on Mac, you may need to enter fn + F2). This feature even applies to features loaded from any R package you use.

16. Function extraction

If you have written a code block to convert to functionality, highlight the code block, and then control + option X type on Linux / Windows on Mac Ctrl + Alt + X. A pop-up window will appear asking you to select a function name.

After you select a feature name, the code is automatically added as the input and code structure required for the feature.

If you have a variable to extract, highlight the variable and enter control + option V on Linux / Windows on Mac Ctrl + Alt + V.

17. Rename range

At some point, you may need to change the function name or the variable used in one of the functions. But using find and replace to do this can be disturbing! Fortunately, RStudio can be renamed in scope. This means that your changes will be limited to the variables or functions of interest. Big data analyzes the R language RStudio using tutorial https://www.aaa-cg.com.cn/data/2394.html to prevent you from accidentally replacing variables of the same name elsewhere in the code script. To use this feature, select the function or variable you want to change, and then enter control + shift + option + M on Mac or Ctrl + Shift + Alt + MLinux / Windows.

18. Multi-cursor support

RStudio supports multiple cursors. When holding down optionMac or AltWindows / Linux, simply click and drag the mouse.

19. Use Python with RStudio and mesh

RStudio supports python encoding. The process of starting and running python in RStudio involves the following general steps:

Install the basic version of Python

Install pip and virtualenv

Create a Python environment in a RStudio project

Activate your Python environment

Install the required Python packages in your environment

Install and configure the R Reticulate package to use Python

Big data analyzes the R language RStudio using tutorials to provide the code required for the above steps. We tried it, and it took only a few minutes to run python in RStudio:

20. Use the DBI package to query SQL

There are many ways to run SQL queries in RStudio. Starting with R's DBI package, here are the three most popular methods.

First, generate an in-memory SQL database to use in all SQL query examples. You will generate a SQL database of the famous "mtcars" dataset. This is the code:

Now write a SQL query to select all cars from the database using a four-cylinder engine. This command returns a data box that you will save as dbi_query:

The data box is as follows:

21. Use R Markdown or use R Notebook to query SQL

By creating a {sql} code block, you can get the same result in R Notebook or R Markdown. Using the connection and database in the first example, run the following code:

Specify output.var = "mt_cars_df" to save the query results to the data box. This data box is a standard R data box, the same as the one you generated in the previous example. You can use this data frame in the R code block to perform analysis or generate ggplot, for example:

twenty-two。 Use dbplyr to query SQL

Finally, you will write standard dplyr commands using the dbplyr package, which will be converted to SQL! Using the connection and database in the first example again, you can write a standard filter () call to query a car with four cylinders, which returns a list object:

If you want to see the SQL code that this command translates to, you can use the show_query () function in dbplyr:

When you are satisfied with the query results, you can use the collect () function in dbplyr to save the results as data boxes:

You have it! There are three different ways to query SQL databases with similar results. The only difference between the examples is that the dbplyr method returns a subtitle, while the first two methods return a standard R data frame.

To learn more about querying SQL databases using RStudio, please refer to big data's tutorial on analyzing the R language RStudio.

23. Take it to the cloud!

RStudio now offers a cloud-based version called RStudio Desktop, and you guessed it. RStudio Cloud . RStudio Cloud allows you to code in RStudio without installing software, just a Web browser.

The work in RStudio Cloud is organized into projects similar to desktop versions, but RStudio Cloud allows you to specify the R version you want for each project.

RStudio Cloud can also easily and securely share projects with colleagues and ensure that the work environment is fully recreated every time you visit the project.

As you can see, the layout of RStudio Cloud is very similar to that of RStudio Desktop:

The above is how the R language RStudio is used, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report