Definitely, animation package do not provide any interface for shiny app. Take grad.desc() for example, if we want to build an dynamic app that control all arguments of grad.desc() on app panel, we need split the function into sections and rewrite each section into server.R file.

I have another simple method to generate anmiation with shiny app. The method is generate pictures and use shiny app to show all pictures as slides.

Firstly, I need generate animation frames, use saveHTML() can get all pictures in images folder.

saveHTML({
         ani.options(interval = 0.3)
         grad.desc()
     }, img.name = "grad.desc", htmlfile = "grad.desc.html", ani.height = 500, 
         ani.width = 500, title = "Demonstration of the Gradient Descent Algorithm", 
         description = "The arrows will take you to the optimum step by step.")

Then, write a slide-app.

ui.R

library(shiny)
shinyUI(fluidPage(
  # Application title
  titlePanel("Grad.desc demo"),
  # Sidebar with a slider input for the number of frames
  sidebarLayout(
    sidebarPanel(
      sliderInput('myslider', 
                  'Steps', 
                  min=1, 
                  max=35, # count all frames(pictures) 
                  value=1, 
                  animate=animationOptions(interval = 0,loop=T)
      )
    ),  
    # Show ui
    mainPanel(
      uiOutput("ui")
    )
  )
))

server.R

library(shiny)
shinyServer(function(input, output, session) {
  imgurl <- reactive({
    i=input$myslider
    return(paste0("./images/grad.desc",i,".png")) #the path of pictures
  })
  output$ui <- renderUI({
    tags$div(
      tags$img(src = imgurl())
    )
  })
})

The directory structure.

.
|-- server.R
|-- ui.R
`-- www
    `-- images # images folder

Finally, runApp("demo") you would see the demo app running successfully!

Imgur


Is there any way to generate YAML front matter block of each markdown files automatically?

The answer is yes! And there is more than one way to do it.

I want to introduce two ways, one is using rake tool, the other is using bash.

Rake tool

To generate YAML front matter blockk in markdown file, we need a program that automatically write a markdown file with YAML front matter block.

Here is the program tool “Rake”.

Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax.

Maybe you have some questions:

What is Make-like program ?

In software development, Make is a utility that automatically builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program.

Makefiles are special format files that together with the make utility will help you to automagically build and manage your projects.

Do we need to write a makefile for Rake?

Of course. We need write a “Rakefile” for Rake utility.

Rakefiles (rake’s version of Makefiles) are completely defined in standard Ruby syntax. No XML files to edit. No quirky Makefile syntax to worry about (is that a tab or a space?)

Using Rake to generate YAML front matter block, step by step:

1.install Rake

gem install rake

Here is the simple usage of rake.

2.write Rakefile

Here is the Rakefile Format.

Tl;dr: Just see below.

require 'time'
# Usage: rake post title="A Title" date="2014-04-14" ## date is alternatively part
desc "Create a new post"
task :post do
  unless FileTest.directory?('./_posts')
    abort("rake aborted: '_posts' directory not found.")
  end
  title = ENV["title"] || "new-post"
  slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
  begin
    date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now)
    .strftime('%Y-%m-%d')
  rescue Exception => e
    puts "Error: date format must be YYYY-MM-DD!"
    exit -1
  end
  filename = File.join('.', '_posts', "#{date}-#{slug}.md")
  if File.exist?(filename)
    abort("rake aborted: #{filename} already exists.")
  end
  puts "Creating new post: #{filename}"
  open(filename, 'w') do |post|
    post.puts "---"
    post.puts "published: true" 
    post.puts "layout: post"
    post.puts "title: #{title.gsub(/-/,' ')}"
    post.puts "author: Yu" # change to your name
    post.puts "category:"
    post.puts "tags:"
    post.puts "---"
  end
end

You can save those code in a Rakefile, and move the Rakefile to a jekyll website folder, for example yulijia.github.io/. When build a new post, we only need type rake post title="Hello, World" on terminal in the jekyll website folder.

BASH script

If you do not like rake tool, you can use bash to generate YAML front matter block.

Here is the script:

#!/bin/bash
read -p "Enter the title: " title 
Title=`echo $title | tr -d '[:punct:]'`
for word in $Title
do
  dashedTitle=${dashedTitle}-${word}
done
filename="`date +%Y-%m-%d`${dashedTitle}.md"
touch $filename
echo "---" >> $filename
echo "published: true" >> $filename
echo "layout: post" >> $filename
echo "title: \"${title}\"" >> $filename
echo "author: Yu" >> $filename
echo "categories:" >> $filename
echo "tags:" >> $filename
echo "-" >> $filename
echo "---" >> $filename
echo "" >> $filename

This article is definitely a advertisement :p

Last year, I made two Jekyll themes, one is Freshman, the other is Freshman21. Freshman is a simple html/css theme that I made to learn Jekyll and css. Freshman21 looks like WordPress theme. A tribute to Twenty Twelve and Twenty eleven. I send a pull request to jekyllthemes.org, thanks to Matt Harzewski, my theme is already updated to the website in January.

If you are a fan of WordPress theme and try using Jekyll to build your own blog, may the Freshman21 be with you.

Freshman21 has two version: one-colunm and two-columns.

Welcome to submit new request to the theme at github.


Today, I googled “A paper a day” to find if there any slogan about reading papers. Then I found this article A paper a day keeps the doctor away – advice for surviving in academia.

The author give us some useful advice for surviving in academia.

  • There is no single road to the Ivory Tower.
  • Find your balance.
  • Try not to be an ass.
  • Don’t be bitter.
  • Keep up with the literature, “A paper a day”.

Live long and prosper, folks.


Winter is coming, I am back!

Poor drawing skill, I almost forgot how to use PhotoShop.

I_am_back