我有一个带有 UI 的 Shiny 网页,它的条件是在侧边栏面板的顶部选择“Alfa/Beta”,然后在每种情况下从字段列表中进行选择。
ui.R
如果我对文件中的 selectInputs 的值列表进行硬编码,我设法达到了一个可以工作的点ui.R
,但是现在我想要一个工作版本,它将 selectInputsui.R
与来自获取的内容相结合来自 db 中的数据server.R
。
我的问题是我不知道如何将可能值列表从 db 选择传递server.R
到 selectInputs in ui.R
,然后将选择传递回server.R
以生成绘图。我在教程中阅读的示例没有这种高级别的条件面板,这让我感到困惑。
我勾勒出我想象的如何工作,但留下了一些我不知道该怎么做的地方(见下文)。
任何想法如何来回传递这些选择?
用户界面
## Old hard-coded values
# alfaFieldA = c("One","Ttwo")
# ...
shinyUI(pageWithSidebar(
headerPanel("Page Dev"),
sidebarPanel(
selectInput("option","Alfa/Beta",c("Alfa","Beta")),
conditionalPanel(condition="input.option=='Alfa'",
selectInput("SOMEHOW.alfaFieldA","Alfa FieldA",choices=alfaFieldA,selected=alfaFieldA,multiple=T)
),
conditionalPanel(condition="input.option=='Beta'",
selectInput("SOMEHOW.betaFieldA" ,"Beta FieldA" ,choices=betaFieldA,selected=betaFieldA,multiple=T)
),
wellPanel(
p(strong("Selection from lists from either Alfa or Beta:")),
conditionalPanel(condition="input.option=='Alfa'",
selectInput("SOMEHOW.alfaFieldB","Alfa FieldB",choices=alfaFieldB,selected=alfaFieldB,multiple=T)
),
conditionalPanel(condition="input.option=='Alfa'",
selectInput("SOMEHOW.alfaFieldC","Alfa FieldC",choices=alfaFieldC,selected=alfaFieldC,multiple=T)
),
conditionalPanel(condition="input.option=='Beta'",
selectInput("SOMEHOW.betaFieldB" ,"Beta FieldB" ,choices=betaFieldB,selected=betaFieldB,multiple=T)
),
conditionalPanel(condition="input.option=='Beta'",
selectInput("SOMEHOW.betaFieldC" ,"Beta FieldC" ,choices=betaFieldC,selected=betaFieldC,multiple=T)
)
)
),
# display the tabset depending on choosing Alfa or Beta above.
mainPanel(
conditionalPanel(condition="input.option=='Alfa'",
tabsetPanel(tabPanel("A1"),tabPanel("A2"))),
conditionalPanel(condition="input.option=='Beta'",
tabsetPanel(tabPanel("B1"),tabPanel("B2"),tabPanel("B3")))
)
))
服务器.R
library(RMySQL)
shinyServer(function(input, output, clientData) {
# Gather data
dbcon = dbConnect(MySQL(),user="userRO",password="xxxxx",host="my-host-01",port=5000,dbname="mydb")
fa = dbSendQuery(dbcon, "select a, b, c from mytableA")
fa_data = fetch(fa, n=-1)
fa_data = fa_data[nchar(fa_data$a)!=0,]
# make 'mytableA.a' list visible to conditionalPanel selectInput alfaFieldA
fb = dbSendQuery(dbcon, "select a, b, c from mytableB")
fb_data = fetch(fb, n=-1)
fb_data = fb_data[nchar(fb_data$a)!=0,]
dbret = dbDisconnect(dbcon)
# make 'mytableB.a' list visible to conditionalPanel selectInput betaFieldA SOMEHOW
output$A1 <- renderGvis({
# manipulate fa_data for selected FieldA and FieldB values SOMEHOW and plot something
})
output$B1 <- renderGvis({
# manipulate fb_data for selected FieldA, FieldB and FieldC values SOMEHOW and plot something
})
# ...
})