将数据帧转换为selectInput(闪亮)中的选择列表 [英] Transforming data frame to a selection list in selectInput (Shiny)

查看:146
本文介绍了将数据帧转换为selectInput(闪亮)中的选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框对应于下面的示例:

I've a data frame corresponding to the sample below:

df = data.frame(subject=c("Subject A", "Subject B", "Subject C", "Subject D"),id=c(1:4))

我想把这个数据框转换成可以在 selectInput 中方便地实现的列表对象:

I would like to transform this data frame to a list object that could be conveniently implemented in selectInput:

selectInput("subject", "Subject",
            choices = #my_new_list )

我希望最终用户能够看到选择中的主题列表和 selectInput 返回相应的数值( id )。

I would like for the end-user to see the list of subjects in the selection and for the selectInput to return the corresponding numerical value (id).

如果我尝试通过:

df <- data.frame(lapply(df, as.character),
                         stringsAsFactors = FALSE)
df <- as.list(df)

selectInput 下拉菜单显示所有可用选项:

The selectInput drop down menu shows all available options:

我只想列出主题并传递

推荐答案

使用功能 split

my_new_list <- split(df$id, df$subject)
my_new_list
#$`Subject A`
#[1] 1

#$`Subject B`
#[1] 2

#$`Subject C`
#[1] 3

#$`Subject D`
#[1] 4

与一起使用的功能

Together with function with:

my_new_list <- with(df, split(id, subject))

这篇关于将数据帧转换为selectInput(闪亮)中的选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆