1
votes

I'm having trouble with my shiny app. I want that the user can type in all the variables needed for the substr function to filter data from a data frame using dplyr.

I made an example using the dataframe iris.

In the textInput(select1) I like to type in "Species". In the numericInput(start1) I like to type in "4". In the numericInput(end1) I like to type in "6". In the textInput(match1) I like to type in "osa".

Now I want that the tableOutput only shows the rows which matches the criteria "osa" in the column "Species" from digit 4 to 6.

The numericInput(start1), the numericInput(end1) and the textInput(match1) are working. But the textInput(select1) doesn't work. When I'm using the input as variable I'm getting an empty data frame.

When I change the code an type in "Species" instead of reactivevar1() in the substr function I get the data frame I want.

Example:

filter(substr(Species, reactivevar2(), reactivevar3()) == reactivevar4())

This alternative works. But this is not what I want.

I want this to work:

filter(substr(reactivevar1(), reactivevar2(), reactivevar3()) == reactivevar4())

I tried different functions like substring and stringr::str_sub. I also tried as.character.

This is the full example:

library(shiny)
library(dplyr)

ui = fluidPage(
  textInput(inputId="select1", label="Type in variable", value = "", width = NULL, placeholder = NULL),
  numericInput(inputId="start1", label="Start digit", value=NULL, min = NA, max = NA, step = NA,
               width = NULL),
  numericInput(inputId="end1", label="End digit", value=NULL, min = NA, max = NA, step = NA,
               width = NULL),
  textInput(inputId="match1", label="Criteria to match", value = "", width = NULL, placeholder = NULL),
  actionButton(inputId="startfil", label="Start filter", icon = NULL, width = NULL),

  tableOutput('table')

)
server = function(input, output,session) {

  obs <- observeEvent(input$startfil, {

  var1 <- NA
    reactivevar1 <- reactive({
    var1 <- input$select1
    return(var1)})

  var2 <- NA
  reactivevar2 <- reactive({
    var2 <- input$start1
    return(var2)})

  var3 <- NA
  reactivevar3 <- reactive({
    var3 <- input$end1
    return(var3)})

  var4 <- NA
  reactivevar4 <- reactive({
    var4 <- input$match1
    return(var4)})


  irisfiltered <- iris %>%
    filter(substr(reactivevar1(), reactivevar2(), reactivevar3()) == reactivevar4()) #reactivevar1() doesn't work
  output$table <- renderTable(irisfiltered)

  })

}

shinyApp(ui = ui, server = server)

I just can't figure out what is wrong with my code. It is important that the user can type in a start and an end digit to filter the substring.

1

1 Answers

0
votes

Welcome to SO! reactivevar1() has the value "Species", so your substr function returns "cie". And

substr(reactivevar1(), reactivevar2(), reactivevar3()) == reactivevar4()

returns FALSE, when you type i.e. "osa" in reactivevar4()

You could use getin your pipe statement like this:

irisfiltered <- iris %>%
      filter(substr(get(reactivevar1()), reactivevar2(), reactivevar3()) == reactivevar4())
output$table <- renderTable(irisfiltered)

Or make use of !! and as.name

    iris %>%
        filter(substr(!!as.name(reactivevar1()), reactivevar2(), reactivevar3()) == reactivevar4())
 output$table <- renderTable(irisfiltered)

Hope this helps