To create a shiny widget the data is assumed to be of class likertData
, which basically consists of three data frames.
library('knitr') library(shinyLikert) # load the package from installed library #require( 'devtools' ) #devtools::load_all( "../.." ) # load the package from source testData2 = createTestData()
testData3 = createTestData() plot( renderShinyLikert( testData3 ) )
The structure of the data is as follows
summary( testData2 )
likert_data
consists of answers of 100 participants to 10 questions.createTestData
gives "country" and "gender" as factors.question_factors
consists of factors, that let you classify the questions into groups. In this case, each question has been marked "easy" or "hard". Therefore, we get a factorial variable with 10 observations. To get a better idea of the factors, use the renderFactorOverview
functionfluidPage( renderFactorOverview( testData2 ) )
The dropdown menu lets you choose a factor and the plot element displays a bar chart of the factor levels. Since the testData2
is generated with a uniform distribution, the factor levels are equally distributed.
We used the dataset testData2
here. This data consists of three data frames.
names( testData2 )
The first data frame likert_data
consists of the likert items. The row names are the participant IDs and the column names are the questions of the of the survey.
kable( head( testData2$likert_data[,1:6] ) )
The second data frame row_factors
can be used to classify the participants.
kable( head( testData2$row_factors ) )
The third data frame column_factors
can be used to classify the questions.
kable( head( testData2$column_factors ) )
To display the likert_data
, use the renderShinyLikert
function
plot( renderShinyLikert( testData2 ) )
This function can be used in the following way
renderShinyLikert(
dataset , dropdown_factor )
The dataset is assumed to be of class likertData
. The output is a list contining the plot and the control element. They can be used like here
rendered2 = renderShinyLikert( testData2, dropdown = "country" ) rendered2$selector rendered2$plot
wich produces the following output.
rendered2 = renderShinyLikert( testData2 , dropdown = "country" ) rendered2$selector rendered2$plot dataTableOutput( "ID 1.factorTable" )
The factors to be used in dropdown
can be either row factors or column factors called by name. The dataset is filtered according to those factors prior to plotting. If you want to filter by question, just add another column to testData2$column_factors
testData2$column_factors = cbind( testData2$column_factors, question = paste( "Question", 1:10 ) )
and now change the dropdown
argument fom country
to c("difficulty", "question")
rendered2.1 = renderShinyLikert( testData2, dropdown = c("difficulty", "question") ) rendered2.1$selector rendered2.1$plot
To use this function in a ui/server setup, you can use the following syntax
shinyApp( ui= fluidPage( uiOutput("selector"), uiOutput( "plot" ) ), server = function( input, output, session ){ testData3 = createTestData() rendered = renderShinyLikert( testData3 , dropdown = "gender" ) output$plot = rendered$plot output$selector = rendered$selector } )
output$selector
is the inputpanel which lets you select the gender in this case. To add more inputs, from them together in a list.
rendered3 = renderShinyLikert( testData2, dropdown = c( "country", "difficulty" ) ) rendered3$selector rendered3$plot
You can also use column_factors
exactly as above.
plot( renderShinyLikert( testData2, dropdown = c( "difficulty" ) ) )
To control the height of the plot, use the option height = c( height_min, height_max )
plot( renderShinyLikert( testData2, dropdown = c( "gender", "country" ), height = c( 300, 600 ) ) )
To get a custom order for the responses, use the responselevels parameter
plot( renderShinyLikert( testData2, dropdown = "gender", response_levels = c( "Strongly Agree", "Agree", "Neutral" ) ) )
HH::likert
You can also pass down arguments down to HH::likert
plot( renderShinyLikert( testData2, dropdown = "country", main = "custom_title", horizontal = FALSE, as.percent = TRUE, positive.order = TRUE ) )
You can also use the names of column_factors
for the dropdown argument.
plot( renderShinyLikert( testData2, dropdown = "difficulty" ) )
For factror analysis, you can also use the split_factors
argument.
plot( renderShinyLikert( testData2, split = c( "difficulty", "gender", "country" ) ) )
plot( renderShinyLikert( testData2 , dropdown = "country", split = "difficulty" ) )
plot( renderShinyLikert( testData2, height = c(500,800), dropdown = c("gender","difficulty"), group = "country" ) )
plot( renderShinyLikert( testData2, height = c(500,800), dropdown = c("country","difficulty"), group = "gender", grouping = "HH" ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.