【问题标题】:Validating the data with colors in RShiny在 R Shiny 中使用颜色验证数据
【发布时间】:2018-04-25 20:06:32
【问题描述】:

我正在开发一个闪亮的应用程序。我需要用 2 种颜色验证 textOutput 中显示的值。例如,如果 textOutput 中显示的值为 100,则应突出显示粉红色。这个对我有用。但如果显示的值小于 100 或大于 100,则应突出显示红色。它对我不起作用。

除此之外,我还需要将输入和显示的文本居中对齐。

require(shiny)
require(shinyjs)
#install.packages("shinyjs")

ui = fluidPage( useShinyjs(),
                inlineCSS(list(.lightpink   = "background-color: lightpink"), (.red   = "background-color: red")),

                fluidRow(
                  column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
                  )
                ),
                fluidRow(uiOutput("inputGroup")),
                fluidRow(column(3,wellPanel(textOutput("text3"))))
)

# takes in two arguments
sumN <- function(a, x){
  a <- sum(a, as.numeric(x),na.rm=T)
  return(a)
}

server <- function(input, output, session) {

  Widgets <- eventReactive(input$View,{ input_list <- lapply(1:(input$count),
                                                             function(i) {
                                                               inputName <- paste("id", i, sep = "")
                                                               textInputRow <- function (inputId,value) {
                                                                 textAreaInput(inputName,"", width = "200px", height = "43px", resize = "horizontal" )
                                                                 #numericInput(inputName,"",1,0,100)
                                                               }
                                                               column(4,textInputRow(inputName, "")) })
  do.call(tagList, input_list)},ignoreInit = T)

  output$inputGroup = renderUI({Widgets()})



  getvalues <- reactive({
    val <- 0
    for(lim in 1:input$count){
      observeEvent(input[[paste0("id",lim)]], { 
        updateTextAreaInput(session,paste0("id",lim), value = ({
          x =  as.numeric(input[[paste0("id",lim)]])
          if(!(is.numeric(x))){0}
          else if(!(is.null(x) || is.na(x))){
            if(x < 0){
              0 
            }else if(x > 100){
              100
            } else{
              return (isolate(input[[paste0("id",lim)]]))
            } 
          } 
          #else{0}
          else if((is.null(x) || is.na(x))){
            0
          } 
        })
        )
      })
      req(as.numeric(input[[paste0("id",lim)]]) >= 0 & as.numeric(input[[paste0("id",lim)]]) <= 100)
      val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
    }
    val
  })

  output$text3 <- renderText({
    #getvalues()
    # if(getvalues() > 100){
    #    0



    # }
    #else(getvalues())

    getvalues()

  })

  observeEvent(getvalues(), {
    nn <- getvalues()
    if(is.numeric(as.numeric(nn)) & !is.na(as.numeric(nn)) & nn == 100) {

      addClass("text3", 'lightpink')

    } else  { addClass('text3','red')}
  })

}

shinyApp(ui=ui, server = server)

这是使用的代码..

输出的屏幕截图。如果值超过 100,它应该变成红色。

谁能帮我处理这段代码?

【问题讨论】:

  • 但是如果值超过100,这里会突出显示粉色。我怎样才能变成红色
  • 这个粉红色是从哪里来的?你能分享一个截图吗,因为我只变红了!
  • 只是为了验证你想要红色(如果它是 100)和粉红色(如果它小于或大于 100)?
  • 如果值为 100,则应显示粉红色。如果值大于或小于 100,则应显示红色

标签: r shiny


【解决方案1】:

添加removeClass 修复pink 并应用red

    require(shiny)
require(shinyjs)
#install.packages("shinyjs")

ui = fluidPage( useShinyjs(),
                inlineCSS(list('.lightpink' = "background-color: lightpink",'.red'   = "background-color: red", "textarea" = 'text-align: center', '#text3 ' = 'text-align: center')),

                fluidRow(
                  column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
                  )
                ),
                fluidRow(uiOutput("inputGroup")),
                fluidRow(column(3,wellPanel(textOutput("text3"))))
)

# takes in two arguments
sumN <- function(a, x){
  a <- sum(a, as.numeric(x),na.rm=T)
  return(a)
}

server <- function(input, output, session) {

  Widgets <- eventReactive(input$View,{ input_list <- lapply(1:(input$count),
                                                             function(i) {
                                                               inputName <- paste("id", i, sep = "")
                                                               textInputRow <- function (inputId,value) {
                                                                 textAreaInput(inputName,"", width = "200px", height = "43px", resize = "horizontal" )
                                                                 #numericInput(inputName,"",1,0,100)
                                                               }
                                                               column(4,textInputRow(inputName, "")) })
  do.call(tagList, input_list)},ignoreInit = T)

  output$inputGroup = renderUI({Widgets()})



  getvalues <- reactive({
    val <- 0
    for(lim in 1:input$count){
      observeEvent(input[[paste0("id",lim)]], { 
        updateTextAreaInput(session,paste0("id",lim), value = ({
          x =  as.numeric(input[[paste0("id",lim)]])
          if(!(is.numeric(x))){0}
          else if(!(is.null(x) || is.na(x))){
            if(x < 0){
              0 
            }else if(x > 100){
              100
            } else{
              return (isolate(input[[paste0("id",lim)]]))
            } 
          } 
          #else{0}
          else if((is.null(x) || is.na(x))){
            0
          } 
        })
        )
      })
      req(as.numeric(input[[paste0("id",lim)]]) >= 0 & as.numeric(input[[paste0("id",lim)]]) <= 100)
      val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
    }
    val
  })

  output$text3 <- renderText({
    #getvalues()
    # if(getvalues() > 100){
    #    0



    # }
    #else(getvalues())

    getvalues()

  })

  observeEvent(getvalues(), {
    nn <- getvalues()
    if(is.numeric(as.numeric(nn)) & !is.na(as.numeric(nn)) & nn == 100) {

      removeClass("text3", 'red')
      addClass('text3','lightpink')

    } else  { addClass('text3','red')}
  })

}

shinyApp(ui=ui, server = server)

【讨论】:

  • 是的,它有效。您能说一下如何将 textAreaInput 中输入的文本居中对齐吗?
  • textOutput 可以吗?
  • @NevedhaAyyanar 你可以一起问同样的问题!
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 2021-05-10
  • 2018-11-11
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2017-09-16
  • 2017-06-13
相关资源
最近更新 更多