Employing input and output to deal with changes in Shiny app parameters

If you now look closer at the server object, you can see that the function behind it shows an input and an output argument. These two arguments are the solution Shiny found to support the kinds of double-sided exchanges of information we were mentioning before:

  • input is a list that stores all choices coming from the ui object, for instance, a selected column among a list of columns or a range of minimum and maximum values to be employed to filter a vector
  • output is a list of objects representing the result of computation performed from the server function

This is the real building block of all of the Shiny world. Let's develop a small example before applying Shiny to our report. Imagine we want to build a small app that is able to create and plot a distribution of n numbers, with defined by the user of the app. We need:

  • A control that lets the user decide n
  • A piece of interface to show the resulting plot
  • A piece of server to create the distribution and build the plot

In the Shiny, this translates into the following:

ui <- fluidPage(
numericInput("num", label = h3("Numeric input"), value = 100),
plotOutput("plot")
)

server <- function(input, output, session) {
output$plot <- renderPlot({
rnorm(input$num) %>% hist() })
}
shinyApp(ui, server)

If we now source this code (assuming we have already installed and loaded the library shiny), it will produce the following output:

Now that you know a bit of R markdown and the Shiny app framework, we can smoothly complete our report by adding one paragraph for each of the following analyses:

  • Sentiment analysis
  • Wordcloud
  • ngram analysis
  • Shareholders network analysis

Given the great integration between markdown and the R language, writing this chapter will simply mean taking the code from the previously performed in the R script and placing it within different code chunks. You will then be easily able to add notes and comments to your analyses while writing text outside your chunks and respecting the markdown conventions. We can therefore obtain the report with the following code:

## results reproduced below are results from mentioned analyses.
### sentiment analysis

```{r}
comments %>%
unnest_tokens(word,text)-> comments_tidy
#sentiment analysis

lexicon <- get_sentiments("bing")

comments_tidy %>%
inner_join(lexicon) %>%
count(company, sentiment) %>%
spread(sentiment, n, fill = 0) %>%
mutate(sentiment = positive -negative)-> comments_sentiment

ggplot(comments_sentiment, aes(x = sentiment)) +
geom_histogram()
```

### wordcloud

```{r}

comments_tidy %>%
filter(!word %in% stop_words$word) %>%
count(word) %>%
with(wordcloud(word, n))
```
### ngram analysis
#### bigrams
```{r}
comments %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) -> bigram_comments

bigram_comments %>%
separate(bigram, c("word1", "word2"), sep = " ") %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word) %>%
count(word1, word2, sort = TRUE) %>%
head() %>%
kable()
```
#### trigrams
```{r}

comments %>%
unnest_tokens(trigram, text, token = "ngrams", n = 3) -> trigram_comments

trigram_comments %>%
separate(trigram, c("word1", "word2","word3"), sep = " ") %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word) %>%
filter(!word3 %in% stop_words$word) %>%
count(word1, word2, word3, sort = TRUE) %>%
kable()
```
### network analysis

```{r}
information %>%
filter(grepl("share holders", text)) %>%
mutate(shareholders = gsub("share holders: ","",text)) %>%
separate(col = shareholders, into = c("first","second","third"),sep = ";") %>%
gather(key = "number",value ="shareholder",-company,-text) %>%
filter(!is.na(shareholder)) %>%
select(company,shareholder)-> shareholders
graph_from_data_frame(shareholders)-> graph_object
#linking the size of a node to its degree
deg <- degree(graph_object, mode="all")
V(graph_object)$size <- deg*3

set.seed(30)
graph_object %>%
ggraph() +
geom_edge_link(alpha = .2) +
geom_node_point(aes(size = size),alpha = .3) +
geom_node_text(aes(label = name,size = size), vjust = 1, hjust = 1, check_overlap = FALSE)+
theme_graph()
```

Let me show you now how to create the final interactive data lineage paragraph part of your report, leveraging the shiny framework we have seen before.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset