我正在尝试为某些反应性列计算加权百分比。我可以使用以下代码在R上做到这一点:
a <- cbind(c(1, 0, 1, 0, 1), c(1, 1, 2, 2, 1), c(100, 200, 300, 50, 500))
colnames(a) <- c("gender", "race", "weights")
a <- as.data.frame(a)
a_stack <- a %>%
na.omit() %>%
select(gender, race, weights) %>%
group_by(gender, race) %>%
summarize(totalw = sum(weights)) %>%
mutate(Percentage = (totalw / sum(totalw)) * 100) %>%
arrange(gender)
这是我的输出:Output。
从上面可以看出,权重是基于性别/种族相加的,我得到了想要的最终结果。
尽管如此,当我尝试将其转换为R Shiny并在反应性上下文中使用它时,我收到以下错误消息 “评估错误:参数的无效'类型'(字符)。”
这是我在R Shiny中使用的代码。
completeFun <- function(data, desiredCols) {
completeVec <- complete.cases(data[, desiredCols])
return(data[completeVec, ])
}
edited_stackbar <- reactive ({
completeFun(edited, c(input$x, input$y, input$weight)) %>%
select_(input$x, input$y, input$weight) %>%
group_by_(input$x, input$y) %>%
summarize(totalw = sum(input$weight)) %>%
mutate(Percentage = (totalw / sum(totalw)) * 100) %>%
arrange_(input$x) %>%
mutate(label_pos = cumsum(Percentage) - Percentage / 2,
perc_text = paste0(round(Percentage), "%"))
})
很难再现它,但我认为主要问题在于事物的“总结”部分。我不确定是否应该使用电抗/电抗值函数,因为权重和变量会根据用户的输入而变化,或者是否应该使用其他数据集。
我将非常感谢您提供的所有帮助!谢谢。
答案 0 :(得分:0)
如果总结是错误,您可以尝试吗?
edited_stackbar <- reactive ({
completeFun(edited, c(input$x, input$y, input$weight)) %>%
select_(input$x, input$y, input$weight) %>%
group_by_(input$x, input$y) %>%
summarize(totalw = sum(edited$nput$weight)) %>%
mutate(Percentage = (totalw / sum(totalw)) * 100) %>%
arrange_(input$x) %>%
mutate(label_pos = cumsum(Percentage) - Percentage / 2,
perc_text = paste0(round(Percentage), "%"))
})
或
edited_stackbar <- reactive ({
completeFun(edited, c(input$x, input$y, input$weight)) %>%
select_(input$x, input$y, input$weight) %>%
group_by_(input$x, input$y) %>%
summarize(totalw = sum(get(input$weight))) %>%
mutate(Percentage = (totalw / sum(totalw)) * 100) %>%
arrange_(input$x) %>%
mutate(label_pos = cumsum(Percentage) - Percentage / 2,
perc_text = paste0(round(Percentage), "%"))
})
无论如何,我们都没有足够的信息来回答您。您可以显示dput(已编辑)吗?