4

Suppose I have 2 columns in my dataset, and I want to output out a histogram and summary depending on which column we are on, currently I have 2 if statements per output checking what the input is equal to.

ui <- fluidPage(
  titlePanel("First iteration"),
  sidebarLayout(
    sidebarPanel("Sidebar Panel",
    selectInput("typetoshow", "Choose what you want to display", 
                choices = c("pts", "upts"))             
                 ),
    mainPanel("main panel",
              plotOutput("hist"),
              verbatimTextOutput("sum")
              )
  )
)

server <- function(input, output){
  output$hist <- renderPlot({
    if (input$typetoshow == "pts"){
      hist(data$pts)
    }
    if (input$typetoshow == "upts"){
      hist(data$upts)
    }
  })
  output$sum <- renderPrint({
    if (input$typetoshow == "pts"){
      summary(data$pts)
    }
    if (input$typetoshow == "upts"){
     summary(data$upts)
    }
  })
}

I tried doing

hist(data$input$typetoshow)

But it is giving me an error saying 'x must be numeric' in the histogram and shows NULL for the output summary, is there any way to do this without making many if statements? I am going to be dealing with 10+ columns at one point so I want the code to be neat.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Wallace
  • 340
  • 1
  • 3
  • 9

2 Answers2

1

We can use req at the top and then use [[ to subset the dataset column based on the value from 'input$typetoshow'

server <- function(input, output){
  output$hist <- renderPlot({
    req(input$typetoshow)
    
    
      hist(data[[input$typetoshow]])
   
  })
  output$sum <- renderPrint({
       req(input$typetoshow)
    
      summary(data[[input$typetoshow]])
    
  })
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Sorry I don't see how this simplifies anything? The number of if statements remains the same? – Wallace Jun 07 '21 at 17:14
  • @Wallace I read your question as `But it is giving me an error saying 'x must be numeric' in the histogram and shows NULL` – akrun Jun 07 '21 at 17:15
  • Ah, no the general block of code I sent you works fine, it's just I am trying to remove the need for all the if statements, so rather than check what the value is equal to, I want to just plot the input type – Wallace Jun 07 '21 at 17:16
  • @Wallace the issue is that I saw an error in the description and I thought it is what you want to rectify – akrun Jun 07 '21 at 17:16
  • The error occurs when I attempt ```hist(data$input$typetoshow)```, not when there are if statements, sorry for being unclear – Wallace Jun 07 '21 at 17:18
  • 1
    @Wallace updaed the post – akrun Jun 07 '21 at 17:19
  • Wow, this worked and was exactly what I wanted, thank you so much. You already solved my problem but if you have extra time do you mind explaining to me what the double square brackets do in R? I primarily work with Python – Wallace Jun 07 '21 at 17:21
  • 1
    @Wallace It is just that `$` match literally and your code is correct with `if/else` when you are using `$`, but `[[` is more general as it can evaluate the object to get the value inside i.e. from `input$typetoshow`, with `$`, it will be read as literally – akrun Jun 07 '21 at 17:22
0

You can make it simpler with dplyr::select:

server <- function(input, output){
  output$hist <- renderPlot({
    hist(unlist(dplyr::select(data, input$typetoshow)))
  })
  output$sum <- renderPrint({
    summary(dplyr::select(data, input$typetoshow))

    })
}
Pedro Alencar
  • 1,049
  • 7
  • 20