Advanced inputs and outputs

# Define safe_readLines()

safe_readLines <- safely(readLines)

# Use the safe_readLines() function with map(): html

html <- map(urls, safe_readLines)

# Call str() on html

str(html)

# Extract the result from one of the successful elements

html[["example"]][["result"]]

# Define save_readLines() and html

safe_readLines <- safely(readLines)

html <- map(urls, safe_readLines)

# Examine the structure of transpose(html)

str(transpose(html))

# Extract the results: res

res<-transpose(html)[["result"]]

# Extract the errors: errs

errs<-transpose(html)[["error"]]

# Initialize some objects

safe_readLines <- safely(readLines)

html <- map(urls, safe_readLines)

res <- transpose(html)[["result"]]

errs <- transpose(html)[["error"]]

# Create a logical vector is_ok

is_ok<-map_lgl(errs,is_null)

# Extract the successful results

res[is_ok]

# Extract the input from the unsuccessful results

urls[!is_ok]

# Initialize n and mu

n <- list(5, 10, 20)

mu <- list(1, 5, 10)

# Create a sd list with the values: 0.1, 1 and 0.1

sd<-list(0.1,1,0.1)

# Edit this call to pmap() to iterate over the sd list as well

pmap(list(n, mu,sd), rnorm)

# Define list of functions

f <- list("rnorm", "runif", "rexp")

# Parameter list for rnorm()

rnorm_params <- list(mean = 10)

# Add a min element with value 0 and max element with value 5

runif_params <- list(min=0,max=5)

# Add a rate element with value 5

rexp_params <- list(rate=5)

# Define params for each function

params <- list(

rnorm_params,

runif_params,

rexp_params

)

# Call invoke_map() on f supplying params as the second argument

invoke_map(f,params, n = 5)

# Turn this snippet into find_breaks()

find_breaks<-function(x){

rng <- range(x, na.rm = TRUE)

seq(rng[1], rng[2], length.out = 30)

}

?seq

# Call find_breaks() on sims[[1]]

find_breaks(sims[[1]])

# Increase sample size to 1000

sims <- invoke_map(f, params, n = 1000)

# Compute nice_breaks (don't change this)

nice_breaks <- map(sims, find_breaks)

# Create a vector nice_titles

nice_titles<-c("Normal(10, 1)","Uniform(0, 5)","Exp(5)")

# Use pwalk() instead of walk2()

pwalk(list(sims,nice_breaks,main=nice_titles), hist,xlab="")

# Pipe this along to map(), using summary() as .f

sims %>%

walk(hist) %>% map(summary)

你可能感兴趣的:(Advanced inputs and outputs)