R闪亮。没有适用于 'mutate' 的方法应用于 class "c('reactiveExpr', 'reactive', 'function')" 的对象
R Shiny. No applicable method for 'mutate' applied to an object of class "c('reactiveExpr', 'reactive', 'function')"
我正在尝试创建一个非常简单的 Shiny 应用程序,它可以让用户 select 一组表并查看这些表共享哪些变量。
我的输入数据(在下面的代码中名为“df”)如下所示:
Tables
Variables
tabla_1
A
tabla_1
Z
tabla_1
Y
tabla_1
V
tabla_1
B
tabla_2
H
tabla_2
B
tabla_2
A
tabla_2
U
tabla_3
U
tabla_3
S
tabla_3
M
tabla_4
U
tabla_4
A
tabla_4
B
tabla_4
V
tabla_4
Q
tabla_4
O
tabla_4
F
如果用户 select 编辑了所有可用的表来比较它们,则输出必须如下所示(我下面代码中的 mytable1):
Variables
tabla_1
tabla_2
tabla_3
tabla_4
A
Yes
Yes
No
Yes
Z
Yes
No
No
No
Y
Yes
No
No
No
V
Yes
No
No
Yes
B
No
Yes
No
Yes
H
No
Yes
No
No
U
No
Yes
Yes
Yes
S
No
Yes
Yes
No
M
No
No
Yes
No
Q
No
No
No
Yes
O
No
No
No
Yes
F
No
No
No
Yes
现在我的代码如下所示:
library(shiny)
library(dplyr)
ui <- fluidPage(
title = "Compare Data Tables",
sidebarPanel(
checkboxGroupInput("show_tables", "Tables to compare",
unique(df$Tables), selected = unique(df$Tables)
),
mainPanel("comparison", DT::dataTableOutput("mytable1"))))
server <- function(input, output) {
filter1 <- reactive({subset(df, Tables == input$show_tables)})
result<-filter1 %>%
mutate(n = "Yes")%>%
pivot_wider(names_from = Tables, values_from = n, values_fill = list(n = "No"))
output$mytable1 <- DT::renderDataTable({
DT::datatable(result)
})
}
shinyApp(ui, server)
在我 运行 收到此消息后:
Error in UseMethod("mutate") : no applicable method for 'mutate'
applied to an object of class "c('reactiveExpr', 'reactive',
'function')"
我不确定哪里出了问题。是不是我不能将 mutate 函数用于反应对象。如果是这样,我可以使用什么来获得相同的输出?
非常感谢您的任何建议。
我们可以将 server
部分更改为
server <- function(input, output) {
result <- reactive({
req(input$show_tables)
tmp <- subset(df, Tables %in% input$show_tables)
tmp %>%
dplyr::mutate(n = "Yes")%>%
tidyr::pivot_wider(names_from = Tables, values_from = n, values_fill = list(n = "No"))
})
output$mytable1 <- DT::renderDataTable({
DT::datatable(result())
})
}
shinyApp(ui, server)
-输出
我正在尝试创建一个非常简单的 Shiny 应用程序,它可以让用户 select 一组表并查看这些表共享哪些变量。
我的输入数据(在下面的代码中名为“df”)如下所示:
Tables | Variables |
---|---|
tabla_1 | A |
tabla_1 | Z |
tabla_1 | Y |
tabla_1 | V |
tabla_1 | B |
tabla_2 | H |
tabla_2 | B |
tabla_2 | A |
tabla_2 | U |
tabla_3 | U |
tabla_3 | S |
tabla_3 | M |
tabla_4 | U |
tabla_4 | A |
tabla_4 | B |
tabla_4 | V |
tabla_4 | Q |
tabla_4 | O |
tabla_4 | F |
如果用户 select 编辑了所有可用的表来比较它们,则输出必须如下所示(我下面代码中的 mytable1):
Variables | tabla_1 | tabla_2 | tabla_3 | tabla_4 |
---|---|---|---|---|
A | Yes | Yes | No | Yes |
Z | Yes | No | No | No |
Y | Yes | No | No | No |
V | Yes | No | No | Yes |
B | No | Yes | No | Yes |
H | No | Yes | No | No |
U | No | Yes | Yes | Yes |
S | No | Yes | Yes | No |
M | No | No | Yes | No |
Q | No | No | No | Yes |
O | No | No | No | Yes |
F | No | No | No | Yes |
现在我的代码如下所示:
library(shiny)
library(dplyr)
ui <- fluidPage(
title = "Compare Data Tables",
sidebarPanel(
checkboxGroupInput("show_tables", "Tables to compare",
unique(df$Tables), selected = unique(df$Tables)
),
mainPanel("comparison", DT::dataTableOutput("mytable1"))))
server <- function(input, output) {
filter1 <- reactive({subset(df, Tables == input$show_tables)})
result<-filter1 %>%
mutate(n = "Yes")%>%
pivot_wider(names_from = Tables, values_from = n, values_fill = list(n = "No"))
output$mytable1 <- DT::renderDataTable({
DT::datatable(result)
})
}
shinyApp(ui, server)
在我 运行 收到此消息后:
Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "c('reactiveExpr', 'reactive', 'function')"
我不确定哪里出了问题。是不是我不能将 mutate 函数用于反应对象。如果是这样,我可以使用什么来获得相同的输出?
非常感谢您的任何建议。
我们可以将 server
部分更改为
server <- function(input, output) {
result <- reactive({
req(input$show_tables)
tmp <- subset(df, Tables %in% input$show_tables)
tmp %>%
dplyr::mutate(n = "Yes")%>%
tidyr::pivot_wider(names_from = Tables, values_from = n, values_fill = list(n = "No"))
})
output$mytable1 <- DT::renderDataTable({
DT::datatable(result())
})
}
shinyApp(ui, server)
-输出