Word clouds in Shiny Dean A ali Shiny Consultant Building Web - - PowerPoint PPT Presentation

word clouds in shiny
SMART_READER_LITE
LIVE PREVIEW

Word clouds in Shiny Dean A ali Shiny Consultant Building Web - - PowerPoint PPT Presentation

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES Word clouds in Shiny Dean A ali Shiny Consultant Building Web Applications in R with Shiny: Case Studies Word clouds Visual representation of text BIG WORDS = COMMON, small


slide-1
SLIDE 1

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Word clouds in Shiny

Dean Aali

Shiny Consultant

slide-2
SLIDE 2

Building Web Applications in R with Shiny: Case Studies

Word clouds

  • Visual representation of text
  • BIG WORDS = COMMON, small words = rare
slide-3
SLIDE 3

Building Web Applications in R with Shiny: Case Studies

Word clouds in R - function

Created by your friend:

create_wordcloud(data, num_words = 100, background = "white")

  • data: text to use in word cloud
  • Single string:


data = “Some very long story”

  • List of strings:


data = c(“Some very”, “long story”)

  • num_words: maximum number of words
  • background: background colour
slide-4
SLIDE 4

Building Web Applications in R with Shiny: Case Studies

Word clouds in R - usage

us_constitution <- “We the People of the United States, ...” create_wordcloud(data = us_constitution, num_words = 15, background = "yellow")

slide-5
SLIDE 5

Building Web Applications in R with Shiny: Case Studies

Word clouds: from R to Shiny

  • create_wordcloud() requires R knowledge to

use

  • Create Shiny app ⇒ anyone can create


word cloud

slide-6
SLIDE 6

Building Web Applications in R with Shiny: Case Studies

Word clouds in Shiny

  • Word clouds are new type of output
  • wordcloud2Output() +

renderWordcloud2()

slide-7
SLIDE 7

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Let’s practice!

slide-8
SLIDE 8

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Adding word sources

Dean Aali

Shiny Consultant

slide-9
SLIDE 9

Building Web Applications in R with Shiny: Case Studies

Textarea inputs

  • data argument is text, use textInput()?
  • textAreaInput() similar, but provides multiple

rows

textAreaInput(inputId, label, value, rows, ...)

slide-10
SLIDE 10

Building Web Applications in R with Shiny: Case Studies

File inputs - UI

  • File inputs for uploading a (text) file to Shiny app

fileInput(inputId, label, ...)

slide-11
SLIDE 11

Building Web Applications in R with Shiny: Case Studies

File inputs - UI

?fileInput() for more options

slide-12
SLIDE 12

Building Web Applications in R with Shiny: Case Studies

File inputs - server

  • Aer selecting a file, it gets uploaded and available to

Shiny

  • Text inputs: input$<inputId> is text
  • Numeric inputs: input$<inputId> is number
  • File inputs: input$<inputId> is NOT a file
slide-13
SLIDE 13

Building Web Applications in R with Shiny: Case Studies

File inputs - server

  • File inputs: input$<inputId> is dataframe with 1

row per file

  • Variables: name, size, type, datapath

name size type datapath 1 myfile.txt 6000 text/plain C:/path/to/temporary/file/0.txt

  • datapath is most important: path of file
  • Read selected file:

  • Text file:


input$<inputId>$datapath readLines(input$<inputId>$datapath)

slide-14
SLIDE 14

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Let’s practice!

slide-15
SLIDE 15

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Combining all the word sources

Dean Aali

Shiny Consultant

slide-16
SLIDE 16

Building Web Applications in R with Shiny: Case Studies

Combining all the word sources

  • 3 data sources for word cloud
  • Text object: artofwar
  • User text: textAreaInput()
  • Text file: fileInput()
  • Next step: allow all together
  • Radio buons to select word

source

slide-17
SLIDE 17

Building Web Applications in R with Shiny: Case Studies

Radio buons - review

radioButtons(
 "time_of_day", "Choose your favourite time of day", choices = c("Morning", "Afternoon", "Evening"), selected = "Afternoon"
 )

slide-18
SLIDE 18

Building Web Applications in R with Shiny: Case Studies

Radio buons - advanced

radioButtons( "time_of_day", "Choose your favourite time of day", choices = c("I'm a morning person!" = "Morning", "Love my afternoons" = "Afternoon", "Night owl here!" = "Evening"), selected = "Afternoon" ) > str(input$time_of_day) chr "Afternoon"

slide-19
SLIDE 19

Building Web Applications in R with Shiny: Case Studies

Conditional panels

  • Show/hide UI elements based on input value
  • condition is similar to R code, but


input$<id> is replaced by input.<id>

  • ... is any UI

conditionalPanel(condition, ...)

slide-20
SLIDE 20

Building Web Applications in R with Shiny: Case Studies

Conditional panels

ui <- fluidPage( radioButtons("time_of_day",
 "Choose your favourite time of day",
 ...), plotOutput("morning_only_plot") ) ui <- fluidPage( radioButtons("time_of_day",
 "Choose your favourite time of day",
 ...), conditionalPanel( condition = "input.time_of_day == 'Morning'", plotOutput("morning_only_plot") ) )

slide-21
SLIDE 21

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Let’s practice!

slide-22
SLIDE 22

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Fine tune the reactivity

Dean Aali

Shiny Consultant

slide-23
SLIDE 23

Building Web Applications in R with Shiny: Case Studies

Reactivity review

  • reactive() and input$ are reactive
  • Code depending on reactive variables re-runs when

dependencies update

  • Accessing reactive value makes it dependency

x <- reactive({ y() * input$num1 * input$num2 })

slide-24
SLIDE 24

Building Web Applications in R with Shiny: Case Studies

Isolate

  • Use isolate() to not create reactive dependency
  • If reactive value inside isolate() is modified,

nothing happens

x <- reactive({ y() * isolate({ input$num1 }) * input$num2 }) x <- reactive({ y() * isolate({ input$num1 * input$num2 }) })

slide-25
SLIDE 25

Building Web Applications in R with Shiny: Case Studies

Isolate everything

  • Sometimes you want to isolate all reactives

x <- reactive({ isolate({ y() * input$num1 * input$num2 }) })

  • Need a way to trigger x to re-run on demand
slide-26
SLIDE 26

Building Web Applications in R with Shiny: Case Studies

Action buons

# After clicking on a button twice > str(input$button) int 2

  • Only one simple interaction: click

actionButton(inputId, label, ...)

  • Value of buon is number of times it was clicked
slide-27
SLIDE 27

Building Web Applications in R with Shiny: Case Studies

Action buons as reactivity triggers

  • Accessing buon input value in server triggers

reactivity

  • Add buon to UI

actionButton(inputId = "calculate_x", label = "Calculate x!")

  • Access buon to make it dependency

x <- reactive({ input$calculate_x isolate({ y() * input$num1 * input$num2 }) })

slide-28
SLIDE 28

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Let’s practice!

slide-29
SLIDE 29

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Wrap-up: Go and make your own apps!

Dean Aali

Shiny Consultant

slide-30
SLIDE 30

Building Web Applications in R with Shiny: Case Studies

slide-31
SLIDE 31

Building Web Applications in R with Shiny: Case Studies

Chapter 2: Ploing app

Shiny is great for customizing plots with many parameters

slide-32
SLIDE 32

Building Web Applications in R with Shiny: Case Studies

Chapter 3: Data exploration app

Shiny is great as a data exploration tool. Think about ease of use and user experience, not only functionality.

slide-33
SLIDE 33

Building Web Applications in R with Shiny: Case Studies

Chapter 4: Cloud word app

Shiny is great for exposing R code as graphical interface, or for sharing your R code with non R users.

slide-34
SLIDE 34

BUILDING WEB APPLICATIONS IN R WITH SHINY: CASE STUDIES

Your turn!