我正在为我的学校开发一个学生成绩的应用程序。
我希望在为每个学生的成绩添加特定数字之后看到失败vs传递的摘要。
以下示例
library(shiny)
biology_num <- c(49,64,74,84)
biology_pass_fail <- ifelse(biology_num < 50, "Fail", "Pass")
First <- data.frame(cbind(biology_num,biology_pass_fail))
First$biology_num <- as.numeric(as.character(First$biology_num))
UI
ui <- fluidPage(
selectInput("Subject",
"Choose Subject",
choices=list("biology")),
numericInput("Addition","If we added","1"),
tableOutput("table"),
tableOutput("table2")
)
服务器 以下是问题
server <- shinyServer(function(input,output){
#Here I want to create a new dataframe that combines
#the existing grades + the potential add
New_biology <- reactive({
as.data.frame(cbind(New_biology_num+input$Addition))
New_biology$New_biology_pass_fail <-
as.factor(ifelse(New_biology$New_biology_num <50,"Fail","Pass"))
})
output$table <- renderTable(as.table(summary(First$biology_pass_fail)))
output$table2 <- renderTable(as.table(summary({New_biology$New_biology_pass_fail()})))
}
)
shinyApp(ui,server)
然后我收到错误
类型&#39;关闭的对象&#39;不是子集表格
我将New_biology$New_biology_pass_fail
传递给output$table2
或reactive
公式的方式并不正确。
非常感谢您的指导!
答案 0 :(得分:0)
这似乎按照你喜欢的方式工作:
library(shiny)
biology_num <- c(49, 64, 74, 84)
biology_pass_fail <- ifelse(biology_num < 50, "Fail", "Pass")
# DON'T call data.frame(cbind()). Just use data.frame()!
First <- data.frame(biology_num, biology_pass_fail)
ui <- fluidPage(
selectInput("Subject",
"Choose Subject",
choices=list("biology")),
numericInput("Addition","If we added","1"),
tableOutput("table"),
tableOutput("table2")
)
server <- function(input,output) {
New_biology <- reactive({
New_biology_num <- biology_num + input$Addition
data.frame(New_biology_num,
New_biology_pass_fail = as.factor(ifelse(New_biology_num < 50, "Fail", "Pass")))
})
output$table <- renderTable(First)
output$table2 <- renderTable(New_biology())
}
shinyApp(ui, server)
知道反应元素是功能是很重要的。因此,如果您创建一个反应元素(在本例中为New_biology
),则需要将其作为函数调用以返回一个值(即New_biology()
)。如果反应元素返回data.frame,并且您想要访问特定列,则需要在函数调用后应用访问器(例如New_biology()$New_biology_pass_fail
)。