闪亮:将带有 space 的文本传递给图表不起作用,但它可以在没有 space 的情况下工作

Shiny: Passing on text with a space to chart does not work, but it works without a space

我的 Shiny 脚本从下拉列表中获取输入。基于此,我(在服务器端)设置了一个应该显示在图表中的特定字符串(在反应中)(例如作为 x 轴标题)。这仅在字符串不包含空格时有效,但如果包含空格则图表中不会显示任何字符串。

如何让它接受任何字符串?

这是我的代码(我修改了 Shiny 教程中的一个示例以使其尽可能简单):

# server.r
# Here the string is set depending on what was chosen by the user
shinyServer(function(input, output, session) {
label1_new <- reactive({
if (input$variable1=="pp_pmw") {label1_new <- "PP pmw"}
if (input$variable1=="perc_pp*100") {label1_new <- "PP percent"}
if (input$variable1=="formality") {label1_new <- "Formality"}
})

label1_new2 <- renderText({label1_new()})

output$distPlot <- renderPlot({
x    <- faithful[, 2]  # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins
# xlabel1_new2() contains the string from above
hist(x, breaks = bins, col = 'darkgray', border = 'white', xlab=label1_new2())
})

})


# ui.r
library(shiny)


shinyUI(fluidPage(

# Application title
titlePanel("Hello Shiny!"),

# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
  selectInput("variable1", "Circle size:",
            list("PP pmw" = "pp_pmw", 
                 "PP percent" = "perc_pp*100", 
                 "Formality" = "formality")),

  sliderInput("bins",
              "Number of bins:",
              min = 1,
              max = 50,
              value = 30)
),

# Show a plot of the generated distribution
mainPanel(
  plotOutput("distPlot")
)
)
))

renderText 用于 ui.r,不适用于创建要在 server.r

中使用的字符串
# server.r
# Here the string is set depending on what was chosen by the user
shinyServer(function(input, output, session) {
label1_new <- reactive({
  if (input$variable1=="pp_pmw") return("PP pmw")
  if (input$variable1=="perc_pp*100") return("PP percent")
  if (input$variable1=="formality") return("Formality")
})


output$distPlot <- renderPlot({
  x    <- as.numeric(unlist(faithful[, 2]))  # Old Faithful Geyser data
  bins <- seq(min(x), max(x), length.out = input$bins + 1)

  # draw the histogram with the specified number of bins
  hist(x, breaks = bins, col = 'darkgray', border = 'white', xlab=label1_new())
})

})

(同ui.r)