I am attempting to make a shiny app for my dissertation. In my dissertation I have a number of variables (16) that I am analyzing across 5 conditions.
In my app I wish to have a side panel with a list of variables and conditions as radiobuttons.
In my main panel I want the following output:
- Plotmeans graph with the mean by condition (I've got this part)
- A summary for the selected variable and the selected condition
- A density plot for the selected variable and the selected condition
- Output from shapiro.test for selected variable and selected condition
- Interpretation of the shapiro.test (i.e., normal/not normal)
I am able to easily get the plot with the means by condition, however I am having problems getting the rest of the output to display. There has to be something wrong with the way I reference my radiobuttons for the condition because after I click "Analyze" I keep getting an error telling me that the variable I selected is not found.
Please take a look at my code as I would appreciate any help:
#Shiny app to display means, summary, and normality interpretation for each
variable and condition in study 3
library(shiny)
#############################################################################
# Define UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Variable Means by Condition (Study 3)", align = "center", style =
"color:black")),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "var", label = "Select a Variable:",
c("Time from Catch to Lowest COM" = "T0_1",
"Time from Lowest COM to Release" = "T1_2",
"Release Time" = "T0_2",
"Knee Extension at Catch" = "T0_Knee_Ext",
"Hip Extension at Catch" = "T0_Hip_Ext",
"Minimum Ball Height" = "Min_Ball_Ht",
"Ball Height at Lowest COM" = "T1_Ball_Ht",
"Knee Extension at Lowest COM" = "T1_Knee_Ext",
"Hip Extension at Lowest COM" = "T1_Hip_Ext",
"Shoulder Flexion at Release" = "T2_Sh_Flex",
"Elbow Extension at Release" = "T2_Elb_Ext",
"Release Height" = "T2_Rel_Ht",
"Jump Height" = "T2_Jump_Ht",
"Wrist Extension at Follow-Through" = "T2_Wr_Ext",
"Accuracy" = "ACCURACY",
"Overall Performance" = "Acc.Spd")),
#Add radio buttons to choose a condition
radioButtons(inputId = "cond", label = "Select a Condition:",
c("Condition 1" = 1,
"Condition 2" = 2,
"Condition 3" = 3,
"Condition 4" = 4,
"Condition 5" = 5)),
#Add action button
actionButton("goButton","Analyze")),
# Show a plot of the mean of the selected variable
mainPanel(
#create a plot for selected variable
plotOutput("mean_plot"),
#Get summary for selected variable and selected condition
verbatimTextOutput("summ"),
#Get density plot for selected variable and selected condition
plotOutput("dens_plot"),
#Calculate shapiro wilk test for selected variable and selected
condition
verbatimTextOutput("shap"),
#Return if the selected variable and selected condition is normal or
not
verbatimTextOutput("norm"))
)
)
####################################################################
# Define server logic required to draw plotmeans
server <- function(input, output) {
#import data
library(readr)
dt <- read_csv("dt.csv")
dt$CONDITIONf <- factor(dt$CONDITION, levels = c(1,2,3,4,5), labels =
c("Normal","None","Wrist","Elb. Ht.","Rim"))
#subset data on various inputs from ui
subsetData <- reactive({
new_data <- dt[,CONDITION == input$cond]
return(new_data)
})
#After clicking goButton....
observeEvent(input$goButton, {
#Create plot
output$mean_plot <- renderPlot({
#using gplots plotmeans
library(gplots)
p <- plotmeans(get(input$var) ~ CONDITIONf, data = dt, connect = FALSE,
n.label = FALSE,
mean.labels = TRUE, digits = 2, xlab = "Condition", ylab =
"Mean", main =
"Variable Means by Condition", pch = " ")})
#Get summary for selected variable and condition
#Create density plot
output$dens_plot <- renderPlot({
hist(subsetData[,get(input$var)])
})
#Run shapiro wilk test
output$shap <- renderPrint({
shapiro.test(subsetData[,get(input$var)])
})
#Print interpretation of shapiro.test (ifelse(p-value from shapiro.test <
0.05, "Not Normal", "Normal")
output$norm <- renderPrint({
ifelse(output$shap < 0.05, return("Not Normal", return("Normal")))
})
})
}
#############################################################################
# Run the application
shinyApp(ui = ui, server = server)
If you need the dataset please contact me and I will send it to you. Thanks in advance!
After running:
dplot(head(dt, 20))
In my output I got:
structure(list(X1 = 1:20, PRIM_KEY = 1:20, NAME = c("Andrew Grajeda",
"Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda",
"Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda",
"Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda",
"Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda",
"Andrew Grajeda", "Andrew Grajeda", "Andrew Grajeda"), SUBJECT = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), BIRTHDAY = structure(c(11860, 11860, 11860, 11860,
11860, 11860, 11860, 11860, 11860, 11860, 11860, 11860, 11860,
11860, 11860, 11860, 11860, 11860, 11860, 11860), class = "Date"),
TODAY_DATE = structure(c(17616, 17616, 17616, 17616, 17616,
17616, 17616, 17616, 17616, 17616, 17616, 17616, 17616, 17616,
17616, 17616, 17616, 17616, 17616, 17616), class = "Date"),
AGE = c(15.7698630136986, 15.7698630136986, 15.7698630136986,
15.7698630136986, 15.7698630136986, 15.7698630136986,
15.7698630136986,
15.7698630136986, 15.7698630136986, 15.7698630136986,
15.7698630136986,
15.7698630136986, 15.7698630136986, 15.7698630136986,
15.7698630136986,
15.7698630136986, 15.7698630136986, 15.7698630136986,
15.7698630136986,
15.7698630136986), YOE = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), DAILY_SHOTS = c(50L,
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L,
50L, 50L, 50L, 50L, 50L, 50L, 50L), CLIP = c("00_1", "00_1",
"00_1", "00_1", "00_1", "00_1", "00_1", "00_1", "00_1", "00_1",
"00_1", "00_1", "00_1", "00_1", "00_1", "00_1", "00_1", "00_1",
"00_1", "00_1"), HEIGHT = c(1.73, 1.73, 1.73, 1.73, 1.73,
1.73, 1.73, 1.73, 1.73, 1.73, 1.73, 1.73, 1.73, 1.73, 1.73,
1.73, 1.73, 1.73, 1.73, 1.73), Group = c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), CONDITION = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), SHOT = c(1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L), ACCURACY = c(4.5, 4.5, 4, 4.5, 4, 4.5,
4.5, 4, 3.5, 4.5, 3, 2, 2, 2, 3, 4.5, 4.5, 2, 3, 3), Make = c(1L,
1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L,
1L, 0L, 0L, 0L), T0 = structure(c(-2209075175, -2209075170,
-2209075164, -2209075158, -2209075153, -2209075149, -2209075143,
-2209075136, -2209075130, -2209075126, -2209075040, -2209075035,
-2209075030, -2209075025, -2209075020, -2209075015, -2209075010,
-2209075006, -2209075001, -2209074998), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), T0_Knee_Ext = c(169.7, 165.7,
169.3, 173, 171.3, 168.7, 164.1, 165.7, 166.8, 165.7, 164,
157.4, 157.4, 157.4, 147.2, 147.2, 150, 149.9, 152, 149),
T0_Hip_Ext = c(172.6, 172.6, 172.6, 176.7, 171.7, 171.7,
161.1, 161.1, 168.9, 171.7, 163.7, 160.9, 160.9, 160.9, 154.5,
156.2, 156.2, 156.2, 156.5, 156.2), Min_Ball_Ht = c(0.93,
0.94, 0.96, 0.92, 0.95, 0.94, 0.94, 0.93, 0.94, 0.93, 0.8,
0.81, 0.8, 0.8, 0.81, 0.81, 0.8, 0.8, 0.81, 0.8), T1 =
structure(c(-2209075175,
-2209075169, -2209075163, -2209075157, -2209075152, -2209075148,
-2209075143, -2209075135, -2209075129, -2209075125, -2209075039,
-2209075034, -2209075029, -2209075025, -2209075020, -2209075015,
-2209075010, -2209075005, -2209075001, -2209074997), class =
c("POSIXct",
"POSIXt"), tzone = "UTC"), T0_1 = c(0.601, 0.534, 0.601,
0.567, 0.601, 0.601, 0.584, 0.6, 0.567, 0.6, 0.422, 0.372,
0.339, 0.355, 0.288, 0.272, 0.339, 0.289, 0.222, 0.289),
T1_Ball_Ht = c(1.04, 1.03, 1.02, 1.03, 1.04, 1.05, 1.04,
1.03, 1.03, 1.04, 0.97, 0.94, 0.95, 0.96, 0.97, 0.96, 0.95,
0.94, 0.95, 0.96), T1_Knee_Ext = c(116.3, 119.6, 122.9, 119.2,
127.4, 126.9, 134.4, 129, 134.5, 134.4, 112.3, 116.4, 122.3,
119.7, 121.6, 121.6, 117.7, 117.7, 117.7, 117.7), T1_Hip_Ext = c(142,
138.4, 138.4, 138.4, 142.9, 147.9, 147.9, 147.9, 147.9, 147.9,
133.5, 133.5, 141.5, 148.2, 145.4, 145.4, 145.4, 145.4, 145.4,
145.4), T2 = structure(c(-2209075174, -2209075169, -2209075163,
-2209075157, -2209075152, -2209075148, -2209075142, -2209075135,
-2209075129, -2209075125, -2209075039, -2209075034, -2209075029,
-2209075025, -2209075020, -2209075014, -2209075010, -2209075005,
-2209075001, -2209074997), class = c("POSIXct", "POSIXt"), tzone =
"UTC"),
T1_2 = c(0.267, 0.3, 0.3, 0.267, 0.266, 0.283, 0.267, 0.267,
0.3, 0.3, 0.267, 0.267, 0.283, 0.217, 0.267, 0.333, 0.284,
0.267, 0.334, 0.267), T0_2 = c(0.868, 0.834, 0.901, 0.834,
0.867, 0.884, 0.851, 0.867, 0.867, 0.9, 0.689, 0.639, 0.622,
0.572, 0.555, 0.605, 0.623, 0.556, 0.556, 0.556), T2_Sh_Flex =
c(137.3,
140.8, 134.2, 138.6, 138, 138.6, 138.6, 134.2, 134.2, 140.8,
138, 138, 136, 136, 136, 137, 137, 136, 136, 136), T2_Elb_Ext =
c(179.8,
179.8, 179, 179, 178.5, 179.4, 179.2, 179, 178.9, 179.8,
174.9, 174.9, 174.9, 174.9, 174.9, 175, 174.8, 174.9, 175,
174.8), T2_Rel_Ht = c(2.17, 2.18, 2.17, 2.18, 2.17, 2.17,
2.18, 2.17, 2.18, 2.17, 2.17, 2.17, 2.18, 2.17, 2.17, 2.18,
2.17, 2.18, 2.18, 2.17), T2_Jump_Ht = c(0.05, 0.06, 0.05,
0.06, 0.05, 0.05, 0.06, 0.05, 0.06, 0.05, 0.05, 0.05, 0.06,
0.05, 0.05, 0.06, 0.05, 0.06, 0.06, 0.05), T2_Wr_Ext = c(109.3,
106.8, 106.8, 106.8, 107.9, 109.1, 106.8, 107.8, 107, 107.5,
120, 113.5, 107.9, 100.5, 100.5, 100.5, 100.5, 100.5, 100.5,
100.5), CONDITIONf = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =
c("Normal",
"None", "Wrist", "Elb. Ht.", "Rim"), class = "factor"), Makef =
c("Make",
"Make", "Miss", "Make", "Miss", "Make", "Make", "Miss", "Make",
"Make", "Miss", "Miss", "Miss", "Miss", "Miss", "Make", "Make",
"Miss", "Miss", "Miss"), ACCURACYf = c("Inside Rim - Make",
"Inside Rim - Make", "Inside Rim - Miss", "Inside Rim - Make",
"Inside Rim - Miss", "Inside Rim - Make", "Inside Rim - Make",
"Inside Rim - Miss", "Top Rim - Make", "Inside Rim - Make",
"Top Rim - Miss", "Outside Rim", "Outside Rim", "Outside Rim",
"Top Rim - Miss", "Inside Rim - Make", "Inside Rim - Make",
"Outside Rim", "Top Rim - Miss", "Top Rim - Miss"), ACCURACYnorm =
c(0.875,
0.875, 0.75, 0.875, 0.75, 0.875, 0.875, 0.75, 0.625, 0.875,
0.5, 0.25, 0.25, 0.25, 0.5, 0.875, 0.875, 0.25, 0.5, 0.5),
T0_2norm = c(0.317038102084831, 0.292595255212078, 0.340762041696621,
0.292595255212078, 0.316319194823868, 0.328540618260244,
0.304816678648454, 0.316319194823868, 0.316319194823868,
0.340043134435658, 0.188353702372394, 0.152408339324227,
0.14018691588785, 0.104241552839684, 0.092020129403307,
0.127965492451474,
0.140905823148814, 0.0927390366642703, 0.0927390366642703,
0.0927390366642703), T0_2norm.inv = c(0.682961897915169,
0.707404744787922, 0.659237958303379, 0.707404744787922,
0.683680805176132, 0.671459381739756, 0.695183321351546,
0.683680805176132, 0.683680805176132, 0.659956865564342,
0.811646297627606, 0.847591660675773, 0.85981308411215,
0.895758447160316,
0.907979870596693, 0.872034507548526, 0.859094176851186,
0.90726096333573, 0.90726096333573, 0.90726096333573), Acc.Spd =
c(1.55796189791517,
1.58240474478792, 1.40923795830338, 1.58240474478792,
1.43368080517613,
1.54645938173976, 1.57018332135155, 1.43368080517613,
1.30868080517613,
1.53495686556434, 1.31164629762761, 1.09759166067577,
1.10981308411215,
1.14575844716032, 1.40797987059669, 1.74703450754853,
1.73409417685119,
1.15726096333573, 1.40726096333573, 1.40726096333573)), .Names =
c("X1",
"PRIM_KEY", "NAME", "SUBJECT", "BIRTHDAY", "TODAY_DATE", "AGE",
"YOE", "DAILY_SHOTS", "CLIP", "HEIGHT", "Group", "CONDITION",
"SHOT", "ACCURACY", "Make", "T0", "T0_Knee_Ext", "T0_Hip_Ext",
"Min_Ball_Ht", "T1", "T0_1", "T1_Ball_Ht", "T1_Knee_Ext", "T1_Hip_Ext",
"T2", "T1_2", "T0_2", "T2_Sh_Flex", "T2_Elb_Ext", "T2_Rel_Ht",
"T2_Jump_Ht", "T2_Wr_Ext", "CONDITIONf", "Makef", "ACCURACYf",
"ACCURACYnorm", "T0_2norm", "T0_2norm.inv", "Acc.Spd"), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))