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)
}
}
})
}
}
})
}
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,]