R - 使用 format() 函数删除空格
R - removing white spaces using the format() function
在我的 Shiny 应用程序中,我正在尝试打印一个值,该值实际上是一笔钱。
此时代码如下:
text <- reactive({format(data()), big.mark= ",", trim =
"TRUE")})
output$profit <- renderText({
paste("The total profit is \u00a3",text(),".")
但是,从text()
返回的值前后仍然有空格。我该如何摆脱它们?
对粘贴功能进行一些试验,并注意文档中的 sep
参数:
paste("The total profit is \u00a3","5,000",".")
[1] "The total profit is £ 5,000 ."
这表明问题与 text()
的结果无关。相反:
paste("The total profit is \u00a3","5,000",".",sep = "")
[1] "The total profit is £5,000."
您可能还对方便设置货币样式的包 lucr 感兴趣。
您可以使用 paste0
删除字符串之间的空格。
或者如@joran所说,在paste
中添加sep=""
选项。
此外,我们可以使用glue::glue
glue("The total profit is \u00a3{text()}")
-完整代码
library(shiny)
library(glue)
options(scipen = 999)
df1 <- data.frame(amount = c(5000, 10000, 200000))
ui <- fluidPage(
selectInput("amt", "amount", choices = df1$amount),
verbatimTextOutput(outputId = "profit")
)
server <- function(input, output) {
data <- reactive(as.numeric(input$amt))
text <- reactive({
format(data(), big.mark= ",", trim = TRUE)})
output$profit <- renderText({
glue("The total profit is \u00a3{text()}")
})
}
shinyApp(ui = ui, server = server)
-输出
在我的 Shiny 应用程序中,我正在尝试打印一个值,该值实际上是一笔钱。
此时代码如下:
text <- reactive({format(data()), big.mark= ",", trim =
"TRUE")})
output$profit <- renderText({
paste("The total profit is \u00a3",text(),".")
但是,从text()
返回的值前后仍然有空格。我该如何摆脱它们?
对粘贴功能进行一些试验,并注意文档中的 sep
参数:
paste("The total profit is \u00a3","5,000",".")
[1] "The total profit is £ 5,000 ."
这表明问题与 text()
的结果无关。相反:
paste("The total profit is \u00a3","5,000",".",sep = "")
[1] "The total profit is £5,000."
您可能还对方便设置货币样式的包 lucr 感兴趣。
您可以使用 paste0
删除字符串之间的空格。
或者如@joran所说,在paste
中添加sep=""
选项。
此外,我们可以使用glue::glue
glue("The total profit is \u00a3{text()}")
-完整代码
library(shiny)
library(glue)
options(scipen = 999)
df1 <- data.frame(amount = c(5000, 10000, 200000))
ui <- fluidPage(
selectInput("amt", "amount", choices = df1$amount),
verbatimTextOutput(outputId = "profit")
)
server <- function(input, output) {
data <- reactive(as.numeric(input$amt))
text <- reactive({
format(data(), big.mark= ",", trim = TRUE)})
output$profit <- renderText({
glue("The total profit is \u00a3{text()}")
})
}
shinyApp(ui = ui, server = server)
-输出