Shiny 中的条件过滤器:无法将 <character> 转换为 <list>
Conditional filters in Shiny: Can't convert <character> to <list>
为了理解 Shiny,我正在尝试创建一个可以让用户 select 几个 var_1 类别的应用程序(参见下面的 input)在一个 select 菜单中,然后在第二个 select 菜单中仅显示这些类别中的可用选项。 output 应在第一列显示 var_1 selection,在第二列显示 var_2,并且 var_3 应显示为列(我为此使用了 pivot_wider)以及 var_3 中的元素是否存在于 selection 上(“是”或“否”)。
也许通过查看我的输入和输出表,我能够以更好的方式解释我的 objective:
输入(下面代码中的tf)
var_1
var_2
var_3
red
table1
column1
red
table1
column1
red
table1
column1
blue
table2
column2
blue
table2
column2
blue
table2
column2
green
table3
column3
green
table3
column3
green
table3
column3
输出
var_1
var_2
column1
column2
column3
red
table1
Yes
No
No
red
table1
Yes
No
No
red
table1
Yes
No
No
blue
table2
No
Yes
No
blue
table2
No
Yes
No
blue
table2
No
Yes
No
green
table3
No
No
Yes
green
table3
No
No
Yes
green
table3
No
No
Yes
这是我用来尝试构建应用程序的代码,但我不断收到错误,我将在代码下方显示:
library(shinyWidgets)
tf<-test_2_filtros
shinyApp(
ui = pageWithSidebar(
headerPanel("Conditional Filters"),
sidebarPanel(
selectizeGroupUI(
id = "my-filters",
inline = FALSE,
params = list(
var_one = list(inputId = "var_1", title = "Select variable 1", placeholder = 'select'),
var_two = list(inputId = "var_2", title = "Select variable 2", placeholder = 'select')
)
)
),
mainPanel(
tableOutput("table")
)
),
server = function(input, output, session) {
res_mod <- callModule(
module = selectizeGroupServer,
id = "my-filters",
data = tf,
vars = c("var_1", "var_2")
)
output$table <- renderTable({
res_mod()%>%
dplyr::mutate(n = "Yes")%>%
tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No"))
})
},
options = list(height = 500)
)
这些是我现在收到的错误和警告:
Warning: Values are not uniquely
identified; output will contain list-cols.
- Use
values_fn = list
to suppress this warning.
- Use
values_fn = length
to identify where the duplicates arise
- Use
values_fn = {summary_fun}
to summarise duplicates
Warning: Error in: Can't convert character to list.
感谢您给我的任何建议。
您的问题是由数据中的重复行引起的。您可以通过定义一个唯一的 id 来修复它,其中 row_num
如下所示。
output$table <- renderTable({
req(res_mod())
res_mod() %>%
dplyr::mutate(n = "Yes") %>%
mutate(row_num = 1:n()) %>%
tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No")) %>%
select(-row_num)
})
为了理解 Shiny,我正在尝试创建一个可以让用户 select 几个 var_1 类别的应用程序(参见下面的 input)在一个 select 菜单中,然后在第二个 select 菜单中仅显示这些类别中的可用选项。 output 应在第一列显示 var_1 selection,在第二列显示 var_2,并且 var_3 应显示为列(我为此使用了 pivot_wider)以及 var_3 中的元素是否存在于 selection 上(“是”或“否”)。 也许通过查看我的输入和输出表,我能够以更好的方式解释我的 objective:
输入(下面代码中的tf)
var_1 | var_2 | var_3 |
---|---|---|
red | table1 | column1 |
red | table1 | column1 |
red | table1 | column1 |
blue | table2 | column2 |
blue | table2 | column2 |
blue | table2 | column2 |
green | table3 | column3 |
green | table3 | column3 |
green | table3 | column3 |
输出
var_1 | var_2 | column1 | column2 | column3 |
---|---|---|---|---|
red | table1 | Yes | No | No |
red | table1 | Yes | No | No |
red | table1 | Yes | No | No |
blue | table2 | No | Yes | No |
blue | table2 | No | Yes | No |
blue | table2 | No | Yes | No |
green | table3 | No | No | Yes |
green | table3 | No | No | Yes |
green | table3 | No | No | Yes |
这是我用来尝试构建应用程序的代码,但我不断收到错误,我将在代码下方显示:
library(shinyWidgets)
tf<-test_2_filtros
shinyApp(
ui = pageWithSidebar(
headerPanel("Conditional Filters"),
sidebarPanel(
selectizeGroupUI(
id = "my-filters",
inline = FALSE,
params = list(
var_one = list(inputId = "var_1", title = "Select variable 1", placeholder = 'select'),
var_two = list(inputId = "var_2", title = "Select variable 2", placeholder = 'select')
)
)
),
mainPanel(
tableOutput("table")
)
),
server = function(input, output, session) {
res_mod <- callModule(
module = selectizeGroupServer,
id = "my-filters",
data = tf,
vars = c("var_1", "var_2")
)
output$table <- renderTable({
res_mod()%>%
dplyr::mutate(n = "Yes")%>%
tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No"))
})
},
options = list(height = 500)
)
这些是我现在收到的错误和警告:
Warning: Values are not uniquely identified; output will contain list-cols.
- Use
values_fn = list
to suppress this warning.- Use
values_fn = length
to identify where the duplicates arise- Use
values_fn = {summary_fun}
to summarise duplicates
Warning: Error in: Can't convert character to list.
感谢您给我的任何建议。
您的问题是由数据中的重复行引起的。您可以通过定义一个唯一的 id 来修复它,其中 row_num
如下所示。
output$table <- renderTable({
req(res_mod())
res_mod() %>%
dplyr::mutate(n = "Yes") %>%
mutate(row_num = 1:n()) %>%
tidyr::pivot_wider(names_from = var_3, values_from = n, values_fill = list(n = "No")) %>%
select(-row_num)
})