Hello,
Beginner R programmer here, still working on a Jeopardy! project where I publish a quiz. I have one published here...
I am working on an update where I print out all of the correct answers after a user submits their own answers, but ran into a problem. I keep getting this error message...
"Warning: Error in : object of type 'closure' is not subsettable
100: tagWrite
99: doRenderTags
98: processDeps
97: transform
96: func
94: f
93: Reduce
84: do
83: hybrid_chain
82: origRenderFunc
81: output$number_correct
1: runApp"
I have tried debugging myself and using the internet as a resource, but couldn't find the answer using this...
I don't know what the issue here is. I have the exact same code as I have in my app that is published and works except for what I have at the end where I am attempting to print out the correct answers. Code is below...
ui <- fluidPage(
#creates a set of radio buttons, only one of which can be pressed at any time
#id: rand_or_spec-what i refer to it as and its stored as computationally, label is radio buttons with h3-hierarchical difference f|| formatting
#choices: a list of values, widget will build a button f|| each value of the list. text/value LIKE key/value. text shows up, value is whats stored as computationally
#selected: value that should be initially selected
radioButtons("rand_or_spec", label = h3("Radio buttons"),
choices = list("Random Category" = 1, "Specific Category" = 2),
selected = NULL),
#placeholder from when server going to plug in text box
uiOutput("Specific_Category"),
#creates action button f|| submitting users choice, either random || what they enter
actionButton("submit_category", label = "SUBMIT"),
#area that prints what the category is after they have been selected
uiOutput("category_selected"),
#placeholder f|| when server puts in questions into text boxes f|| all 5 questions (cont'd)
#and text box f|| user to answer questions
uiOutput("quiz"),
#printing # correct
uiOutput("number_correct")
)
#creates server function, which is where the output is generated, some of which are puts of the ui.
#server is listener.input is clicked, output is generated
#render is action button to take stuff from server and render it to ui
#can put server in seperate file in ui
server <- function(input, output){
#imports jeop3column dataframe from csv
jeop3column <- read.csv(file = "data/jeop3column2.csv")
#observes aka if ANY of the inputs referenced that are mentioned in observe function changes, code in the observe runs again: if specific_category is chosen, render Specific_Category
#creates new textinput button with ID of user_category, without a label, that says "input category here"
#|| else, BREAK -> go to next section
observe({
if(input$rand_or_spec == 2){
output$Specific_Category <- renderUI({
#taglist makes output get one html element, computationally known as "user_category", no label, text
tagList(textInput("user_category", label = NULL, value = "input category here"))
})
}
else{
output$Specific_Category <- renderUI({
br()
})
}
})
#next observe the event that if/WHEN the submit category button is clicked, then following code will be run
#create a variable called single category that is a random selection of
#a category from my df.
#sample is a function f|| a random selection, 1 = sample size, [[1]] == get a string and not list
observeEvent(input$submit_category, {
if(input$rand_or_spec == 1){
single_cat<- sample(unique(as.character(jeop3column$category)), 1)
#if not random, single_cat = user input f|| user category
} else{
#if what user entered in cateogry, uses that, DO MORE FUZZY WUZZY
if (input$user_category %in% jeop3column$category)
single_cat <- input$user_category
else
single_cat<- sample(unique(as.character(jeop3column$category)), 1)[[1]]
}
#single_cat_df == subset of jeop3column where category == selected single category
#single_cat with 5 questions == sample of scdf with 5 selections of rows, all the columns
#function f|| validating category
#if number of rows in dataframe where category is category >=5, category is ok
#|| else new category = a new random sample from df, then repeat >=5
validate_cat <- function (cat){
if (nrow(jeop3column[jeop3column$category == cat,]) >= 5){
return(cat)
}
else{
new_cat <- sample(unique(as.character(jeop3column$category)), 1)
validate_cat(new_cat)
}
}
#single category being selected
single_cat <- validate_cat(single_cat)
single_cat_df <- jeop3column[jeop3column$category == single_cat, ]
single_cat_df_5 <- single_cat_df[sample(nrow(single_cat_df), 5), ]
#observe when category is created and create displayed category button
output$category_selected <- renderUI({
paste("Category Selected: ", single_cat)
})
#output of quiz
#what does tag list do
#creates text input, label is question, then populates 1st q from df, enter text f|| answer
#paste joins two strings in r: question 1 AND question from DF
output$quiz <- renderUI({
tagList(
textInput("answer1", label = h3(paste("QUESTION 1: ", single_cat_df_5$answer[1])), value = "Enter text..."),
textInput("answer2", label = h3(paste("QUESTION 2: ", single_cat_df_5$answer[2])), value = "Enter text..."),
textInput("answer3", label = h3(paste("QUESTION 3: ", single_cat_df_5$answer[3])), value = "Enter text..."),
textInput("answer4", label = h3(paste("QUESTION 4: ", single_cat_df_5$answer[4])), value = "Enter text..."),
textInput("answer5", label = h3(paste("QUESTION 5: ", single_cat_df_5$answer[5])), value = "Enter text..."),
actionButton("submit_answers", label = "Submit Answers")
)
})
})
#agrep("Nixon", "Nixon")
#s1a = "Nixon"
#s1b = "naxon"
observeEvent(input$submit_answers, {
num_right <- 0
output$number_correct <- renderUI({
paste("Percentage correct", "YOU GOT A: ", num_right)
output$correct_answers <- renderUI({
taglist(
textInput("answer1", label = h3(paste("ANSWER 1: ", single_cat_df_5$question[1])), value = "Enter text..."),
textInput("answer2", label = h3(paste("ANSWER 2: ", single_cat_df_5$question[2])), value = "Enter text..."),
textInput("answer3", label = h3(paste("AnSWER 3: ", single_cat_df_5$question[3])), value = "Enter text..."),
textInput("answer4", label = h3(paste("ANSWER 4: ", single_cat_df_5$question[4])), value = "Enter text..."),
textInput("answer5", label = h3(paste("ANSWER 5: ", single_cat_df_5$question[5])), value = "Enter text..."),
)
})
})
}
)}
shinyApp(ui = ui, server = server)
The section I likely need to troubleshoot is below...
output$correct_answers <- renderUI({
textInput("answer1", label = h3(paste("ANSWER 1: ", single_cat_df_5$question[1])), value = "Enter text..."),
textInput("answer2", label = h3(paste("ANSWER 2: ", single_cat_df_5$question[2])), value = "Enter text..."),
textInput("answer3", label = h3(paste("AnSWER 3: ", single_cat_df_5$question[3])), value = "Enter text..."),
textInput("answer4", label = h3(paste("ANSWER 4: ", single_cat_df_5$question[4])), value = "Enter text..."),
textInput("answer5", label = h3(paste("ANSWER 5: ", single_cat_df_5$question[5])), value = "Enter text..."),
The formatting works for above in my code when it populates the questions, but when I Want to show the correct answers it gives me the error message of "Warning: Error in : object of type 'closure' is not subsettable".
Any help or guidance would be appreciated as I'm somewhat stuck here.
Thanks!
-Taylor