第 3 章 Process of Data Visualization
3.1 Drake: Program planning
Drake package:
3.1.1 Command block
For each command block, it behaves like a function body.
- It can pass a value in the LAST line that produces a print oput.
- Either global environment objects or target values can be used from outside the command block. Therefore, objects in other blocks can not be accessed unless it is passed to a target.
3.1.2 Build a plan
<- 3
a <- 15
cc <- drake::drake_plan(
examplePlan target1 = {
+ 5 -> b
a <- 0
cc if (b < 10) b
},target2 = {
cc
},target3 = {
+ a
target1
} )
Each target can only access other target values or object values in the global environment.
3.1.3 Make it happen
make(examplePlan)
3.1.4 Access target ex post
readd(target1)
readd(target2)
readd(target3)
or
loadd(target1)
loadd(target2)
loadd(target3)
If your work in a R project directory, once a drake plan is make()-ed, you can access target value via readd()
/loadd()
even if you restart your R session.
- All targets are cached unless
clean()
is called.
3.1.5 Target dependency
vis_drake_graph(examplePlan)
3.1.6 Source and Output
<-
planExample ::drake_plan(
draketarget1 = {
download.file("https://.....")
},:
:
targetN = {
save(someObject, "myResult.rda")
} )
file_in/file_out
<-
planExample ::drake_plan(
draketarget1 = {
download.file(file_in("https://....."))
},:
:
targetN = {
save(someObject, file_out("myResult.rda"))
} )
file_in
: once"https://....."
content change, it will trigger remaking target1.file_out
: once"myResult.rda"
is deleted, it will trigger remaking targetN.
file_in
/file_out
only support exogenous path specification, i.e. one "..."
completes the path specificatioin. Paths generated by some operations like paste0(myroot,"data.txt")
are not supported.
3.2 Cache
3.2.1 SOAR package
library(SOAR)
Attach() # attach .R_Cache
<- 5
obj1 Store(obj1)
<- 3 # Won't change obj1 in cache, but create an obj1 in global environment.
obj1 Store(obj1) # Unless store it in cache.
Remove(obj1)
3.2.2 econDV::%=%
object %=% value
: create object with value at the first time, and Store(object). When runobject %=% value
the second time, this command will be ignore.
library(WDI)
<-
import_search WDIsearch(string = "Imports of goods and services")
<-
world_import WDI(indicator = import_search[12, 1])
system.time(
<-
world_import WDI(indicator = import_search[12, 1])
)
library(econDV)
system.time(
%=%
world_import WDI(indicator = import_search[12, 1])
)
- If
world_import
exists, it will be stored in R.Cache environment.
3.3 rmd2drake: Equip R Markdown with Drake
convert Rmd to a drake plan R script, which includes corresponding make/visualization function
can apply to multiple plans; each has its own cache.
3.3.1 Addins
Boxy SVG
probably only work in Mac OS
::svgOpen("....svg") rmd2drake