R Shiny:使用 select 框显示具有多个输入的单个 table
RShiny: Display a single table with multiple inputs using select box
我有两组输入:月份(Jan ~ Dec)、季度(Q1 ~ Q4),它们在 R 环境中保存为数据框。 (12 个月 + 4 个季度,总共 16 个数据框)
我创建了两个 select 框,我们可以选择显示月份或季度,如下图所示。
但是当我写两个 output$table
代码时,闪亮的应用程序只对月输出或季度输出有反应。
我已经在单个主面板中成功创建了两个 table。
但是,我不想显示两个 tables(看起来很难看),而是想在主面板中显示一个 table 与两个 select 框一起反应。 (双输入单输出).
所以我试过写 if 语句,但它不起作用。
这是我一直在测试的 server.R 和 ui.R
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")),
selectInput("dataset1", "Choose Quatar:",
choices = c("Q1", "Q2", "Q3", "Q4"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output) {
monthInput <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec
)
})
quatarInput <- reactive({
data2 = switch(input$dataset,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
#Test: Add if statement here -> If bla bla it is Month, else it is Quatar(Because both switch must work for same location where table is displayed)
output$table <- DT::renderDataTable({
if (input$monthInput){
DT::datatable(monthInput())
} else {
if(input$quatarInput){
DT::datatable(quatarInput())}
}
})
})
shinyApp(ui=ui,server=server)
另外一个问题是,是否可以在 select 框中指定级别?
比如高级别的select框有月,或者季度,当我选择月的时候,我可以在低级别的select框中选择一月到十二月。换句话说,当我在高位 select 框中选择四分之一时,我可以在低位 select 框中选择 Q1 ~ Q4。
有人可以帮我解决这个问题吗?
关于如何更改您的 UI 设计,我有一个建议。不是有两个 selectInputs,而是有一个 selectInput 可以根据另一个输入在两种样式(月和季度)之间切换。
# I don't have your data, so here I'm just going to create some. Delete these lines and replace them with your actual data
Jan = overflow::sorandf()
Feb = overflow::sorandf()
Mar = overflow::sorandf()
Apr = overflow::sorandf()
May = overflow::sorandf()
Jun = overflow::sorandf()
Jul = overflow::sorandf()
Aug = overflow::sorandf()
Sep = overflow::sorandf()
Oct = overflow::sorandf()
Nov = overflow::sorandf()
Dec = overflow::sorandf()
Q1 = overflow::sorandf()
Q2 = overflow::sorandf()
Q3 = overflow::sorandf()
Q4 = overflow::sorandf()
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("viewdataradio","View data by:", choices = c("month", "quarter"), inline = TRUE, selected = "month"),
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output,session) {
observe({
if(input$viewdataradio == "month"){
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")
firstchoice = "Jan"
label = "Choose Month:"
}else{
choices = c("Q1","Q2","Q3","Q4")
firstchoice = "Q1"
label = "Choose Quarter:"
}
updateSelectInput(session, "dataset", label = label, choices = choices, selected = firstchoice)
})
data <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
output$table <- DT::renderDataTable({
datatable(data())
})
})
shinyApp(ui=ui,server=server)
这看起来更整洁,我想也能回答您的问题。
我有两组输入:月份(Jan ~ Dec)、季度(Q1 ~ Q4),它们在 R 环境中保存为数据框。 (12 个月 + 4 个季度,总共 16 个数据框)
我创建了两个 select 框,我们可以选择显示月份或季度,如下图所示。
但是当我写两个 output$table
代码时,闪亮的应用程序只对月输出或季度输出有反应。
我已经在单个主面板中成功创建了两个 table。 但是,我不想显示两个 tables(看起来很难看),而是想在主面板中显示一个 table 与两个 select 框一起反应。 (双输入单输出).
所以我试过写 if 语句,但它不起作用。
这是我一直在测试的 server.R 和 ui.R
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")),
selectInput("dataset1", "Choose Quatar:",
choices = c("Q1", "Q2", "Q3", "Q4"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output) {
monthInput <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec
)
})
quatarInput <- reactive({
data2 = switch(input$dataset,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
#Test: Add if statement here -> If bla bla it is Month, else it is Quatar(Because both switch must work for same location where table is displayed)
output$table <- DT::renderDataTable({
if (input$monthInput){
DT::datatable(monthInput())
} else {
if(input$quatarInput){
DT::datatable(quatarInput())}
}
})
})
shinyApp(ui=ui,server=server)
另外一个问题是,是否可以在 select 框中指定级别? 比如高级别的select框有月,或者季度,当我选择月的时候,我可以在低级别的select框中选择一月到十二月。换句话说,当我在高位 select 框中选择四分之一时,我可以在低位 select 框中选择 Q1 ~ Q4。
有人可以帮我解决这个问题吗?
关于如何更改您的 UI 设计,我有一个建议。不是有两个 selectInputs,而是有一个 selectInput 可以根据另一个输入在两种样式(月和季度)之间切换。
# I don't have your data, so here I'm just going to create some. Delete these lines and replace them with your actual data
Jan = overflow::sorandf()
Feb = overflow::sorandf()
Mar = overflow::sorandf()
Apr = overflow::sorandf()
May = overflow::sorandf()
Jun = overflow::sorandf()
Jul = overflow::sorandf()
Aug = overflow::sorandf()
Sep = overflow::sorandf()
Oct = overflow::sorandf()
Nov = overflow::sorandf()
Dec = overflow::sorandf()
Q1 = overflow::sorandf()
Q2 = overflow::sorandf()
Q3 = overflow::sorandf()
Q4 = overflow::sorandf()
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("viewdataradio","View data by:", choices = c("month", "quarter"), inline = TRUE, selected = "month"),
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output,session) {
observe({
if(input$viewdataradio == "month"){
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")
firstchoice = "Jan"
label = "Choose Month:"
}else{
choices = c("Q1","Q2","Q3","Q4")
firstchoice = "Q1"
label = "Choose Quarter:"
}
updateSelectInput(session, "dataset", label = label, choices = choices, selected = firstchoice)
})
data <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
output$table <- DT::renderDataTable({
datatable(data())
})
})
shinyApp(ui=ui,server=server)
这看起来更整洁,我想也能回答您的问题。