# R-programming-statistics

R is a programming language possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithm, linear regression, time series, statistical inference to name a few. Most of the R libraries are written in R,but many large companies also use R programming language, including Uber, Google, Airbnb, Facebook and so on

## Email subscriptions

Delivered by FeedBurner

## Making Dynamic HTML Maps with Leaflet

x <- c(1,3,5,2,11,9,3,9,12,3)
> y <- c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)
> z=sinh(root(x^2+Y^2))
Error in root(x^2 + Y^2) : could not find function "root"
> root(9)
Error in root(9) : could not find function "root"
> sq(9)
Error in sq(9) : could not find function "sq"
> n <- 10
> mycolors <- rainbow(n)
> pie(rep(1, n), labels=mycolors, col=mycolors)
> mygrays <- gray(0:n/n)
> pie(rep(1, n), labels=mygrays, col=mygrays)
> par(font.lab=3, cex.lab=1.5, font.main=4, cex.main=2)
> windowsFonts(
+     A=windowsFont("Arial Black"),
+     B=windowsFont("Bookman Old Style"),
+     C=windowsFont("Comic Sans MS")
+ )
> par(pin=c(4,3), mai=c(1,.5, 1, .2))
> plot(dose, drugA, type="b",
+      col="red", lty=2, pch=2, lwd=2,
+      main="Clinical Trials for Drug A",
+      sub="This is hypothetical data",
+      xlab="Dosage", ylab="Drug Response",
+      xlim=c(0, 60), ylim=c(0, 70))
Error in plot(dose, drugA, type = "b", col = "red", lty = 2, pch = 2,  :
> title(main="main title", sub="sub-title",
+       xlab="x-axis label", ylab="y-axis label")
> title(main="main title", sub="sub-title",
+       xlab="x-axis label", ylab="y-axis label")
> sinh(pie)
Error in sinh(pie) : non-numeric argument to mathematical function
> mpg = 41.4020-3.4179*wt-0.0824*disp+0.0001277*disp^2
>
> plot(density(rnorm(100)),main="Normal density",xlab="x")
>
> x=rnorm(100)
> library("ggplot2", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘ggplot2’ was built under R version 3.5.1
> library(ggplot2)
> xes <- cbind(seq(0, 10, length.out = 100),
+              seq(2.5, 12.5, length.out = 100),
+              seq(5, 15, length.out = 100),
+              seq(7.5, 17.5, length.out = 100))
>
> matplot(x = xes, y = xmat, type = 'l')
Sepal.Length Sepal.Width Petal.Length
1          5.1         3.5          1.4
2          4.9         3.0          1.4
3          4.7         3.2          1.3
4          4.6         3.1          1.5
5          5.0         3.6          1.4
6          5.4         3.9          1.7
Petal.Width Species
1         0.2  setosa
2         0.2  setosa
3         0.2  setosa
4         0.2  setosa
5         0.2  setosa
6         0.4  setosa
> boxplot(iris[,1],xlab="Sepal.Length",ylab="Length(in centemeters)",
+         main="Summary Charateristics of Sepal.Length(Iris Data)")
> ggplot(iris, aes(x = Petal.Width, y = Petal.Length, color = Species)) +
+     geom_point()
>
> miris <- iris
> boxplot(Petal.Width~Species.o, data = miris,
+         xlab = "Species", ylab = "Petal Width",
+         main = "Iris Data, ordered by mean sepal width", varwidth = TRUE,
+         col = 2:4)
> library("maptools", lib.loc="~/R/win-library/3.5")
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
Warning messages:
1: package ‘maptools’ was built under R version 3.5.2
2: package ‘sp’ was built under R version 3.5.2
> library(maps)
Warning message:
package ‘maps’ was built under R version 3.5.2
> library(maps)
> library("heatmap3", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘heatmap3’ was built under R version 3.5.2
> library("mime", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘mime’ was built under R version 3.5.1
> library("viridisLite", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘viridisLite’ was built under R version 3.5.1
> install.packages("leaflet")
(as ‘lib’ is unspecified)
also installing the dependencies ‘gridExtra’, ‘viridis’

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/gridExtra_2.3.zip'
Content type 'application/zip' length 1108401 bytes (1.1 MB)

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/viridis_0.5.1.zip'
Content type 'application/zip' length 1866609 bytes (1.8 MB)

trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/leaflet_2.0.2.zip'
Content type 'application/zip' length 2349843 bytes (2.2 MB)

package ‘gridExtra’ successfully unpacked and MD5 sums checked
package ‘viridis’ successfully unpacked and MD5 sums checked
package ‘leaflet’ successfully unpacked and MD5 sums checked

> library(leaflet)
Warning message:
package ‘leaflet’ was built under R version 3.5.2
> library(leaflet)
> data.frame(state.name, state.center) %>%
+     leaflet() %>%
+     addMarkers(lng = ~x, lat = ~y,
+                popup = ~state.name,
+                clusterOptions = markerClusterOptions())
> ui <- fluidPage(
+     sliderInput(inputId = "slider",
+                 label = "values",
+                 min = 0,
+                 max = 100,
+                 value = 0,
+                 step = 1),
+     leafletOutput("my_leaf")
+ )
Error in fluidPage(sliderInput(inputId = "slider", label = "values", min = 0,  :
could not find function "fluidPage"
>
> library(plotly)

Attaching package: ‘plotly’

The following object is masked from ‘package:ggplot2’:

last_plot

The following object is masked from ‘package:stats’:

filter

The following object is masked from ‘package:graphics’:

layout

Warning message:
package ‘plotly’ was built under R version 3.5.2
> library(plotly)
> dfb <- world.cities[world.cities\$country.etc=="Brazil",]
> dfb\$poph <- paste(dfb\$name, "Pop", round(dfb\$pop/1e6,2), " millions")
> dfb\$q <- with(dfb, cut(pop, quantile(pop), include.lowest = T))
>
> levels(dfb\$q) <- paste(c("1st", "2nd", "3rd", "4th"), "Quantile")
> dfb\$q <- as.ordered(dfb\$q)
>
> ge <- list(
+     scope = 'south america',
+     showland = TRUE,
+     landcolor = toRGB("gray85"),
+     subunitwidth = 1,
+     countrywidth = 1,
+     subunitcolor = toRGB("white"),
+     countrycolor = toRGB("white")
+ )

>
> plot_geo(dfb, lon = ~long, lat = ~lat, text = ~poph,
+          marker = ~list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
+          color = ~q, locationmode = 'country names') %>%
+     layout(geo = ge, title = 'Populations<br>(Click legend to toggle)')
No scattergeo mode specifed:
Setting the mode to markers
Warning message:
package ‘bindrcpp’ was built under R version 3.5.2
> plot_geo(dfb, lon = ~long, lat = ~lat, text = ~poph,
+          marker = ~list(size = sqrt(pop/10000) + 1, line = list(width = 0)),
+          color = ~q, locationmode = 'country names') %>%
+ layout(geo = ge, title = 'Populations<br>(Click legend to toggle)')
No scattergeo mode specifed:
Setting the mode to markers
> library(shiny)
> ui <- fluidPage(
+     leafletOutput("my_leaf")
+ )
> server <- function(input, output, session){
+
+     output\$my_leaf <- renderLeaflet({
+
+         leaflet() %>%
+             setView(lat = -37.8, lng = 144.8, zoom = 10)
+
+     })
+
+ }
>
> shinyApp(ui, server)

Listening on http://127.0.0.1:3243

> ui <- fluidPage(
+     sliderInput(inputId = "slider",
+                 label = "values",
+                 min = 0,
+                 max = 100,
+                 value = 0,
+                 step = 1),
+     leafletOutput("my_leaf")
+ )
>
> server <- function(input, output, session){
+     set.seed(123456)
+     df <- data.frame(latitude = sample(seq(-38.5, -37.5, by = 0.01), 100),
+                      longitude = sample(seq(144.0, 145.0, by = 0.01), 100),
+                      value = seq(1,100))
+ output\$my_leaf <- renderLeaflet({
+
+     leaflet() %>%
+         setView(lat = -37.8, lng = 144.8, zoom = 8)
+
+ })
+
+ df_filtered <- reactive({
+     df[df\$value >= input\$slider, ]
+ })
+
+ observe({
+     leafletProxy(mapId = "my_leaf", data = df_filtered()) %>%
+         clearMarkers() %>% ## clear previous markers
+ })
+
+ }
>
> shinyApp(ui, server)

Listening on http://127.0.0.1:3243
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively
Assuming "longitude" and "latitude" are longitude and latitude, respectively

> data.frame(state.name, state.center) %>%
+     leaflet() %>%
+     addMarkers(lng = ~x, lat = ~y,
+                popup = ~state.name,
+                clusterOptions = markerClusterOptions())
> shinyUI(fluidPage(
+     # Application title
+     titlePanel("Hello World!")
+ ))
<div class="container-fluid">
<h2>Hello World!</h2>
</div>
>
> shinyServer(function(input, output){})
>
> ui <- fluidPage(  checkboxGroupInput("checkGroup1", label = h3("This is a Checkbox group"),
+                                      choices = list("1" = 1, "2" = 2, "3" = 3),
+                                      selected = 1),
+                   fluidRow(column(3, verbatimTextOutput("text_choice")))
+ )
> server <- function(input, output){
+     output\$text_choice <- renderPrint({
+         return(paste0("You have chosen the choice ",input\$checkGroup1))})
+ }
>
> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:3243

> ui <- fluidPage(
+                  label = HTML('<FONT color="red"><FONT size="5pt">Welcome</FONT></FONT><br> <b>Your
+ favorite color is red ?</b>'),
+                  choices = list("TRUE" = 1, "FALSE" = 2),
+                  selected = 1,
+                  inline = T,
+                  width = "100%"),fluidRow(column(3, textOutput("value"))))
>
> server <- function(input, output){
+     output\$value <- renderPrint({
+         else{return("Sorry !")}})}
>
> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:3243

> ui <- fluidPage(
+     selectInput("id_selectInput",
+                 label = HTML('<B><FONT size="3">What is your favorite color ?</FONT></B>'),
+                 multiple = TRUE,
+                 choices = list("red" = "red", "green" = "green", "blue" = "blue", "yellow" = "yellow"),
+                 selected = NULL),
+     br(), br(),
+     fluidRow(column(3, textOutput("text_choice"))))
> server <- function(input, output){
+     output\$text_choice <- renderPrint({
+         return(input\$id_selectInput)})
+ }
> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:3243

> ui <- fluidPage() #Create the ui
> server <- function(input, output){} #create the server
> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:3243

> shinyApp(appFile="path_to_my_R_file_containig_the_app")
Error in shinyApp(appFile = "path_to_my_R_file_containig_the_app") :
unused argument (appFile = "path_to_my_R_file_containig_the_app")
>
> ui <- shinyUI(fluidPage(
+     titlePanel("Basic widgets"),
+     fluidRow(
+
+         column(3,
+                h3("Buttons"),
+                actionButton("action", label = "Action"),
+                br(),
+                br(),
+                submitButton("Submit")),
+
+         column(3,
+                h3("Single checkbox"),
+                checkboxInput("checkbox", label = "Choice A", value = TRUE)),
+
+         column(3,
+                checkboxGroupInput("checkGroup",
+                                   label = h3("Checkbox group"),
+                                   choices = list("Choice 1" = 1,
+                                                  "Choice 2" = 2, "Choice 3" = 3),
+                                   selected = 1)),
+
+         column(3,
+                dateInput("date",
+                          label = h3("Date input"),GoalKicker.com – R Notes for Professionals 218
Error: unexpected input in:
"               dateInput("date",
label = h3("Date input"),GoalKicker.com –"
>                          library(shiny)
>                          ui <- fluidPage() #Create the ui
>                          server <- function(input, output){} #create the server
>                          shinyApp(ui = ui, server = server) #run the App