R中下划线的作用是什么?例如,在下面的代码中,行:input$tbl_rows_current
;确定当前显示的数据放入变量filtered_data
;但是,如果我将其更改为 input$tbl_rows_all
,所有过滤的数据都放入变量filtered data
.
我了解它在这里的作用,但它的一般用途是什么?
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)
最佳答案
下划线在语义上没有意义,它们只是变量名的一部分。 (在史前时代,_
是赋值运算符 <-
的同义词,不能用于变量名。) tbl_rows_current
和 tbl_rows_all
只是 input
的两个特定元素列表。根据作者的喜好,它们同样可以被称为
tblrowscurrent
和 tblrowsall
TblRowsCurrent
和 TblRowsAll
tbl.rows.current
和 tbl.rows.all
oranges
和 jackhammers
如果你喜欢这种东西,请查看 Are there any official naming conventions for R?
但是请注意,您不能更改这些名称;只有原始包作者才能拥有。这些元素不是在你的代码中定义的,而是在 Shiny 的一面——它是 Shiny 的 API/接口(interface)的一部分,它期望看到这些特定的元素(即具有这些特定名称的元素)。
关于r - 下划线的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38461561/