4

What is the function of an underscore in R? For example, in the code below, the line: input$tbl_rows_current; determines that the current data being displayed is placed into the variable filtered_data; however, if I change it to input$tbl_rows_all, all the data filtered is placed into the variable filtered data.

I understand how it functions here, but what is its general use?

ui = fluidPage(dataTableOutput('tbl'),
           plotOutput('plot1')
)

server = function(input, output) {   
 output$tbl = renderDataTable({
    datatable(KSI, filter="top",rownames=TRUE,options = list(lengthChange = FALSE))
})
output$plot1 = renderPlot({  
   filtered_data <- as.numeric(*input$tbl_rows_current*)     
   print(filtered_data)   
 })
}  
shinyApp(ui=ui, server=server)
Spacedman
  • 92,590
  • 12
  • 140
  • 224
B.Doe
  • 43
  • 1
  • 4
  • 5
    Please only use the `rstudio` tag if your question concerns the RStudio code editor. You wouldn't use a `pen-and-paper` tag when asking a question about grammar! – Gregor Thomas Jul 19 '16 at 17:55

1 Answers1

8

Underscores are not semantically meaningful, they're just part of the variable name. (In the prehistoric era, _ was synonymous with the assignment operator <- and couldn't be used in variable names.) tbl_rows_current and tbl_rows_all are just two particular elements of the input list. Depending on the preferences of the author, they could equally well have been called

  • tblrowscurrent and tblrowsall
  • TblRowsCurrent and TblRowsAll
  • tbl.rows.current and tbl.rows.all
  • oranges and jackhammers

If you like this sort of thing, check out Are there any official naming conventions for R?

Note, however, that you can't change these names; only the original package author could have. These elements are defined not in your code, but on the shiny side -- it's part of the shiny API/interface that it's expecting to see these particular elements (i.e., elements with these particular names).

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • +1 but it’s worth pointing out that it’s not a *subjective* preference: I can’t fault option 2 (though I personally hate it) but all the other options are objectively inferior. – Konrad Rudolph Jul 19 '16 at 14:44
  • 4
    this is the stuff of flame wars. I know an R Core member who prefers dot-separated `x.y` to snake case `x_y` because (1) it's traditional in old-fashioned R code and (2) he claims it's easier not to have to hit the shift key (!) – Ben Bolker Jul 19 '16 at 14:46
  • 1
    But it simply clashes with S3 dispatch, there’s an objective argument for why this convention is bad. – Konrad Rudolph Jul 19 '16 at 14:48
  • 1
    I happen to agree with you. I'm just saying it gets contentious. – Ben Bolker Jul 19 '16 at 14:49
  • There are many different opinions out there. Here's Google's "style guide" for R, advocating dot-separated identifiers and discouraging the use of the snake case: https://google.github.io/styleguide/Rguide.xml – RHertel Jul 19 '16 at 15:18
  • @BenBolker Thank you for the response. The penny is on the ledge but it hasn't quite dropped. If I change the code to `input$tblrowsall`; I get no output. At what point have I defined these elements in my input list? This is interesting as going from `all` to `current` changes nothing in my output. I thought this may have been a dev tools upgrade problem but perhaps I've misunderstood something more fundemental? – B.Doe Jul 20 '16 at 11:35
  • 1
    these elements are defined not in your code, but on the shiny side -- it's part of the shiny API/interface that it's expecting to see these particular elements (i.e., elements with these particular names) – Ben Bolker Jul 20 '16 at 19:42