Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of underscore

Tags:

r

shiny

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)
like image 773
B.Doe Avatar asked Mar 12 '23 16:03

B.Doe


1 Answers

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).

like image 84
Ben Bolker Avatar answered Mar 23 '23 02:03

Ben Bolker