tips and tricks

Han Oostdijk

2019/10/17

Date last run: 09Apr2021

Searching

Task Command Remark
Search in files grep -ri ‘text’ recursive and case insensitive
Search in files grep -n ‘text’ show line numbers
Search in R files grep -ri ‘trace’ --include *.R
Helpinfo for grep grep --help NB repeated - !
Find filename(s) find . -name ‘*mag2*‘ find files below current folder containing ‘mag2’ (in Terminal)

R Debugging

trace(get_trends,quote(browser()),where=asNamespace("rtweet"))
rtweet::get_trends("amsterdam")
untrace(rtweet::get_trends)

as.list(body(rtweet:::get_trends_))
trace(rtweet:::get_trends_,
			quote(browser()),
			at=8,
			where=asNamespace("rtweet"))
rtweet::get_trends("amsterdam")
untrace(rtweet:::get_trends_)

browser() commands

From browser() function help info:

Command Description
c exit the browser and continue execution at the next statement
cont synonym for c
f finish execution of the current loop or function
help print this list of commands
n evaluate the next statement, stepping over function calls. For byte compiled functions interrupted by browser calls, n is equivalent to c
s evaluate the next statement, stepping into function calls. Again, byte compiled functions make s equivalent to c
where print a stack trace of all active function calls
r invoke a “resume” restart if one is available; interpreted as an R expression otherwise. Typically “resume” restarts are established for continuing from user interrupts
Q exit the browser and the current evaluation and return to the top-level prompt

Two methods to include code with highlighting. See that the two paths are different. In the first case the base directory is the note or post directory and in the second case the base directory is the project library.

`​``{r echo=T,eval=T,comment=''}
cat(readLines("../../R/update_HOQC.R",warn=F),sep="\n")
`​``


```{r include2,code=readLines("./R/update_HOQC.R",warn=F),eval=F}
```

insert graphics

Graphics created by an Rmd file post/2020-06-09-an-html-document-with-aside-s are placed in a subfolder of
static/post/2020-06-09-an-html-document-with-aside-s_files . We can use the same folder when we want to include other graphic files.

    `​``{R echo=F}
    getwd()
    #> [1] "D:/data/R/my_website_xmin/content/post"
    knitr::include_graphics('post/2020-06-09-an-html-document-with-aside-s_files/css.PNG',error=F)
    `​``
    
    in text we can use
    ![](/post/2020-06-09-an-html-document-with-aside-s_files/css.PNG)

Notice the difference in the (beginning of the) pathnames of the graphic file: for text (the markdown syntax) the pathname should start with a slash (/post/ ...) but in the R-code the slash should not be used (post/ ...). Also it is necessary to code error=F in the knitr::include_graphics call otherwise an error will occur because the path provided will not find the graphics file. In both cases the generated path in the html file will be ../../../../../post/ ...

insert footnotes

To insert a footnote use '[^fnptr]' 1 where fnptr is an arbitrary but unique (in this document) identifier that references a text that is defined by including '[^fnptr]:' (note the ':') at the end of the document.

test only one file in active package

testthat:::test_package_dir(package = 'tabr', 
   test_path = './tests/testthat',
   filter = 'phrase',
   reporter=check_reporter())

include code chunks in text

In the section above we showed three consecutive backticks . This is not so easy as it seems because we generated the section (in fact the whole entry) with knitr where three such backticks are ‘eaten’ after being considered to be code block delimeters. See explanation. The second block used method 1 (adding empty string) in the explanation and for the first block the second (zero-width space) method was used.

include paired asteriks (*) in text

Paired asteriks are used to italicise a phrase. When you just want to display a phrase within asteriks e.g. *italics* use the html escape code *  .

include non-breaking space in text

Use the html-code    .

include session_info chunk

Use

`​``{r child='../../local_files/session_info.Rmd'} 
`​`` 

to include the standard session_info chunk.

solution for empty line before last entry in list

After hugo version 0.57 I noticed that a list immediately followed by a code block forces an empty line between the list and its last entry. This can be solved by inserting a new line (so we see an empty line) and a comment like <!-- inserted to avoid incorrect list ending -->
after the last entry.

documentation with Roxygen2

Since version 2.7 there are several website with improved information about Roxygen2:

activate Git in RStudio

When an RStudio project is created without Git, there is no invisible indication of Git functionality. Select/open the terminal pane and make sure that the active folder is the main folder of the project. Then type git init in the terminal pane. After closing and reopening the project in RStudio the Git pane will show.

dates and times

The anytime package interprets characters in local time (i.e. ‘Europe/Berlin’ here) when asUTC == F and otherwise as a UTC time. When the time is given as a character it will be represented in the timezone specified with the tz argument and when none is specified in the local timezone. However when the time is a POSIX object no conversion to a timezone will take place.

HOQCutil::silent_library(anytime)
nowp = Sys.time() ; str(nowp) 
#>  POSIXct[1:1], format: "2021-04-09 10:00:28"
x=anytime(nowp,tz="America/New_York",asUTC=F) ;str(x) # will not convert POSIX !
#>  POSIXct[1:1], format: "2021-04-09 10:00:28"
nowc = as.character(nowp) ; str(nowc) # no longer POSIX but character only
#>  chr "2021-04-09 10:00:28"
x=anytime(nowc,tz="America/New_York",asUTC=F) ;str(x) # will convert to New York timezone
#>  POSIXct[1:1], format: "2021-04-09 04:00:28"
x=anytime(nowc,tz="America/New_York",asUTC=F) ;str(x+60*60*24) # New York time over a day
#>  POSIXct[1:1], format: "2021-04-10 04:00:28"

x=anytime(nowc,tz="Europe/Amsterdam",asUTC=T) ;str(x) # interpreted as UTC time and represented in Amsterdam time
#>  POSIXct[1:1], format: "2021-04-09 12:00:28"
x=anytime(nowc,asUTC=T) ;str(x) # interpreted as UTC time and represented in local (= Amsterdam) time
#>  POSIXct[1:1], format: "2021-04-09 12:00:28"
x=anytime(nowc,tz="America/New_York",asUTC=T) ;str(x) # interpreted as UTC time and represented in New York time
#>  POSIXct[1:1], format: "2021-04-09 06:00:28"
x=anytime(nowc,tz="America/New_York",asUTC=T) ;str(x+60*60*24) # New York time over a day
#>  POSIXct[1:1], format: "2021-04-10 06:00:28"

The website TWiki.org Service: Date and Time Gateway - Timezone Selector has an overview of possible timezone codes.

keyboard shortcuts

From Handboek Internetresearch & datajournalistiek and 47 Keyboard Shortcuts That Work in All Web Browsers

Ctrl+L: goto address field
Ctrl+mouse click: open the link you clicked on in an new tab
Ctrl+W: close the current tab
Ctrl+T: open a new tab
Ctrl+N: open a new window
Ctrl+Shift+N: open a new anonymous window
Ctrl+(+Shift)Tab: switch between tabs
Ctrl+R: refresh page
function F11: toggle fullscreen mode
Alt+(left/right)arrow: previous/next page
keep previous button pressed: history

Google (Groups) Common search techniques

From Common search techniques and Handboek Internetresearch & datajournalistiek :

Search social media: Put @ in front of a word to search social media. For example: @twitter
Search for a price: Put $ in front of a number. For example: camera $400
Search hashtags: Put # in front of a word. For example: #throwbackthursday
Exclude words from your search: Put - in front of a word you want to leave out. For example, jaguar speed -car
Search for an exact match: Put a word or phrase inside quotes. For example, “tallest building”
Search within a range of numbers: Put .. between two numbers. For example, camera $50..$100
Combine searches: Put “OR” between each search query. For example, marathon OR race
Search for a specific site: Put “site:” in front of a site or domain. For example, site:youtube.com or site:.gov
Search for related sites: Put “related:” in front of a web address you already know. For example, related:time.com
See Google’s cached version of a site: Put “cache:” in front of the site address
Restrict name to author (in Google Groups): Put “authorname:” in front of the name
Restrict searches: Put AND (or nothing) between each search query
Search nearby words : Put “NEAR” between two search wordse
Wildcard: Put * in search string or word

Footnotes:


  1. A footnote just to show that it can be done