(This article was first published on ** R – jannesm**, and kindly contributed to R-bloggers)

Today we are proud to announce a major release of the RQGIS package providing an interface between R and QGIS. We have completeley rewritten RQGIS by now using reticulate to establish a tunnel to the Python QGIS API. To make RQGIS even more user-friendly, we have implemented, among others, following features:

`set_env`

now caches its output, so you have to call it only once`find_algorithms`

now supports regular expressions`run_qgis`

now accepts R named arguments as input (see example below). The`load_output`

argument is now logical. If`TRUE`

all specified output files will be loaded into R.- RQGIS now supports simple features

For a detailed overview of all changes, please refer to the release news.

Let’s use RQGIS for calculating the slope and the aspect of a digital elevation model. First of all, we have to attach RQGIS and a digital elevation model (dem) to the global environment:

```
library("RQGIS")
data("dem", package = "RQGIS")
```

Next, we need to find all paths necessary to run successfully the Python QGIS API. Please note, that the output of `set_env`

will be cached and reused in subsequent function calls. Note also, that `set_env`

is always faster when you indicate the path to your QGIS installation, in my case e.g., `set_env(root = "C:/OSGeo4W64")`

. Secondly, we open a QGIS Python application with `open_app`

.

```
set_env()
open_app()
```

Now, that we have established a tunnel to the QGIS Python API, we are ready for some geoprocessing. First, we need a geoalgorithm, that computes the slope and the aspect of a digital elevation model. To find the name of such a geoalgorithm, we use a regular expression which searches for the terms `slope`

and `aspect`

in all available QGIS geoalgorithms.

`find_algorithms("slope(.+)?aspect")`

```
## [1] "r.slope.aspect - Generates raster layers of slope, aspect, curvatures and partial derivatives from a elevation raster layer.--->grass7:r.slope.aspect"
## [2] "Slope, aspect, curvature----------------------------->saga:slopeaspectcurvature"
## [3] "r.slope.aspect - Generates raster layers of slope, aspect, curvatures and partial derivatives from a elevation raster layer.--->grass:r.slope.aspect"
```

We will use `grass7:r.slope.aspect`

. To retrieve its function parameters and arguments you can run `get_usage("grass7:r.slope.aspect")`

. Use `get_args_man`

to collect the parameter-argument list including all default values:

`params <- get_args_man("grass7:r.slope.aspect")`

As with previous RQGIS releases, you can still modify the parameter-argument list and submit it to `run_qgis`

:

```
params$elevation <- dem
params$slope <- "slope.tif"
params$aspect <- "aspect.tif"
run_qgis(alg = "grass7:r.slope.aspect", params = params)
```

But now you can also use R named arguments in `run_qgis`

, i.e. you can specify the geoalgorithms parameters directly in run_qgis (adapted from package rgrass7). Here, we specify the input- and the output-rasters. For all other parameters, default values will automatically be used. For more information on the R named arguments, please refer also to the documentation by running `?run_qgis`

and/or `?pass_args`

.

```
out <- run_qgis(alg = "grass7:r.slope.aspect", elevation = dem,
slope = "slope.tif", aspect = "aspect.tif",
load_output = TRUE)
```

```
## $slope
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\RtmpmsSSA4/slope.tif"
##
## $aspect
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\RtmpmsSSA4/aspect.tif"
##
## $dxy
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\ac7b8544e8194dd1a1b8710e6091f1f3\\dxy.tif"
##
## $dxx
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\1576d9dc93434a578b3aeb16bedb17a2\\dxx.tif"
##
## $tcurvature
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\afea27676cc049faaa8526a486f13f70\\tcurvature.tif"
##
## $dx
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\2d71fd26b1aa4868a5cdfd0d7ad47a0c\\dx.tif"
##
## $dy
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\458f38f6c71947d3a37532e4bc6a6a53\\dy.tif"
##
## $pcurvature
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\80ad6fa1843d4d3a92ed0b4c6a39dcfa\\pcurvature.tif"
##
## $dyy
## [1] "C:\\Users\\pi37pat\\AppData\\Local\\Temp\\processing5bb46293bfb243f092a57ce9cf50348b\\6c52d235c8614a719954f1f744e3fef1\\dyy.tif"
```

Setting `load_output`

to `TRUE`

automatically loads the resulting QGIS output back into R. Since we have specified two ouput files, the output will be loaded into R as a list (here named `out`

) with two elements: a slope and an aspect raster. However, in the background, QGIS calculates all terrain attributes and derivatives provided by `grass7:r.slope.aspect`

, and saves them to a temporary location. Of course, if you wish you can still access these layers from there.

Before running RQGIS, you need to install third-party software. We wrote a package vignette, which guides you through the installation process of QGIS, GRASS and SAGA on various platforms. To access the vignette, please run:

`vignette("install_guide", package = "RQGIS")`

For more information on RQGIS and examples how to use RQGIS, please refer to its github page and my blog.

To **leave a comment** for the author, please follow the link and comment on their blog: ** R – jannesm**.

R-bloggers.com offers

(This article was first published on ** R – Incidental Ideas**, and kindly contributed to R-bloggers)

It’s been a while since my last post on some TB WHO data. A lot has happened since then, including the opportunity to attend the Open Data Science Conference (ODSC) East held in Boston, MA. Over a two day period I had the opportunity to listen to a number of leaders in various industries and fields. It… Continue Reading →

To **leave a comment** for the author, please follow the link and comment on their blog: ** R – Incidental Ideas**.

R-bloggers.com offers

(This article was first published on ** R-exercises**, and kindly contributed to R-bloggers)

**Shiny Application Layouts – Shiny Themes**

In the last part of the series we will check out which themes are available in the `shinythemes`

package. More specifically we will create a demo app with a selector from which you can choose the theme you want.

This part can be useful for you in two ways.

First of all, you can see different ways to enhance the appearance and the utility of your shiny app.

Secondly you can make a revision on what you learnt in “Building Shiny App” series and in this series, as we will build basic shiny staff in order to present it in the proper way.

Read the examples below to understand the logic of what we are going to do and then test yous skills with the exercise set we prepared for you. Lets begin!

Answers to the exercises are available here.

**Create the App.**

In order to see how themes affect the application components ,we have seen until now, we need to create them.

As we are going to use `tags`

here, a good idea is to use `taglList()`

in order to create our app. `TagList()`

is ideal for users who wish to create their own sets of tags.

Let’s see an example of the skeleton of our application and then create our own step by step before applying to it the theme selector.

#ui.R

`tagList(`

navbarPage(

"Title",

tabPanel("Navbar 1",

sidebarPanel(

sliderInput("slider","Slider input:", 1, 100, 50),

tags$h5("ActionButton:"),

actionButton("action", "Action")

),

mainPanel(

tabsetPanel(

tabPanel("Table",

h4("Table"),

tableOutput("table")),

tabPanel("VText",

h4("Verbatim Text"),

verbatimTextOutput("vtxt")),

tabPanel("Header",

h1("Header 1"),

h2("Header 2"))

)

)

),

tabPanel("Navbar 2")

))

#server.R

`function(input, output) {`

output$vtxt <- renderText({

paste(input$slider)

})

output$table <- renderTable({

iris

```
```

` })`

}

**Exercise 1**

Create a UI using tag List with the form of a Navbar Page and name it “Themes”. **HINT**: Use `tagList()`

and `navbarPage()`

.

**Exercise 2**

Your Navbar Page should have two tab Panels named “Navbar 1” and “Navbar 2”. **HINT**: Use `tabPanel()`

.

**Exercise 3**

In “Navbar 1” add sidebar and main panel. **HINT**: Use `sidebarPanel()`

and `mainPanel()`

.

**Exercise 4**

Create three tab panels inside the main panel. Name them “Table”, “Text” and “Header” respectively. **HINT**: Use `tabsetPanel()`

and `tabPanel()`

.

**Exercise 5**

In the tab panel “Table” add a table of the `iris`

dataset. Name it “Iris”. **HINT** : Use `tableOutput()`

and `renderTable({})`

.

**Exercise 6**

In the tab panel “Text” add verbatim Text nad name it “Vtext”. **HINT**: Use `verbatimTextOutput()`

.

**Exercise 7**

Add a slider and an actionbutton in the sidebar. Connect the slider with the “Text” tab panel. Use tags to name the actionbutton. **HINT**: Use `sliderInput()`

,`actionButton()`

, `renderText()`

and `tags`

.

**Exercise 8**

In the tab panel “Header” add two headers with size `h1`

and `h2`

respectively.

**Shinythemes**

**Exercise 9**

Install and load the package `shinythemes`

.

**Exercise 10**

Place `themeSelector()`

inside your `tagList()`

and then use different shiny themes to see how they affect all the components you created so far.

To **leave a comment** for the author, please follow the link and comment on their blog: ** R-exercises**.

R-bloggers.com offers

(This article was first published on ** R code – Serious Stats**, and kindly contributed to R-bloggers)

In an earlier blog post I provided R code for a CI of a difference in R square for dependent and non-dependent correlations. This was based on a paper by Zou (2007). That paper also provides a method for calculating the CI of a difference in independent R square coefficients based on the limits of the CI for a single R square coefficient. I’ve also been experimenting with knittr and unfortunately haven’t yet worked out how to merge the R markdown output with my blog template, so I’m linking to RPubs for convenience.

You can find the function and a few more details here.

Filed under: comparing correlations, confidence intervals, R code, serious stats Tagged: confidence intervals, R, R code, software, statistics

To **leave a comment** for the author, please follow the link and comment on their blog: ** R code – Serious Stats**.

R-bloggers.com offers

(This article was first published on ** Revolutions**, and kindly contributed to R-bloggers)

Data scientist Giora Simchoni recently published a fantastic analysis of the history of pop songs on the Billboard Hot 100 using the R language. Giora used the rvest package in R to scrape data from the Ultimate Music Database site for the 350,000 chart entries (and 35,000 unique songs) since 1940, and used those data to create and visualize several measures of song popularity over time.

A novel measure that Giora calculates is "area under the song curve": the sum of all the ranks above 100 for every week the song is in the Hot 100. By that measure, the most popular (and also longest-charting) song of all time is Radioactive by Imagine Dragons:

It's turns out that calculating this "song integral" is pretty simple in R when you use the tidyverse:

calculateSongIntegral <- function(positions) { sum(100 - positions) } billboard %>% filter(EntryDate >= date_decimal(1960)) %>% group_by(Artist, Title) %>% summarise(positions = list(ThisWeekPosition)) %>% mutate(integral = map_dbl(positions, calculateSongIntegral)) %>% group_by(Artist, Title) %>% tally(integral) %>% arrange(-n)

Another fascinating chart included in Giora's post is this analysis of the most frequent words to appear in song titles, by decade. He used the tidytext package to extract individual words from song titles and then rank them by frequency of use:

So it seems as though Love Is All Around (#41, October 1994) after all! For more analysis of the Billboard Hot 100 data, including Top-10 rankings for various measures of song popularity and the associated R code, check out Giora's post linked below.

Sex, Drugs and Data: Billboard Bananas

To **leave a comment** for the author, please follow the link and comment on their blog: ** Revolutions**.

R-bloggers.com offers

The post The Sierpinski Triangle: Visualising infinity in R appeared first on The Devil is in the Data.

]]>
(This article was first published on ** The Devil is in the Data**, and kindly contributed to R-bloggers)

Wacław Sierpiński was a mathematical genius who developed several of the earliest fractals. The Sierpiński triangle is an easy to conceptualise geometrical figure but it hides a fascinating mathematical complexity. Start by drawing an equilateral triangle and draw another one in its centre. Then draw equilateral triangles in the four resulting triangles, and so on, *ad infinitum*.

The original Sierpinski triangle will eventually disappear into Cantor dust, a cloud of ever shrinking triangles of infinitesimal size. The triangle is self-similar, no matter how far you zoom in, the basic geometry remains the same.

A fascinating method to create a Sierpinski Triangle is a chaos game. This method uses random numbers and some simple arithmetic rules. Sierpinski Triangles can be created using the following six steps:

- Define three points in a plane to form a triangle.
- Randomly select any point on the plane.
- Randomly select any one of the three triangle points.
- Move half the distance from your current position to the selected vertex.
- Plot the current position.
- Repeat from step 3.

This fractal is an implementation of chaos theory as this random process attracts to a complex ordered geometry. The game only works with random numbers and when selecting random vertices of the triangle.

This code implements the six rules in R. The code first initializes the triangle, defines a random starting point and then runs a loop to place random dots. The R plot engine does not draw pixels but uses characters, which implies that the diagram is not as accurate as it could be but the general principle is clear. The x(11) and Sys.sleep() commands are used to plot during the for-loop.

# Sierpinsky Triangle # Initialise triangle p <- c(0, 500, 1000) q <- c(0, 1000, 0) x11() par(mar = rep(0, 4)) plot(p, q, col= "red", pch = 15, cex = 1, axes = FALSE) # Random starting point x <- sample(0:1000, 1) y <- sample(0:1000, 1) # Chaos game for (i in 1:10000) { Sys.sleep(.001) n <- sample(1:3, 1) x <- floor(x + (p[n] - x) / 2) y <- floor(y + (q[n] - y) / 2) points(x, y, pch = 15, cex = 0.5) }

This algorithm demonstrates how a seemingly chaotic process can result in order. Many other versions of chaos games exist, which I leave to the reader to play with. If you create your own versions then please share the code in the comment box below.

The post The Sierpinski Triangle: Visualising infinity in R appeared first on The Devil is in the Data.

To **leave a comment** for the author, please follow the link and comment on their blog: ** The Devil is in the Data**.

R-bloggers.com offers

(This article was first published on ** R on Rob J Hyndman**, and kindly contributed to R-bloggers)

The nnetar function in the forecast package for R fits a neural network model to a time series with lagged values of the time series as inputs (and possibly some other exogenous inputs). So it is a nonlinear autogressive model, and it is not possible to analytically derive prediction intervals. Therefore we use simulation.

Suppose we fit a NNETAR model to the famous Canadian lynx data:

library(forecast) (fit <- nnetar(lynx, lambda=0.

To **leave a comment** for the author, please follow the link and comment on their blog: ** R on Rob J Hyndman**.

R-bloggers.com offers

(This article was first published on ** Ilya Kashnitsky**, and kindly contributed to R-bloggers)

Following the surprising success of my latest post, I decided to show yet another use case of the handy `ggplot2::annotation_custom()`

. Here I will show how to add small graphical information to maps – just like putting a stamp on an envelope.

The example comes from my current work on a paper, in which I study the effect of urban/rural differences on the relative differences in population ageing (I plan to tell a bit more in one of the next posts). Let’s have a look at the map we are going to reproduce in this post:

So, with this map I want to show the location of more and less urbanized NUTS-2 regions of Europe. But I also want to show – with subplots – how I defined the three subregions of Europe (Eastern, Southern, and Western) and what is the relative frequency of the three categories of regions (Predominantly Rural, Intermediate, and Predominantly Rural) within each of the subregions. The logic of actions is simple: first prepare all the components, then assemble them in a composite plot. Let’s go!

The code to prepare R session and load the data.

```
# additional packages
library(tidyverse)
library(ggthemes)
library(rgdal)
library(viridis)
library(extrafont)
myfont <- "Roboto Condensed"
# load the already prepared data
load(url("https://ikashnitsky.github.io/doc/misc/map-subplots/df-27-261-urb-rur.RData"))
load(url("https://ikashnitsky.github.io/doc/misc/map-subplots/spatial-27-261.RData"))
```

Now, I prepare the spatial objects to be plotted with ggplot2 and create a blank map of Europe – our canvas.

```
# fortify spatial objects
bord <- fortify(Sborders)
fort <- fortify(Sn2, region = "id")
# join spatial and statistical data
fort_map <- left_join(df,fort,"id")
# create a blank map
basemap <- ggplot()+
geom_polygon(data = fortify(Sneighbors),aes(x = long, y = lat, group = group),
fill = "grey90",color = "grey90")+
coord_equal(ylim = c(1350000,5450000), xlim = c(2500000, 6600000))+
theme_map(base_family = myfont)+
theme(panel.border = element_rect(color = "black",size = .5,fill = NA),
legend.position = c(1, 1),
legend.justification = c(1, 1),
legend.background = element_rect(colour = NA, fill = NA),
legend.title = element_text(size = 15),
legend.text = element_text(size = 15))+
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
labs(x = NULL, y = NULL)
```

Okay, now the envelope is ready. It’s time to prepare the stamps. Let’s create a nice mosaic plot showing the distribution of NUTS-2 regions by subregions and the urb/rur categories. I found the simplest way to create a nice mosaic plot on Stack Overflow.

```
# create a nice mosaic plot; solution from SO:
# http://stackoverflow.com/a/19252389/4638884
makeplot_mosaic <- function(data, x, y, ...){
xvar <- deparse(substitute(x))
yvar <- deparse(substitute(y))
mydata <- data[c(xvar, yvar)];
mytable <- table(mydata);
widths <- c(0, cumsum(apply(mytable, 1, sum)));
heights <- apply(mytable, 1, function(x){c(0, cumsum(x/sum(x)))});
alldata <- data.frame();
allnames <- data.frame();
for(i in 1:nrow(mytable)){
for(j in 1:ncol(mytable)){
alldata <- rbind(alldata, c(widths[i],
widths[i+1],
heights[j, i],
heights[j+1, i]));
}
}
colnames(alldata) <- c("xmin", "xmax", "ymin", "ymax")
alldata[[xvar]] <- rep(dimnames(mytable)[[1]],
rep(ncol(mytable), nrow(mytable)));
alldata[[yvar]] <- rep(dimnames(mytable)[[2]], nrow(mytable));
ggplot(alldata, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)) +
geom_rect(color="white", aes_string(fill=yvar)) +
xlab(paste(xvar, "(count)")) +
ylab(paste(yvar, "(proportion)"));
}
typ_mosaic <- makeplot_mosaic(data = df %>% mutate(type = as.numeric(type)),
x = subregion, y = type)+
theme_void()+
scale_fill_viridis(option = "B", discrete = T, end = .8)+
scale_y_continuous(limits = c(0, 1.4))+
annotate("text",x = c(27, 82.5, 186), y = 1.05,
label=c("EAST", "SOUTH", "WEST"),
size = 4, fontface = 2,
vjust = 0.5, hjust = 0,
family = myfont) +
coord_flip()+
theme(legend.position = "none")
```

Just what we needed. The next step is to build a small map showing the three subregions of Europe. But before we proceed to the maps, one thing has to be fixed. `ggplot2`

fails rendering nested polygons. With our regional dataset, London, for example, will not be shown if we do not account for this unpleasant feature. Luckily, there is quite a simple solution to fix that problem.

```
# a nice small function to overcome some mapping problems with nested polygons
# see more at SO
# https://stackoverflow.com/questions/21748852
gghole <- function (fort) {
poly <- fort[fort$id %in% fort[fort$hole, ]$id, ]
hole <- fort[!fort$id %in% fort[fort$hole, ]$id, ]
out <- list(poly, hole)
names(out) <- c("poly", "hole")
return(out)
}
```

Now I build the small map of subregions.

```
# annotate a small map of the subregions of Europe
an_sub <- basemap +
geom_polygon(data = gghole(fort_map)[[1]],
aes(x = long, y = lat, group = group, fill = subregion),
color = NA)+
geom_polygon(data = gghole(fort_map)[[2]],
aes(x = long, y = lat, group = group, fill = subregion),
color = NA)+
scale_fill_manual(values = rev(brbg3)) +
theme(legend.position = "none")
```

Finally, everything is ready to build the main map and stick the two subplots on top of it.

```
# finally the map of Urb/Rur typology
caption <- "Classification: De Beer, J., Van Der Gaag, N., & Van Der Erf, R. (2014). New classification of urban and rural NUTS 2 regions in Europe. NIDI Working Papers, 2014/3. Retrieved from http://www.nidi.nl/shared/content/output/papers/nidi-wp-2014-03.pdf
\nIlya Kashnitsky (ikashnitsky.github.io)"
typ <- basemap +
geom_polygon(data = gghole(fort_map)[[1]],
aes(x=long, y=lat, group=group, fill=type),
color="grey30",size=.1)+
geom_polygon(data = gghole(fort_map)[[2]],
aes(x=long, y=lat, group=group, fill=type),
color="grey30",size=.1)+
scale_fill_viridis("NEUJOBS\ncalssification of\nNUTS-2 regions",
option = "B", discrete = T, end = .8)+
geom_path(data = bord, aes(x = long, y = lat, group = group),
color = "grey20",size = .5) +
annotation_custom(grob = ggplotGrob(typ_mosaic),
xmin = 2500000, xmax = 4000000,
ymin = 4450000, ymax = 5450000)+
annotation_custom(grob = ggplotGrob(an_sub),
xmin = 5400000, xmax = 6600000,
ymin = 2950000, ymax = 4150000)+
labs(title = "Urban / Rural classification of NUTS-2 regions of Europe\n",
caption = paste(strwrap(caption, width = 95), collapse = '\n'))+
theme(plot.title = element_text(size = 20),
plot.caption = element_text(size = 12))
```

Done!

Of course, it takes several iterations to position each element in its proper place. Then, one also needs to play with export parameters to finally get the desired output.

To **leave a comment** for the author, please follow the link and comment on their blog: ** Ilya Kashnitsky**.

R-bloggers.com offers