r语言 - 如何从反应向量中去除所有数字及其周围的字符?



在本文底部显示的演示代码中,我想从渲染choices2数据帧的"选择"列中删除数字"."和空格前缀。

所以choice2当前输出这个(显示在运行代码时标记为"从反应结果('choices2'数据帧)中删除数字"的面板中):

choice subclass
1   1. A        1
2   2. A        1
3   3. A        2
4   4. B        1
5   5. B        3

相反,我想choice2输出这个,去掉数字前缀(包括"."和前导空格):

choice subclass
1      A        1
2      A        1
3      A        2
4      B        1
5      B        3

我该怎么做?我知道最简单的方法是简单地在choice2中重新创建dataDF,但我专门尝试学习如何从响应式上下文中生成的向量或数据帧 (DF) 中去除字符。我对如何在反应式环境中使用矢量/DF 没有清晰的理解。

此外,代码的功能可能看起来很麻烦。我想保留它的整体结构(将reactveValues()observeEvent()结合使用,而不是简单地使用reactive(),例如),因为它是对较长代码的编辑,也是一个学习练习。

演示代码:

library(shiny)
data <- data.frame(choice = c("A", "A", "A", "B", "B"), subclass = c(1, 1, 2, 1, 3))
ui <- fluidPage(
h5(strong("Base data frame ('data' dataframe):")),
verbatimTextOutput("data"),
radioButtons(
inputId = "showData",
label = h5(strong("Multiply base DF subclass by factor of:")),
choiceNames = c("One", "Two"),
choiceValues = c("One", "Two"),
selected = "One",
inline = TRUE
),
h5(strong("Reactive results ('choices1' dataframe):")),
verbatimTextOutput("choices1"),
h5(strong("Strip numbers out of reactive results ('choices2' dataframe):")),
verbatimTextOutput("choices2")
)
server <- function(input, output, session) {
output$data <- renderPrint(data)

rv <- reactiveValues(choices1 = c())

observeEvent(input$showData, {
rv$choices1 <- data
rv$choices1$choice <- paste0(row.names(rv$choices1), ". ", rv$choices1$choice)
if (input$showData == "Two") {
rv$choices1[, 2] <- 2 * rv$choices1[, 2]
}
})

output[["choices1"]] <- renderPrint({
rv$choices1
})

# would like choices2 to strip number, ".", and the space out of choices1
output[["choices2"]] <- renderPrint({
rv$choices1
})

}
shinyApp(ui, server)

扩展我上面的评论

library(shiny)
data <- data.frame(choice = c("A", "A", "A", "B", "B"), subclass = c(1, 1, 2, 1, 3))
ui <- fluidPage(
h5(strong("Base data frame ('data' dataframe):")),
verbatimTextOutput("data"),
radioButtons(
inputId = "showData",
label = h5(strong("Multiply base DF subclass by factor of:")),
choiceNames = c("One", "Two"),
choiceValues = c("One", "Two"),
selected = "One",
inline = TRUE
),
h5(strong("Reactive results ('choices1' dataframe):")),
verbatimTextOutput("choices1"),
h5(strong("Strip numbers out of reactive results ('choices2' dataframe):")),
verbatimTextOutput("choices2")
)
server <- function(input, output, session) {
output$data <- renderPrint(data)

# Edited
rv <- reactiveValues(choices1 = c(), choices2=NA)

observeEvent(input$showData, {
rv$choices1 <- data
rv$choices1$choice <- paste0(row.names(rv$choices1), ". ", rv$choices1$choice)
if (input$showData == "Two") {
rv$choices1[, 2] <- 2 * rv$choices1[, 2]
}
})

# New observeEvent
observeEvent(rv$choices1, {
rv$choices2 <- rv$choices1 %>% mutate(choice=stringr::str_sub(choice, -1))
})

output[["choices1"]] <- renderPrint({
rv$choices1
})

# would like choices2 to strip number, ".", and the space out of choices1
output[["choices2"]] <- renderPrint({
# Edit here
rv$choices2
})

}
shinyApp(ui, server)

最新更新