Home > Enterprise >  How to strip out of a reactive vector all numbers and the characters surrounding it?
How to strip out of a reactive vector all numbers and the characters surrounding it?

Time:08-03

In demo code shown at the bottom of this post, I'd like to strip out the number, ".", and space prefix from the "choice" column of the rendered choices2 dataframe.

So choice2 currently outputs this (shown in the panel labeled "Strip numbers out of reactive results ('choices2' dataframe)" when running the code):

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

I'd instead like choice2 to output this, stripping out the numeric prefixes (including the "." and the leading space):

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

How would I do this? I know the easy way is to simply recreate the data DF in choice2, but I'm specifically trying to learn how to strip characters out from a vector or dataframe (DF) generated in a reactive context. I don't have a clear understanding of how to work with vectors/DF in a reactive setting.

Also, the code may appear cumbersome for what it does. I'd like to preserve its overall structure (use of reactveValues() in combination with observeEvent(), instead of simply using reactive(), for example) as it's a redaction of longer code and it's also a learning exercise.

Demo code:

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)

CodePudding user response:

To expand on my comment above

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)
  • Related