Problems with selection colomns


#1

Hello,

I'm a beginner with R and I have some questions about my first project.

I've got a database with following types of data in data.frame called db2:

'data.frame':	5303 obs. of  9 variables:
 $ Metric.ID          : num  7156 7220 7220 7220 7220 ...
 $ Metric.Name        : Factor w/ 99 levels "Avoid accessing",..: 51 59 59 59 59 59 59 59 59 59 ...
 $ Technical.Criterion: Factor w/ 25 levels "Architecture",..: 4 9 9 9 9 9 9 9 9 9 ...
 $ RT.Snapshot.name   : Factor w/ 1 level "2017": 1 1 1 1 1 1 1 1 1 1 ...
 $ Violation.status   : Factor w/ 2 levels "Added","Deleted": 2 1 2 2 2 1 1 1 1 1 ...
 $ Critical.Y.N       : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Grouping           : Factor w/ 29 levels ": 27 6 6 6 6 7 7 7 7 7 ...
 $ Object.type        : Factor w/ 11 levels ",..: 8 7 7 7 7 7 7 7 7 7 ...
 $ Object.name        : Factor w/ 3771 levels

Now I want to make a simple UI to filter some rows and also can select columns to display with a default length of 10 rows.

In this moment I get following message: Warning: Error in [.data.frame: undefined columns selected

This is my ui.r

library(shiny)

shinyUI(fluidPage
        (
          titlePanel("Dashboard"),
          sidebarLayout(
            sidebarPanel(
              selectInput("MetIDlist",
                          "Metric.ID",
                          c("All",
                            unique(as.character(db2$Metric.ID)))),
              selectInput("TClist",
                          "Technical Criterion",
                          c("All",
                            unique(as.character(db2$Technical.Criterion)))),
              selectInput("RTlist",
                          "Release",
                          c("All",
                            unique(as.character(db2$RT.Snapshot.name)))),
              selectInput("Objectlist",
                          "Objects",
                          c("All",
                            unique(as.character(db2$Object.type)))),
              selectInput("VioStlist",
                          "Violation Status",
                          c("All",
                            unique(as.character(db2$Violation.status)))),
              selectInput("Critlist",
                          "Critical",
                          c("All",
                            unique(as.character(db2$Critical.Y.N)))),
              selectInput("Grouplist",
                          "Group",
                          c("All",
                            unique(as.character(db2$Grouping)))),
              conditionalPanel(condition="db2",
                selectizeInput("show_vars", "Spalten",
                                 names(db2), selected = names(db2), multiple = TRUE)),
              width = 2),
            mainPanel(
               tabsetPanel(
                tabPanel("List", dataTableOutput("table")),
                tabPanel("Summary")
              )
            )
          )
        ))


and this is my server.r

library(shiny)
library(dplyr)
library(stats)
library(DT)

shinyServer(function(input, output) 
  {  
  output$table <- DT::renderDataTable(DT::datatable({
    data <- db2
    
    if (input$MetIDlist != "All") {
      data <- data[as.character(db2$Metric.ID) == input$MetIDlist]
    }
    if (input$TClist != "All") {
      data <- data[as.character(data$Technical.Criterion) == input$TClist]
    }
    if (input$RTlist != "All") {
      data <- data[as.character(data$RT.Snapshot.name) == input$RTlist]
    }
    if (input$Objectlist != "All") {
      data <- data[as.character(data$Object.type) == input$Objectlist]
    }
    if (input$VioStlist != "All") {
      data <- data[as.character(data$Violation.status) == input$VioStlist]
    }
    if (input$Critlist != "All") {
      data <- data[as.character(data$Critical.Y.N) == input$Critlist]
    }
    if (input$Grouplist != "All") {
      data <- data[as.character(data$Grouping) == input$Grouplist]
    }
    data
  }))  
})
  
# dataout <- data()
# dataout <- dataout[, input$show_vars, drop = FALSE]
#
# output$table <- DT::renderDataTable(data(), options=list(pageLength=10))  

There are different ways to solve the problem. But I can't get the parts together for a simple solution.

Please help.

Thanks


#2

I think my problems are the factor columns within my data.frame.

I don't know if it's a good idea to convert it, because I have very stable data in my data.frame and didn't want to manipulate them. They are read only for me.

But I have no idea to use the factor variables correct for my filtering with the selectinput statement.


#3

Is there any hint for me?


#4

You can not use any function in ui part of the code (like unique(as.character(db2$Metric.ID)))). This is just for basic html output not for calculating stuff.

you should try

renderUI function in Server code and generate the select input widget in the server

then use OUtputUI in ui side of the code and call it. This is how you should do it.


#5

thanks for this hint. Did you have a short code sample for me? thanks

when I use a sort function in my UI part it will still work. the sample here will work with functions within the ui: https://shiny.rstudio.com/gallery/basic-datatable.html


#6

That doesn't work. I use this at the moment.

server.ui

   output$select_MectricID <- renderUI({
     selectInput("MetIDlist", 
                 label = "MetIDlist",
                 c("All", sort(unique(db2$Metric.ID))))
   })

and in my ui.r

uiOutput("select_MectricID"),

It's the same error. When I change inside my Dashboard from "all" to a value, I get following error inside my datatableoutput. Error: undefined columns selected


#7

It works

At the end I changed the selecitzeinput statement in the ui.r

selectizeInput('e2', 'Auswahl', selected = names(db2), choices = names(db2), multiple = TRUE),

and the last statement in my server.r

db2out <- db2out[db2out$Metric.ID == input$MetIDlist,]  # Missing comma at the end

db2out[, input$e2, drop = FALSE]

#8

This is not correct. You can use R code in your ui.R (in fact, unless you are writing it in pure HTML, it is all R code!). You just have to make sure that it evaluates to the correct data type for where you put it. In this example, as @talcom is correctly doing, the choices argument just has to evaluate to a list or vector


#9

I just posted a code here yesterday you should take a look at it and yes create a reactive data frame so that you can use it. Go to rstudio.com understand what reactive data frame is

Like

Df()$Metric.ID