[英]In R Shiny, how to sum specified columns of a dataframe and output the results into a table?
Here's an easy one to help make up for some of my other posts.这里有一个简单的方法可以帮助弥补我的其他一些帖子。 I think it's easy but then again I didn't find solutions that work for me in my searches.
我认为这很容易,但我在搜索中没有找到适合我的解决方案。
When running the below MWE code, how do I generate a simple table output that shows the sums of the 2 noted columns ('Nbr' and 'MOB')?运行以下 MWE 代码时,如何生成一个简单的表格输出,显示 2 个标注列(“Nbr”和“MOB”)的总和? The below gives me "Error in sum: invalid 'type' (character) of argument".
下面给了我“总和错误:参数的‘类型’(字符)无效”。
I'd like a table output (not a value embedded in a text string) because in the full App I'll be adding other sums to the output, building up a larger table.我想要一个表格输出(不是嵌入在文本字符串中的值),因为在完整的应用程序中,我将向输出添加其他总和,构建一个更大的表格。
MWE code: MWE代码:
library(shiny)
beta <- matrix(c(19,19,19,'2014-07','2014-08','2014-09',1,2,3),
nrow = 3,
ncol = 3,
dimnames = list(NULL,c("Nbr","Month","MOB")))
beta <- data.frame(beta)
ui <- fluidPage(
tableOutput("summary")
)
server <- function(input, output, session) {
output$summary <- renderTable(c(sum(beta$Nbr,sum(beta$MOB))))
}
shinyApp(ui, server)
The error ( Error in sum(beta$MOB) : invalid 'type' (character) of argument
) is telling you that you can't perform the function sum
on a character vector.错误 (
Error in sum(beta$MOB) : invalid 'type' (character) of argument
) 告诉您无法对字符向量执行sum
函数。 So you first have to convert to numeric, and then the error goes away.所以你首先必须转换为数字,然后错误就会消失。
beta <- matrix(c(19,19,19,'2014-07','2014-08','2014-09',1,2,3),
nrow = 3,
ncol = 3,
dimnames = list(NULL,c("Nbr","Month","MOB")))
beta <- data.frame(beta)
Add this here:在此处添加:
beta[c("Nbr","MOB")]<-sapply(beta[c("Nbr","MOB")],as.numeric)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.