shiny - 如何根据第一个选择选择数据 - 闪亮的应用程序
问题描述
我是使用 Shiny 的新手,我已经阅读了教程,以及关于堆叠溢出的一些问题,但我认为我仍然缺少一些关键概念。
基本上我希望用户首先选择一个数据集。然后基于该数据集,他们可以选择感兴趣的 OTU。然后我将显示一个图,也许还有一个表格。
我有正确选择数据集的语法,但是如何根据该语法生成要选择的 OTU 选项?
任何帮助表示赞赏。
谢谢
ui <- fluidPage(
# Make a title to display in the app
titlePanel(" Exploring the Effect of Metarhizium on the Soil and Root Microbiome "),
# Make the Sidebar layout
sidebarLayout(
# Put in the sidebar all the input functions
sidebarPanel(
# drop down menu to select the dataset of interest
selectInput('dataset', 'dataset', names(abundance_tables)),
# drop down menu to select the OTU of interest
uiOutput("otu"),
#
br(),
# Add comment
p("For details on OTU identification please refer to the original publications")
),
# Put in the main panel of the layout the output functions
mainPanel(
plotOutput('plot')
# ,dataTableOutput("anova.tab")
)
)
)
server <- function(input, output){
# Return the requested dataset ----
datasetInput <- reactive({
switch(input$dataset)
})
#
dataset <- datasetInput()
# output otus to choose basaed on dataset selection
output$otu <- renderUI({
selectInput(inputId = "otu", label = "otu",
choices = colnames(dataset))
})
output$plot <- renderPlot({
#
dataset <- datasetInput()
otu <- input$otu
#dataset<-abundance_tables[[1]]
## melt and add sample metadata
df_annot<-merge(dataset,sample_metadata,by="row.names",all.x=T)
rownames(df_annot)<-df_annot[,1]
df_annot<-df_annot[,-1]
#
dfM<-melt(df_annot,id.vars = c("Location","Bean","Fungi","Insect"),value.name="abund")
# renaming Fungi level to metarhizium
levels(dfM$Fungi)<-c("Metarhizium","No Meta")
#
ggplot(subset(dfM, variable==otu),
aes(x=Insect,y=abund,fill=Fungi))+geom_boxplot()+facet_wrap(~Location,scales="free_y" )+
guides(fill=guide_legend("Metarhizium")) +
ggtitle(otu)
})
}
##
shinyApp(ui=ui,server=server)
好的,我在一些答案后做了一些修复,但现在出现以下错误。
Listening on http://127.0.0.1:5684
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
41: .getReactiveEnvironment()$currentContext
40: .dependents$register
39: datasetInput
38: server [/Users/alisonwaller/Documents/Professional/Brock/Bidochka_Microbiome/shiny/Barelli_shiny.R#68]
1: runApp
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
解决方案
是的,你真的很亲近。只需将此行替换
selectInput('otu', 'otu', uiOutput("otu")),
为:uiOutput("otu"),
这里不需要 SelectInput() ,因为它在服务器函数的 renderUI 中。
推荐阅读
- bash - awk 中的语法错误
- oauth-2.0 - 将 OAuth2.0 与 VueJS 一起使用?如何开始?
- c - 如何在 Eclipse CDT 应用程序中分配输入文件而不是标准输入?
- java - 在列表视图中单击时如何发出错误消息?
- python - Python NLP 从句子中提取拳头段落
- excel - 根据来自其他列的键从多行中获取不同的值
- php - 从 CLI 运行 CakePHP 3 / Phinx 异常
- python - Keras 自定义损失函数不返回
- ios - 如何获取具有roundedRect样式的UITextField的cornerRadius
- r - 使用 jtools 绘制 lm 模型/多元线性回归模型