r - 闪亮的应用程序:如何根据 checkboxGroupInput 输出修改 selectInput 选项
问题描述
我在使用不同的数据集作为闪亮应用程序的输入时遇到问题。我希望用户可以通过checkboxGroupInput
. 根据所选的数据集,其中的选项selectInput
会有所不同。我该如何处理?这是我的应用程序的最小版本(不工作):
library(shiny)
library(Seurat)
#-----------------------------------------------------------
# Select dataset
#-----------------------------------------------------------
x <- readRDS("data/x.rds")
y <- readRDS("data/y.rds")
#-----------------------------------------------------------
#-----------------------------------------------------------
# App code
#-----------------------------------------------------------
ui <- fluidPage(
titlePanel("Shiny app"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("data",
"Select data to use",
choices = c("x", "y"),
selected = 'x',
inline = TRUE,
width = NULL),
selectInput(inputId = "search",
label = "Plot 1 gene",
choices = sort(rownames(input$data)), # input$data is not working
multiple = FALSE,
selectize = TRUE)
),
mainPanel(
plotOutput(outputId = "searchgene"),
)
)
)
server <- function(input, output) {
output$searchgene <- renderPlot({
FeaturePlot(input$data,
features = input$search)
})
}
#-----------------------------------------------------------
# Run the application
#-----------------------------------------------------------
shinyApp(ui = ui, server = server)
如果我想让这段代码工作,我需要预加载两个数据集之一,并在其中choices
写selectInput
“x”或“y”(硬写,没有选项可能)。欢迎任何帮助!
编辑
借助这篇文章R shiny 将反应传递给 selectInput 选择,我可以安排让它运行(但可能不是最佳解决方案)。
一个工作版本:
library(shiny)
library(Seurat)
#-----------------------------------------------------------
# Select dataset
#-----------------------------------------------------------
x <- readRDS("data/x.rds")
y <- readRDS("data/y.rds")
#-----------------------------------------------------------
#-----------------------------------------------------------
# App code
#-----------------------------------------------------------
ui <- fluidPage(
titlePanel("Shiny app"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("data",
"Select data to use",
choices = c("x", "y"),
selected = 'x',
inline = TRUE,
width = NULL),
uiOutput('columns')
),
mainPanel(
plotOutput(outputId = "searchgene")
)
)
)
server <- function(input, output) {
output$searchgene <- renderPlot({
mydata = get(input$data) # repeated
FeaturePlot(mydata,
features = input$search)})
output$columns <- renderUI({
mydata = get(input$data) # repeated
selectInput(inputId = "search",
label = "Plot 1 gene",
choices = sort(rownames(mydata)),
multiple = FALSE,
selectize = TRUE)
})
}
#-----------------------------------------------------------
# Run the application
#-----------------------------------------------------------
shinyApp(ui = ui, server = server)
解决方案
推荐阅读
- rust - 如何使用 Diesel 在 sqlite 中存储任意 JSON 对象
- ansible - 将变量传递给 Ansible httpapi 插件
- powershell - 尝试将值保存到 Access DB,但在尝试 ExecuteNonQuery 时出现错误
- sql - 在创建外键约束时忽略外键值
- python - 如何解决django中clean函数的问题
- c# - 如何检查光标位置是否在windows窗体中的图表控件之外?
- laravel - BACKPACK Laravel 使用 Backpack CRUD 以多对多关系管理数据透视表中的额外列
- android - 在 RemoteViews 中调用 updateAppWidget 时出现 TransactionTooLargeException(仅在 android 7.0 和 6.0 上)
- json - Camel交换标头中的Json对象未正确转换为字符串
- java - 如何在 Selenium Java 中使用 CTRL + v 在输入字段中粘贴文本值?