You are viewing a version of the article as of Jun 10 2018, 21:46.Switch to latest draft from Jun 10 2018, 21:46.

R Template

This article can act as a starting point for any basic R article. It has cells to accomplish some basic setup and programming tasks, as well as a few more complex ones. Just Remix and go!

For explanations of what's going on, see this article on Bash setup and this one on R setup and coding.

1.
System setup

apt-get update >> /dev/null
apt-get install -y fortune cowsay
/usr/games/fortune | /usr/games/cowsay




















































pwd
ls -al
mkdir -p ~/.keras
echo '{
    "epsilon": 1e-07, 
    "image_data_format": "channels_last", 
    "backend": "cntk", 
    "floatx": "float32" 
}' > ~/.keras/keras.json




























# curl -O https://nextjournal.com/images/logo.svg
wget https://nextjournal.com/images/logo.svg

cp ~/.keras/keras.json /results/
cp logo.svg /results/











keras.json

2.
Installing R packages

install.packages("readr")














































































































































































































































































































































































































































































































devtools::install_github("hadley/bookdown")








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































3.
File Handling

cubic.csv
data <- read.csv(cubic.csv, header=FALSE)
library(readr)

# testtext <- read_file() # re-add ref when bug is fixed
testtext <- read_file("/root/.keras/keras.json")

writeLines(testtext)







4.
Calculations

# can use data directly, or import value returned by read uploaded file cell
splitdata <- nilread uploaded file

for (i in seq_along(splitdata$V1)) {
  splitdata$V1[i] <- splitdata$V1[i] * (-1)^(i+1)
}

splitdata$V1 # extract list and return, for use by Javascript cell

5.
Plotting

plot_ly(data = data, y = ~V1, 
        type = "scatter", mode = "markers") %>%
    layout(title="Cubic Function",
           xaxis = list(title = "x"),
           yaxis = list(title = "f(x)")
          )
# Temporary workarounds for Issue #1974
dev.off(which="pdf")
dev.off(which="png")
pdf(NULL)
png("/tmp/foo.png")

p <- ggplot(data = splitdata, 
            aes(x = seq_along(V1), y = V1)
        ) + geom_line() + geom_point() +
  labs(title="Cubic Function", x="x", y="f(x)")

ggplotly(p)
var data = nilmodify uploaded data

plottypes = ['lines','markers']

var traces = plottypes.map(function(ptype) {
  return { type: 'scatter', mode: ptype,
    name: ptype,
    x: [...Array(401).keys()],
    y: data
  }
})

var layout = { title: 'Split Cubic Function',
  xaxis: { title: 'Year' },
  yaxis: { title: 'Temperature [C]' }
}

Nextjournal.plot(traces, layout)