如何格式化R Shiny numericInput? [英] How to format R Shiny numericInput?

查看:159
本文介绍了如何格式化R Shiny numericInput?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多numericInput字段的Shiny应用程序.我想用逗号分隔每10 ^ 3格式化numericInput字段的方法.例如,我要5,000,000而不是5000000.

I have a Shiny app with numerous numericInput fields. I would like a way to format the numericInput fields with commas separating every 10^3. For example, I want 5,000,000 instead of 5000000.

我可以使用formatprettyNum函数在R中执行此操作.但是我没有办法在Shiny中做到这一点.

I can do this in R with the format and prettyNum functions. But I don't have a way to do this in Shiny.

这对UI很有帮助,因为它可以与百分比,金钱等一起使用.有人知道如何将其合并到numericInput字段中吗?

This would be very helpful for the UI because it would work with percents, money, etc. Does anyone have any idea how to incorporate this into the numericInput field?

谢谢!

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  mainPanel(
    numericInput("formatNumber",
                 "Number should be formatted, e.g."5,000,000",
                 value = 1000),
    p(format(5000000.10, big.mark=",", big.interval=3L,
             digits=0, scientific=F))
  )
)

server <- function(input, output) {  
}

shinyApp(ui = ui, server = server)

推荐答案

我找不到对numericInput()有帮助的任何东西,但是下面是与textInput()一起工作的内容.

I could not find anything that would help with numericInput(), but here's what works with textInput() instead.

library(shiny)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      mainPanel(
        textInput("formatNumber1", "Number should be formatted, e.g.5,000,000", value = 1000),
        textInput("formatNumber2", "Number should be formatted, e.g.5,000,000", value = 1000)
      )
    ),

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

      observe({
        updateTextInput(session, "formatNumber1", "Number should be formatted, e.g.5,000,000", 
                           value = prettyNum(input$formatNumber1, big.mark=",", scientific=FALSE))
        updateTextInput(session, "formatNumber2", "Number should be formatted, e.g.5,000,000",
                           value = prettyNum(input$formatNumber2, big.mark=",", scientific=FALSE))
      })
    }
  )
}

这篇关于如何格式化R Shiny numericInput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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