1

I have a shiny application that allows the user to subset a data frame by selecting a column name from a dropdown list and assign a value to it. In the snippet below, df1 is a dataframe and if the user selects "country" from the dropdown and enters "UK" in the textbox, the temporary df, temp_dat should have only values of UK in it. For some reason that I cannot understand it states that 0 rows of 3 cols have been returned. Can somebody help me with this? Thanks in advance.

library(shinydashboard)
ui = dashboardPage(dashboardHeader(),
                   dashboardSidebar(),
                   dashboardBody(uiOutput("subset_check1"), uiOutput("subsetbox1‌​")))

server = function(input, output, session)
{
  names = c("Jane", "Doe", "Job", "Right")
  emp_id = c(1000, 1001, 1002, 1003)
  country = c("UK", "UK", "UK", "US")
  df1 = data.frame(names, emp_id, country)
  file1_cols = colnames(df1)
  output$subset_check1 = renderUI(checkboxInput(
    "subcheck1",
    "Would you like to subset this dataset with a condition?"
  ))
  observe({
    if (!is.null(input$subcheck1))
    {
      if (input$subcheck1 == "TRUE")
      {
        #print("box was checked")
        output$subsetbox1 = renderUI(flowLayout(
          selectInput(
            "subsetbox1sel",
            "Select column:",
            choices = c(file1_cols),
            multiple = T
          ),
          textInput("subsetbox1text", "Enter value")
        ))
        observe({
          if (!is.null(input$subsetbox1sel))
          {
            if (!is.null(input$subsetbox1text))
            {
              cols_sel1 = as.character(input$subsetbox1sel)
              vals_sel1 = as.character(input$subsetbox1text)
              temp_dat = df1[df1$cols_sel1 == vals_sel1, ]
              print(temp_dat)
            }
          }
        })
      }
    }
  })
} 
3
  • I even tried: subset(df1,cols_sel1==vals_sel1) to no avail. I cannot fathom what I'm doing wrong. When I print cols_sel1 I see country and vals_sel1 I see UK as characters. Why then does temp_dat still not have the data I expect. Commented Sep 29, 2016 at 22:09
  • FYI: To make the code runnable I had to add library(shinydashbard) and modify the ui code into ui=dashboardPage(dashboardHeader(), dashboardSidebar(), dashboardBody(uiOutput("subset_check1"),uiOutput("subsetbox1"))). Commented Sep 30, 2016 at 7:23
  • Apologies. Updated the question to reflect this. Commented Sep 30, 2016 at 14:45

1 Answer 1

3

Currently, there is no column named cols_sel1 in the dataframe. But you intend to pass its string literal value. Hence, do not use the dollar $ qualifier but a string to reference the column (i.e., use latter of below) especially since this cols_sel1 varies with user selection:

df$col
df['col']

So simply adjust your temp_dat line to:

temp_dat = df1[df1[cols_sel1]==vals_sel1,]
Sign up to request clarification or add additional context in comments.

2 Comments

@rookieJoe Please accept this answer since it works like a charm
Indeed it works like a charm! Thanks a tonne @Parfait.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.