Write html stimulus description from dataframe

html_stimulus(df, html_content, html_element, column_names, css = NULL,
  id = "my_stim")

Arguments

df

data.frame containing stimulus information and css codes in columns

html_content

character column name containing html content to be placed between html element tags

html_element

character the html element tag to use

column_names

vector of column names containing css attribute definitions

css

vector of names containing valid css attribute names

id

character to set the id of the html element, defaults to "my_stim"

Value

vector of html code defining each stimulus

Examples

# define a data frame with stimulus info stroop_stim <- data.frame(stimulus = length(16), word = rep(c("red","green","blue","yellow"), each=4), color = rep(c("red","green","blue","yellow"), 4), response = rep(c("r","g","b","y"), 4), id = "stroop_stim", fontsize = "16pt") head(stroop_stim)
#> stimulus word color response id fontsize #> 1 1 red red r stroop_stim 16pt #> 2 1 red green g stroop_stim 16pt #> 3 1 red blue b stroop_stim 16pt #> 4 1 red yellow y stroop_stim 16pt #> 5 1 green red r stroop_stim 16pt #> 6 1 green green g stroop_stim 16pt
# create html stimulus definition html_stimulus(df = stroop_stim, html_content = "word", html_element = "p", column_names = c("color","fontsize"), css = c("color", "font-size"), id = "id" )
#> [1] "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>red</p>" #> [2] "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>red</p>" #> [3] "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>red</p>" #> [4] "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>red</p>" #> [5] "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>green</p>" #> [6] "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>green</p>" #> [7] "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>green</p>" #> [8] "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>green</p>" #> [9] "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>blue</p>" #> [10] "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>blue</p>" #> [11] "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>blue</p>" #> [12] "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>blue</p>" #> [13] "<p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>yellow</p>" #> [14] "<p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>yellow</p>" #> [15] "<p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>yellow</p>" #> [16] "<p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>yellow</p>"
# use in a pipe to add stimulus definition to stimuli column library(dplyr)
#> #> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’: #> #> filter, lag
#> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union
stroop_stim <- data.frame(stimulus = length(16), word = rep(c("red","green","blue","yellow"), each=4), color = rep(c("red","green","blue","yellow"), 4), response = rep(c("r","g","b","y"), 4), id = "stroop_stim", fontsize = "16pt") %>% mutate(stimulus = html_stimulus(df = ., html_content = "word", html_element = "p", column_names = c("color","fontsize"), css = c("color", "font-size"), id = "id")) head(stroop_stim)
#> stimulus #> 1 <p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>red</p> #> 2 <p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>red</p> #> 3 <p id = 'stroop_stim' style = 'color: blue; font-size: 16pt;'>red</p> #> 4 <p id = 'stroop_stim' style = 'color: yellow; font-size: 16pt;'>red</p> #> 5 <p id = 'stroop_stim' style = 'color: red; font-size: 16pt;'>green</p> #> 6 <p id = 'stroop_stim' style = 'color: green; font-size: 16pt;'>green</p> #> word color response id fontsize #> 1 red red r stroop_stim 16pt #> 2 red green g stroop_stim 16pt #> 3 red blue b stroop_stim 16pt #> 4 red yellow y stroop_stim 16pt #> 5 green red r stroop_stim 16pt #> 6 green green g stroop_stim 16pt