2017-11-13 10 views
2

私は輝くアプリを開発しています。私は2色でtextOutputに表示される値を検証する必要があります。たとえば、100のtextOutputに値が表示されている場合は、ピンクの色を強調表示する必要があります。わたしにはできる。値が100より小さいか100より大きい値が表示されている場合は、赤色を強調表示する必要があります。それは私にとってはうまくいかない。RShinyの色でデータを検証する

それに加えて、入力して表示するテキストを中央に揃える必要があります。

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を超えると、赤色に変わります。

enter image description here 誰でもこのコードを教えてもらえますか? pinkを修正し、redを適用する

+0

[RshinyにTextOutputにおけるデータの検証]の可能な重複(https://stackoverflow.com/questions/47017520/テキスト中のデータ出力の検証) – amrrs

+0

値が100を超えると、ここでピンクの色が強調表示されます。どのように私は赤に変えることができます –

+0

そしてこのピンクはどこから来ましたか?私は赤だけを取得しているので、あなたはスクリーンショットを共有できますか? – amrrs

答えて

1

追加さremoveClass

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) 

enter image description here

+0

うん、それは動作します。 textAreaInputに入力されたテキストを中央に揃える方法を教えてください。 –

+0

コードを更新してください。 – amrrs

+0

textOutputは可能ですか? –