R tips part1

GitHub repository (source: https://rstudio.github.io/shiny/tutorial/#deployment-local)
If your project is stored in a git repository on GitHub, then others can download and run your app directly. An example repository is at https://github.com/rstudio/shiny_example. The following command will download and run the application:

shiny::runGitHub('shiny_example', 'rstudio')
In this example, the GitHub account is ‘rstudio’ and the repository is ‘shiny_example’; you will need to replace them with your account and repository name.

Pros
Source code is easily visible by recipient (if desired)
Easy to run (for R users)
Very easy to update if you already use GitHub for your project
Git-savvy users can clone and fork your repository
Cons
Developer must know how to use git and GitHub
Code is hosted by a third-party server

Shiny

Pääsin kokeilemaan r-ohjelman shiny-ohjelmaa. Ensin asennettaan paketti:

install.packages(“rsconnect”)
install.packages(“RCurl”)

install.packages(“shiny”)

Installing package into ‘/home/marko/R/i686-pc-linux-gnu-library/3.4’
(as ‘lib’ is unspecified)
also installing the dependencies ‘httpuv’, ‘xtable’, ‘R6’, ‘sourcetools’

Sitten ajetaan ohjelma:
library(shiny)

runExample(“01_hello”)
Esimerkki löytyy täältä shiny sivuiltani.

Tässä koodia:

> #initialize
> library(datasets)
> library(ggplot2)
>
> #helper function (convert vector to named list)
> namel<-function (vec){
+ tmp<-as.list(vec)
+ names(tmp)<-as.character(unlist(vec)) + tmp + } > # shiny server side code for each call
> shinyServer(function(input, output, session){
+ #update variable and group based on dataset
+ output$variable <- renderUI({
+ obj<-switch(input$dataset,
+ “iris” = iris,
+ “mtcars” = mtcars)
+ var.opts<-namel(colnames(obj))
+ selectInput(“variable”,”Variable:”, var.opts) # uddate UI
+ })
+
+ output$group <- renderUI({
+ obj<-switch(input$dataset,
+ “iris” = iris,
+ “mtcars” = mtcars)
+ var.opts<-namel(colnames(obj))
+ selectInput(“group”,”Groups:”, var.opts) # uddate UI
+ })
+
+ output$caption<-renderText({
+ switch(input$plot.type,
+ “boxplot” = “Boxplot”,
+ “histogram” = “Histogram”,
+ “density” = “Density plot”,
+ “bar” = “Bar graph”)
+ })
+
+
+ output$plot <- renderUI({
+ plotOutput(“p”)
+ })
+
+ #plotting function using ggplot2
+ output$p <- renderPlot({
+
+ plot.obj<<-list() # not sure why input$X can not be used directly?
+ plot.obj$data<<-get(input$dataset)
+ plot.obj$variable<<-with(plot.obj$data,get(input$variable))
+ plot.obj$group<<-with(plot.obj$data,get(input$group))
+
+ #dynamic plotting options
+ plot.type<-switch(input$plot.type,
+ “boxplot” = geom_boxplot(),
+ “histogram” = geom_histogram(alpha=0.5,position=”identity”),
+ “density” = geom_density(alpha=.75),
+ “bar” = geom_bar(position=”dodge”)
+ )
+
+ require(ggplot2)
+ #plotting theme
+ .theme<- theme(
+ axis.line = element_line(colour = ‘gray’, size = .75),
+ panel.background = element_blank(),
+ plot.background = element_blank()
+ )
+ if(input$plot.type==”boxplot”) { #control for 1D or 2D graphs
+ p<-ggplot(plot.obj$data,
+ aes(
+ x = plot.obj$group,
+ y = plot.obj$variable,
+ fill = as.factor(plot.obj$group)
+ )
+ ) + plot.type
+
+ if(input$show.points==TRUE)
+ {
+ p<-p+ geom_point(color=’black’,alpha=0.5, position = ‘jitter’)
+ }
+
+ } else {
+
+ p<-ggplot(plot.obj$data,
+ aes(
+ x = plot.obj$variable,
+ fill = as.factor(plot.obj$group),
+ group = as.factor(plot.obj$group),
+ #color = as.factor(plot.obj$group)
+ )
+ ) + plot.type
+ }
+
+ p<-p+labs(
+ fill = input$group,
+ x = “”,
+ y = input$variable
+ ) +
+ .theme
+ print(p)
+ })
+ })

My -SciBlog- start today!

Welcome to MySci -page. Here I introduce some innovative and most interesting things about my trip into doctoral carreer. Now Im doctoral student in LUt and I wonder every day how amazing things you could do nowadays with your laptop. Data mining, AI and IoT will be in some role in my SciBlog. So… if you dare, just follow these pages!

Copyright MySci 2018
Tech Nerd theme designed by Siteturner