【发布时间】:2022-01-03 19:09:24
【问题描述】:
我一直在做一些与 R Shiny 相关的工作。我对 R 很陌生,这可能是一个简单的语法错误。
下面的代码是我的 server.r 和 renderText(mx()) 似乎只返回最后一个按时间顺序排列的 if 语句的输出。 (当我交换它们时,它会相应地改变)。 input$numbers 是我将其更改为列表的文本输入,而 input$mean_type 对应于具有以下选项的单选按钮
radioButtons('mean_type', 'Which operation?',
c('Arithmetic Mean' = 'A', 'Geometric Mean' = 'G', 'Variance' = 'V'))
任何帮助将不胜感激,谢谢!
mx <- reactive({
x <- as.numeric(unlist(strsplit(input$numbers,",")))
if (input$mean_type == 'A') {
mean(x)
}
if (input$mean_type == 'V') {
sd(x)
}
if (input$mean_type == 'G') {
geometric.mean(x)
}
})
output$mean <- renderText(mx())
【问题讨论】:
-
如果传递给
reactive()的表达式主体中没有return,则返回最后一个计算表达式的值,即geometric.mean(x),如果最后一个条件计算为TRUE或NULL否则。解决方案可能是在每个if块中添加returns,或者使用if-else路由