Always feel running code, if there is a prompt task running schedule progress bar tip is good, a lot of times our program running time is generally long, if the program runs without any tips, it was a nightmare, don’t know what is the program being lazy or really stuck, and if the code again to write the print function, circulation is more, Your screen will be instantly flooded with printed prompt text.

After searching, I found a solution. Today WE introduce two packages, which can do task processing, process processing work, write some simple interactive interface.

Here is only a simple usage, only to meet our daily task progress tips.

library("tcltk")
library("tcltk2")
Copy the code

Master the following two main functions:

Set the format of the progress bar

TkProgressBar (title = "R progress bar", # progress bar label = "", # progress bar min = 0, # progress bar Max = 1, # progress bar initial = 0, # start position width = 300 # progress bar widthCopy the code

Enable the progress bar

SetTkProgressBar (pb, # progressbar value, # progressbar value = NULL, # title label = NULL # label) Library ("rvest") Library ("downloader") url<-"https://www.zhihu.com/question/35931586/answer/206258333" link<- read_html(url)%>% html_nodes("div.RichContent-inner>span")%>% html_nodes("img")%>%html_attr("data-original") %>% na.omit link<-link[seq(1,length(link),by=2)] %>% sample(size=10) Name<-sub("https://pic\\d.zhimg.com/v2-","",link) Dir. Create ("D:/R/Image/zhihu0807") setwd("D:/R/Image/zhihu0807") pb< -tkprogressbar (" progress "," completed %",0,100) for(I in) 1:length(link)){info < -sprintf (" done %d%%",round(I *100/length(link)) SetTkProgressBar (pb, I *100/length(link),sprintf(" progress (%s)",info) download(link[I],Name[I], Mode = "wb") sys. sleep(0.5)} close(pb)Copy the code

The TCLTK2 package can customize more progress bar properties and appearance interface, making the progress bar more personalized.

Library ("tcltk2") ### Enable progress bar root < -tktoplevel () l1 < -tk2label (root) pb1 < -tk2progress (root,length=300) tkconfigure(pb1,value=0,maximum=9) tkgrid(l1, row = 0) tkgrid(pb1,row = 1) for(index in 1:length(link)){ download(link[i],Name[i], Mode = "wb") sys. sleep(0.5) Tkconfigure (l1, text = paste("Index", Index)) tkconfigure(pb1,value=index-1) sys. sleep(0.5)} tkdestroy(root) #Copy the code

Is not very humanized ah ~~~ later running large programs, no longer have no bottom in mind, looking at the progress bar you can know how much your program is running.

Since R can perform progress management, I am sure it can also implement it compared to Python. I have found some, but most of them are based on Py2, and the code volume is not small. I am afraid to see a large number of code, and finally found a small number of code, briefly introduce some, hope to call you to help.

import os import re import time import sys import urllib import requests import random from bs4 import BeautifulSoup Url = "https://www.zhihu.com/question/35931586/answer/206258333" header = {' the user-agent ':' Mozilla / 5.0 (X11; Linux x86_64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/59.0.3071.104 Safari/537.36'} Page =requests. Get (URL,headers=header) soup=BeautifulSoup(page.text,'lxml') link_list=[pip.get('data-original') for pip in soup.select("div[class='RichContent-inner'] span img")] link_list=random.sample(link_list,10) os.makedirs("D:/Python/zhihu0807") os.chdir('D:/Python/zhihu0807') for i in range(len(link_list)): name=re.findall(r"v2-.*? \.jpg",link_list[i])[0] urllib.request.urlretrieve(link_list[i],name) sys.stdout.write('\r') sys.stdout.write("%s%% |%s" % (int (I % len (link_list)), int len (I % (link_list)) * '#')) sys. Stdout. Flush () time. Sleep (0.5)Copy the code

There are a number of third-party packages available to create a professional and aesthetically pleasing task progress manager, and you can even write your own toolbar with pop-up functionality, but that takes the simplicity out of it and you can explore on your own!

For online courses, please click on the link below:

Hellobi Live | September 12 R language data visualization application in the business scenario past case please click I lot: https://github.com/ljtyduyu/DataWarehouse/tree/master/File