R/Shiny : 盒子的颜色取决于选择 [英] R/Shiny : Color of boxes depend on select

查看:29
本文介绍了R/Shiny : 盒子的颜色取决于选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建闪亮的动态盒子.我们可以使用 (status = "warning" or "info" etc.) 更改框的状态(颜色)我想根据选择输入的选择(动态)更改此框的颜色:

I try to create dynamic boxes in shiny. We can change the status (color) of the box with (status = "warning" or "info" etc.) I would like to change the color of this box (dynamically) depend on the choice of select input like that :

https://image.noelshack.com/fichiers/2018/32/2/1533638864-v1.pnghttps://image.noelshack.com/fichiers/2018/32/2/1533638864-v2.png

代码如下:

SelectInput("s010102i", label = NULL, 
 choices = list("Value 1" = 1, "Value 2" = 2, "Value 3" = 3), 
 selected = 1) ,width = 12, background = "blue", status = "info")),                                column(4,

我们需要通过一个变量来改变状态中的信息".但是当我尝试将变量放在 ui 端时,它不起作用,当我像 output$s010102i 这样经过服务器时...状态不等于输出中的信息"而是:

We need to change the "info" in status by a variable. But when I try to put variable in ui side, it does not work and when i pass by the server like output$s010102i... the status is not equals to "info" in the output but :

ERROR: Invalid status: <div id="s010102o" class="shiny-html-output"></div>. Valid statuses are: primary, success, info, warning, danger.

我怎样才能动态放置它?以及如何更改选定的变量?

How can i put this dynamically ? And how to change the selected by a variable ?

非常感谢!

编辑 -> 这里有一个可利用的代码来理解:

Edit -> Here an exploitable code to understand :

 library(shiny)
 library(shinydashboard)
 library(DT)


  # Define UI for application that draws a histogram
  ui <- fluidPage(

  dashboardPage(
    dashboardHeader( title = "Simple Test"),

    dashboardSidebar(),      
    dashboardBody(          
    fluidRow(

           box(title = h3("S.01.01.02", style = "display:inline; font-weight:bold"),

             selectInput("s010102i", label = NULL, 
                         choices = list("Not good" = 1, "O.K" = 2, "Good" = 3), 
                         selected = 1) ,width = 12, background = "blue", status = renderUI("s010102o"))
                                                                              # I want a text or a variable here
                                                                              # Logically "danger", warning or success
           # If Not good is selected, i wan that the status (the color) go to red (danger)
    ))))



  # Define server logic required to draw a histogram
  server <- function(input, output) {

    statut = c("danger","warning","success")


    output$s010102o <- reactive({  
      valueStatut = input$s010102i # Give 1 2 or 3
      s010102o =  statut[valueStatut] # give the value of the statut if "danger" the box changes to red 

    })
  }

  # Run the application 
  shinyApp(ui = ui, server = server)

推荐答案

解决方案是使用 renderUI 在服务器中创建整个盒子,然后使用 uiOutput<在 UI 中渲染它/代码>.你可以这样做:

The solution is to create the whole box in the server using renderUI and just render it in the UI using uiOutput. You can do something like this:

library(shiny)
library(shinydashboard)
library(DT)


# Define UI for application that draws a histogram
ui <- fluidPage(

  dashboardPage(
    dashboardHeader( title = "Simple Test"),

    dashboardSidebar(),      
    dashboardBody(          
      fluidRow(
        uiOutput("s010102o")
      ))))



# Define server logic required to draw a histogram
server <- function(input, output) {

  statut = c("danger","warning","success")

  output$s010102o <- renderUI({
    box(title = h3("S.01.01.02", style = "display:inline; font-weight:bold"),

        selectInput("s010102i", label = NULL, 
                    choices = list("Not good" = "danger", "O.K" = "info", "Good" = "success"), 
                    selected = 1) ,width = 12, background = "blue")})

  observeEvent(input$s010102i,{
    output$s010102o <- renderUI({
      box(title = h3("S.01.01.02", style = "display:inline; font-weight:bold"),

          selectInput("s010102i", label = NULL,
                      choices = list("Not good" = "danger", "O.K" = "info", "Good" = "success"),
                      selected = input$s010102i) ,width = 12, background = "blue",status = input$s010102i)
    })


  })

}

# Run the application 
shinyApp(ui = ui, server = server)

但是在更新输入框时要小心.

But be careful as you are updating the box in which the input is.

这篇关于R/Shiny : 盒子的颜色取决于选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆