B.Doe B.Doe - 2 years ago 38
R Question

Meaning of underscore

What is the function of an underscore in R? For example, in the code below, the line:

; determines that the current data being displayed is placed into the variable
; however, if I change it to
, 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'),

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*)
shinyApp(ui=ui, server=server)

Answer Source

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 http://stackoverflow.com/questions/10013545/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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download