简体   繁体   English

用于多个valueBox的SelectInput

[英]SelectInput for multiple valueBoxes

I am looking for a way to have three separate valueBoxes respond to the same selectInput. 我正在寻找一种方法,使三个单独的valueBox响应相同的selectInput。 My dataframe: 我的数据框:

region        Diarrhea       Fever     ARI
Afghanistan   78.2          56.4       29.7
Boys          34.1          23.2       15.6
Girls         18.4          12.8       11.2

For selectInput I want Diarrhe, Fever and ARI as options, and I would like to see three Value boxes, one for Afghanistan, one for Boys and one for Girls with the value corresponding to input variable. 对于selectInput,我需要腹泻,发烧和ARI作为选项,我想看到三个值框,一个用于阿富汗,一个用于男孩,一个用于女孩,其值与输入变量相对应。 I cant seem to figure out how to this.. 我似乎不知道该怎么办。

Thanks! 谢谢!

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Data", status = "primary", solidHeader = T, width = 12,
        fluidPage(
          fluidRow(
            column(2, offset = 0, style = 'padding:1px;', 
                   selectInput(inputId = "selected_data",
                               label = "Indicator",
                               choices = overall[,c(2:4)]))
          )
        )
    ),
    uiOutput("value_box")
  )
)

server <- function(input, output) {
  output$value_box <- renderUI({
    valueBox(input$selected_data,subtitle = "Afghanistan")

  })
}


shinyApp(ui = ui, server = server)

You could make separate uiOutputs , but a more concise approach would be to use lapply inside the renderUI to loop over your resulting dataframe . 您可以创建单独的uiOutputs ,但更简洁的方法是在lapply中使用lapply renderUI结果dataframe Note that I renamed your input to selected_column and I modified the options in the input. 请注意,我将您的输入重命名为selected_column并修改了输入中的选项。

A working example is given below, hope this helps! 下面给出一个工作示例,希望这会有所帮助!


在此处输入图片说明


overall = read.table(text = 'region        Diarrhea       Fever     ARI
Afghanistan   78.2          56.4       29.7
Boys          34.1          23.2       15.6
Girls         18.4          12.8       11.2', header=T)

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Data", status = "primary", solidHeader = T, width = 12,
        fluidPage(
          fluidRow(
            column(2, offset = 0, style = 'padding:1px;', 
                   selectInput(inputId = "selected_column",
                               label = "Indicator",
                               choices = setdiff(colnames(overall),'region')))
          )
        )
    ),
    uiOutput("value_box")
  )
)

server <- function(input, output) {
  output$value_box <- renderUI({
    box(width=12,
    lapply(1:nrow(overall), function(i) {
      valueBox(overall[i,input$selected_column],overall[i,'region'])})
    )
  })
}

shinyApp(ui = ui, server = server)

EDIT: As requested in your comment, this would be an example on how to make this work with separate UI elements: 编辑:根据您的评论中的要求,这将是一个如何使用单独的UI元素进行工作的示例:

overall = read.table(text = 'region        Diarrhea       Fever     ARI
Afghanistan   78.2          56.4       29.7
                     Boys          34.1          23.2       15.6
                     Girls         18.4          12.8       11.2', header=T)

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    box(title = "Data", status = "primary", solidHeader = T, width = 12,
        fluidPage(
          fluidRow(
            column(2, offset = 0, style = 'padding:1px;', 
                   selectInput(inputId = "selected_column",
                               label = "Indicator",
                               choices = setdiff(colnames(overall),'region')))
          )
        )
    ),
    box(width=12,
        uiOutput("value_box1"),
        uiOutput("value_box2")
    )
  )
)

server <- function(input, output) {

  output$value_box1 <- renderUI({
    valueBox(overall[1,input$selected_column],overall[1,'region'])
  })

  output$value_box2 <- renderUI({
    valueBox(overall[2,input$selected_column],overall[2,'region'])
  })

}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM