使用%在Rshiny中动态创建的textBox中附加数据

时间:2021-12-25 21:15:37

I am developing a shiny app. If the value is entered in textBox, the value should be automatically updated with % symbol. The same is applicable to textOutput also.(i.e,) if the value is displayed in textOutput it should be displayed with % symbol (say 100%).

我正在开发一个闪亮的应用程序如果在textBox中输入值,则应使用%符号自动更新该值。这同样适用于textOutput。(即)如果值显示在textOutput中,则应使用%符号(例如100%)显示。

The Rcode used is as follows:

使用的Rcode如下:

require(shiny)

ui = fluidPage(
  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())
  })
}

shinyApp(ui=ui, server = server)

Is it possible in R? Can anyone help me with this code?

R有可能吗?任何人都可以帮我这个代码吗?

1 个解决方案

#1


0  

There's probably a more elegant way of doing this, but I just tried to modify your code as written so it would work. First I add this helper function, then modified the following functions:

这可能是一种更优雅的方式,但我只是尝试修改你的代码,因此它可以工作。首先我添加这个辅助函数,然后修改以下函数:

library(stringr)
strip_percent <- function(string) {
x <- as.numeric(str_extract(string, "[0-9]+"))
return(x)
}

...

getvalues <- reactive({
val <- 0
for(lim in 1:input$count){
  observeEvent(input[[paste0("id",lim)]], { 
    updateTextAreaInput(session,paste0("id",lim), value = ({
      x =  strip_percent(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{
                 paste0(x,"%")
               } 
             } 
             #else{0}
             else if((is.null(x) || is.na(x))){
               '0%'
             }
    })
    )
  })
  req(
    strip_percent(input[[paste0("id", lim)]]) >= 0 
    & strip_percent(input[[paste0("id", lim)]]) <= 100)
  val <- sumN(val,
              strip_percent(input[[paste0("id", lim)]])
  )
}
val
})

output$text3 <- renderText({
#getvalues()
if(getvalues() > 100){
  paste0(0, "%")
}
else(paste0(getvalues(), "%"))
})

#1


0  

There's probably a more elegant way of doing this, but I just tried to modify your code as written so it would work. First I add this helper function, then modified the following functions:

这可能是一种更优雅的方式,但我只是尝试修改你的代码,因此它可以工作。首先我添加这个辅助函数,然后修改以下函数:

library(stringr)
strip_percent <- function(string) {
x <- as.numeric(str_extract(string, "[0-9]+"))
return(x)
}

...

getvalues <- reactive({
val <- 0
for(lim in 1:input$count){
  observeEvent(input[[paste0("id",lim)]], { 
    updateTextAreaInput(session,paste0("id",lim), value = ({
      x =  strip_percent(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{
                 paste0(x,"%")
               } 
             } 
             #else{0}
             else if((is.null(x) || is.na(x))){
               '0%'
             }
    })
    )
  })
  req(
    strip_percent(input[[paste0("id", lim)]]) >= 0 
    & strip_percent(input[[paste0("id", lim)]]) <= 100)
  val <- sumN(val,
              strip_percent(input[[paste0("id", lim)]])
  )
}
val
})

output$text3 <- renderText({
#getvalues()
if(getvalues() > 100){
  paste0(0, "%")
}
else(paste0(getvalues(), "%"))
})