您如何将 shiny select 输入强制转换为数字来计算平均分?
How do you coerce shiny select inputs to be numbers to calculate average score?
我有一个闪亮的应用程序,用户可以在其中 select 不同的选项(“”,非常低、低、中、高),我想根据他们的 select 每个部分内的离子以及总体平均值(非常低 = 1、低 = 2 等——空白选项应视为 NA)。我知道如果我在一个正常的、非反应性数据集中这样做,我可以使用 dplyr 的 case_when()
,但我不确定如何为每个输入做这件事。尽管我对任何解决方案都持开放态度,但我还是很欣赏使用整洁函数的解决方案,尤其是 case_when().
实际调查有 30 个问题,所以如果可能的话,我不想单独指出每个输入(它们都以相似的词干开头,如下例所示)。最后,每个平均值应该是一个数字(不是字符串),这样我以后可以做其他计算。
感谢任何帮助!下面是一个可重现的示例来演示:
library(shiny)
library(tidyverse)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
ui <- fluidPage(
title = "Personalized Quiz",
useShinyjs(),
useShinydashboard(),
tabsetPanel(
tabPanel("Quiz",
sidebarLayout(
sidebarPanel(
h2("Section 1"),
selectInput("q1", "Question 1",
choices = c("", "Very Low", "Low",
"Medium", "High")),
selectInput("q2", "Question 2",
choices = c("", "Very Low", "Low",
"Medium", "High")),
h2("Section 2"),
selectInput("q3", "Question 3",
choices = c("", "Very Low", "Low",
"Medium", "High")),
selectInput("q4", "Question 4",
choices = c("", "Very Low", "Low",
"Medium", "High"))
),
mainPanel(textOutput("section1_results"),
textOutput("section2_results"),
textOutput("overall_results"))
))))
server <- function(input, output) {
section_1 <- reactive({
#create section_1 average here
})
section_2 <- reactive({
#create section_2 average here
})
overall <- reactive({
#create overall average here
})
output$section1_results <- renderText(paste0("Your section 1 average is ", section_1, "."))
output$section2_results <- renderText(paste0("Your section 2 average is ", section_2, "."))
output$overall_results <- renderText(paste0("Your overall average is ", overall, "."))
}
# Run the application
shinyApp(ui = ui, server = server)
这是解决您的问题的方法;在 selectInputs 中,您可以将隐藏值分配给代码中显示的选项。还要确保在输出中以正确的方式引用你的反应,这意味着你在末尾包含空括号。
library(shiny)
library(tidyverse)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
ui <- fluidPage(
title = "Personalized Quiz",
useShinyjs(),
useShinydashboard(),
tabsetPanel(
tabPanel("Quiz",
sidebarLayout(
sidebarPanel(
h2("Section 1"),
selectInput("q1", "Question 1",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
selectInput("q2", "Question 2",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
h2("Section 2"),
selectInput("q3", "Question 3",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
selectInput("q4", "Question 4",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
),
mainPanel(textOutput("section1_results"),
textOutput("section2_results"),
textOutput("overall_results"))
))))
server <- function(input, output) {
section_1 <- reactive({
#create section_1 average here
mean(c(as.numeric(input$q1), as.numeric(input$q2)), na.rm = T)
})
section_2 <- reactive({
#create section_2 average here
mean(c(as.numeric(input$q3), as.numeric(input$q4)), na.rm = T)
})
overall <- reactive({
#create overall average here
mean(c(as.numeric(input$q1), as.numeric(input$q2), as.numeric(input$q3), as.numeric(input$q4)), na.rm = T)
})
output$section1_results <- renderText(paste0("Your section 1 average is ", section_1(), "."))
output$section2_results <- renderText(paste0("Your section 2 average is ", section_2(), "."))
output$overall_results <- renderText(paste0("Your overall average is ", overall(), "."))
}
# Run the application
shinyApp(ui = ui, server = server)
我有一个闪亮的应用程序,用户可以在其中 select 不同的选项(“”,非常低、低、中、高),我想根据他们的 select 每个部分内的离子以及总体平均值(非常低 = 1、低 = 2 等——空白选项应视为 NA)。我知道如果我在一个正常的、非反应性数据集中这样做,我可以使用 dplyr 的 case_when()
,但我不确定如何为每个输入做这件事。尽管我对任何解决方案都持开放态度,但我还是很欣赏使用整洁函数的解决方案,尤其是 case_when().
实际调查有 30 个问题,所以如果可能的话,我不想单独指出每个输入(它们都以相似的词干开头,如下例所示)。最后,每个平均值应该是一个数字(不是字符串),这样我以后可以做其他计算。
感谢任何帮助!下面是一个可重现的示例来演示:
library(shiny)
library(tidyverse)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
ui <- fluidPage(
title = "Personalized Quiz",
useShinyjs(),
useShinydashboard(),
tabsetPanel(
tabPanel("Quiz",
sidebarLayout(
sidebarPanel(
h2("Section 1"),
selectInput("q1", "Question 1",
choices = c("", "Very Low", "Low",
"Medium", "High")),
selectInput("q2", "Question 2",
choices = c("", "Very Low", "Low",
"Medium", "High")),
h2("Section 2"),
selectInput("q3", "Question 3",
choices = c("", "Very Low", "Low",
"Medium", "High")),
selectInput("q4", "Question 4",
choices = c("", "Very Low", "Low",
"Medium", "High"))
),
mainPanel(textOutput("section1_results"),
textOutput("section2_results"),
textOutput("overall_results"))
))))
server <- function(input, output) {
section_1 <- reactive({
#create section_1 average here
})
section_2 <- reactive({
#create section_2 average here
})
overall <- reactive({
#create overall average here
})
output$section1_results <- renderText(paste0("Your section 1 average is ", section_1, "."))
output$section2_results <- renderText(paste0("Your section 2 average is ", section_2, "."))
output$overall_results <- renderText(paste0("Your overall average is ", overall, "."))
}
# Run the application
shinyApp(ui = ui, server = server)
这是解决您的问题的方法;在 selectInputs 中,您可以将隐藏值分配给代码中显示的选项。还要确保在输出中以正确的方式引用你的反应,这意味着你在末尾包含空括号。
library(shiny)
library(tidyverse)
library(shinyjs)
library(shinydashboard)
library(shinyWidgets)
ui <- fluidPage(
title = "Personalized Quiz",
useShinyjs(),
useShinydashboard(),
tabsetPanel(
tabPanel("Quiz",
sidebarLayout(
sidebarPanel(
h2("Section 1"),
selectInput("q1", "Question 1",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
selectInput("q2", "Question 2",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
h2("Section 2"),
selectInput("q3", "Question 3",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
selectInput("q4", "Question 4",
choices = c("", "Very Low" = 1, "Low" = 2,
"Medium" = 3, "High" = 4)),
),
mainPanel(textOutput("section1_results"),
textOutput("section2_results"),
textOutput("overall_results"))
))))
server <- function(input, output) {
section_1 <- reactive({
#create section_1 average here
mean(c(as.numeric(input$q1), as.numeric(input$q2)), na.rm = T)
})
section_2 <- reactive({
#create section_2 average here
mean(c(as.numeric(input$q3), as.numeric(input$q4)), na.rm = T)
})
overall <- reactive({
#create overall average here
mean(c(as.numeric(input$q1), as.numeric(input$q2), as.numeric(input$q3), as.numeric(input$q4)), na.rm = T)
})
output$section1_results <- renderText(paste0("Your section 1 average is ", section_1(), "."))
output$section2_results <- renderText(paste0("Your section 2 average is ", section_2(), "."))
output$overall_results <- renderText(paste0("Your overall average is ", overall(), "."))
}
# Run the application
shinyApp(ui = ui, server = server)