Use DevTools


Background:

Browser developer tools, available on major web browsers -- Chrome, Safari and Firefox to name a few -- enable web developers to quickly collect vital information on most web applications, including ones that you yourself are building. Because it’s the choice for most professional developers, this article will focus on how to use Google Chrome’s DevTools. 

First, we’ll learn how to access the DevTools, then take a look at how to use them to: 

  1. Debug CSS and HTML to solve layout issues 

  2. Debug your JavaScript using the console tool 

  3. See important metadata about your web app 

Accessing the DevTools Window:

On Macs, the easiest way to access DevTools is to navigate to a webpage in a Chrome window, press Ctrl + Click (or Right Click if you have an old-fashioned two-button mouse) anywhere inside the document. A menu will pop up next to where you clicked. Select "Inspect Element." This will launch the DevTools window in the bottom third of your screen. It should look something like this: 

Devtools Panel

You’ll see eight tabs: Elements, Network, Sources, Timeline, Profiles, Resources, Audits and Console. This article will just focus on the Elements, Console and Network tabs, but we recommend you research and experiment with all of them. 

Debug CSS and HTML to Solve Layout Issues:

When laying the foundation for your own web app, it’s common practice to test your code by opening your HTML file in the browser and checking it out for yourself. Your HTML doesn’t need to be hosted on a server to do this! You can just open a new tab in Chrome and hit Cmd + o (Ctrl + o on Windows) or "File" > "Open" to browse your computer for your HTML file. Once your file is up, see if it’s doing what you expected. If something seems off, open the DevTools  by following the instructions above and start investigating. 

The "Elements" tab contains a real-time, interactive DOM (short for document object model). The DOM is the data structure that stores all of your HTML. Play around with it! You can change anything in the DOM and see results instantly. The only catch is that once you reload the page, your changes will disappear – so don’t refresh without copying any changes you want to hold on to. 

The CSS you wrote will be available in the "Elements" tab as its own scrollable section. You can find this on the right side of the DevTools window under "Styles":    

Styles Tab

Just like you could with your HTML elements in the DOM, you can change any property or value here and see the results in real-time. Maybe the rule you wrote for image tags is not positioning them the way you expected, or you want to change a background color or font-size. Experiment all you want here until you like what you see. But remember, you have to change the source code – the HTML and CSS files you loaded into the browser from your computer – or the changes you make in DevTools will disappear as soon as you reload the page. 

Debug your JavaScript Using the Console Tool:

If you’ve written custom JavaScript or jQuery functions to enhance your web app, you have full access to your code inside DevTools. Access your variables and functions or write new ones and watch them change the behavior of your web page within the DevTools console. If your code has syntax errors, or if files necessary to make your JavaScript run can’t be accessed, you’ll see error messages in that console. The error messages will give you concise descriptions about the nature of the problem.

To start using the console, navigate to the rightmost tab, titled "Console." Test that your JavaScript has loaded by starting to type in the name of one of your functions. If you see a pop-up with a list of functions that includes your function’s name, you know that DevTools is reading your JavaScript file. In the example below, DevTools has found my custom function, saysHello(). 

JS console

Remember, the DevTools console is a full JavaScript interpreter. It can run any valid JavaScript expression you type in it. If you’re linked to a library, like jQuery, it has access to all jQuery’s functions, too. 

Console Pro Tips: 

  • While writing JavaScript in the console, you may find that the screen fills up pretty quickly with code and error messages. On Macs, Ctrl + K (Ctrl + L on Windows) clears the console so you can start with a fresh screen. Your previous lines of code are not lost. Use the  arrow key to restore cleared lines. 
  • In the console, the Return (a.k.a, "Enter") key immediately executes your code. So, if you want to write multi-line functions, like the one below, use Ctrl + return (Shift + Enter on Windows) at the end of the line to jump down to a new one. 

multi-line console

The DevTools console is excellent for helping you understand what JavaScript can and can’t do, and giving you instant feedback through error messages and real-time changes to your page. DevTools also has the more advanced V8 debugging tool in the "Sources" tab, which is out of the scope of this article. If it piques your interest,you can learn more about it here.

View Metadata About Your Web App with the NetworkTab:

The "Network" tab – the second tab over from the left, next to "Elements" – contains valuable data about network operations, which are communications between your web browser and the server providing you the web page. If the tab is blank when you open it, you may have to click reload to fill in network data. 

The Network tab looks complicated at first, but it’s worth getting familiar with. 

network tab

It’s a lot, right? Don’t be overwhelmed. Just focus on the table in the bottom half of the tab. Specifically, we’re just going to look a few of those headers: 

Name: This is the column where you can view the name of every resource, a.k.a, every file, that is needed to create the web page you’re looking at. Scroll through the list and check out the file extensions. Can you figure out what types of files they are? See any .js or .png

Method: This column might not be listed by default. If you don’t see it, hover your mouse over any header and press Ctrl + Click or Right Click to see a full list of headers. (Please note: this works for Macs only). 

Under the Methods column it just says, "GET, GET, GET, GET, GET ..." What does that mean? GET is the most common HTTP method: a request for data. The method literally goes and gets resources from a server and brings them back to your web browser. Other methods you might see in this column are POST or DELETE. The POST method brings new data to the server. You’ll see this one when you hit "submit" to create a new user profile on a web page. DELETE is a method that tells the server to delete information. 

Status: Status codes are reports that the server gives to the client (your web browser in this case) about the requested resource. You should see a lot of 200 codes, meaning that the resources for the web page you requested came over "okay" from the server. 404 errors are also particularly important. They’ll be highlighted in red. You can read more about 404 errors and what they mean here

Type: Here is where you can see what type of file the resource is. Scan the list for.css.html.js or file extensions for media files. 

Size: This column shows you the file size of a resource in bytes. Which file would you expect to have more bytes: a CSS stylesheet or a video? 

Time: This column shows you exactly how long it took each resource to load, from the instant it was requested until the last byte loaded in your browser. If you’re on a new computer and have a strong internet connection, most of the resources you’re looking at will have loaded in a matter of milliseconds. Pretty amazing, right? 

Note: This article on Chrome DevTools was meant to serve as a high-level introduction. There is a lotmore to know, and lot more you can do with these tools. Google "Chrome DevTools" to learn more. 







### 关于 R `devtools` 的使用教程及常见问题解决 #### 什么是 `devtools` `devtools` 是一个用于简化 R 包开发过程的强大工具包。它提供了许多实用函数,使得开发者可以更高效地创建、测试和发布 R 包[^2]。 --- #### 安装 `devtools` 要使用 `devtools`,首先需要安装该包: ```r install.packages("devtools") library(devtools) ``` 如果在 Windows 平台上遇到编译依赖项的问题,可以通过以下命令安装所需的 R 工具链: ```r devtools::install_devtools() ``` 这一步通常解决了大部分与 C++ 编译器兼容性相关的问题[^1]。 --- #### 创建一个新的 R 包 `create()` 函数可以帮助快速初始化一个新的 R 包项目结构: ```r create(path = "mypackage", fields = list(Description = "My first package")) ``` 上述代码会在当前工作目录下生成名为 `mypackage` 的文件夹,并自动填充必要的元数据文件(如 DESCRIPTION 和 NAMESPACE 文件)以及默认的文件夹结构。 --- #### 添加单元测试 为了确保包的质量,推荐为每个功能模块编写对应的单元测试。`use_test()` 可以帮助轻松创建测试脚本: ```r use_test("my_function") ``` 此操作将在 `tests/testthat/` 路径下生成相应的测试文件模板。例如,当指定 `"strsplit1"` 作为参数时,会生成如下路径的文件:`tests/testthat/test-strsplit1.R`[^4]。 --- #### 构建并加载本地包 完成开发后,可以利用以下命令构建并临时加载包到当前 R 环境中以便调试: ```r load_all() ``` 或者直接构建整个包: ```r build() ``` 这两个命令的区别在于前者仅适用于开发阶段,而后者则适合打包分发给其他用户。 --- #### 发布至 CRAN 或 GitHub 一旦确认包已准备好对外分享,则可通过以下方式提交至官方仓库或托管平台: - **CRAN 提交准备** - 使用 `check()` 验证是否存在潜在错误; - 修改文档字符串使其符合 Roxygen2 格式标准。 - **GitHub 推送** - 初始化 Git 版本控制; - 将远程地址设为目标存储库链接; - 执行常规推送操作即可同步最新更改。 具体实现细节可查阅官方指南[^3]。 --- #### 解决常见的安装失败问题 针对不同操作系统上可能出现的各种异常情况给出针对性解决方案如下表所示: | 错误描述 | 处理措施 | |------------------------------|--------------------------------------------------------------------------| | Linux 下缺少某些头文件 | 安装对应系统的 build-essential 组件及相关依赖库 | | macOS 报找不到 gfortran | Homebrew 用户运行 brew install gcc | | Windows 上无法找到 make.exe | 下载 MinGW-w64 并配置 PATH | 以上均能有效缓解因环境差异引发的一系列连锁反应。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值