如何在R闪亮应用程序中从数据表中的选定行获取行值
我需要从DT数据表中获取选定的第一行第一列值。使用DATATABLE_ROWS_SELECTED,我能够获得选定的行数,现在我正在寻找从数据表中提取行值的方法。在下面的示例中,有两个基于操作的观察事件按钮,第一个观察事件是导入并显示数据,第二个观察事件需要显示选定的第一行第一列的值,以便我可以使用相同的实现其他功能。请注意,在实际应用中,导入的数据是Web服务API,我正在R中解析它并转换为Data Frame。
示例:
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Data Table Example"),
dashboardSidebar(
sidebarMenu(
menuItem('Tabs', tabName='tabs',
menuSubItem('Tab 1', tabName='tab1'),
menuSubItem('Tab 2', tabName='tab2')
)
)
),
dashboardBody(
tabItems(
tabItem(tabName='tab1',
actionButton("import","Import"),
br(),
tags$div(tags$h3(tags$b(" Get Selected Row Values",align="middle",style="color: rgb(57,156,8)"))),
br(),
DT::dataTableOutput('ProductDataTable')
),
tabItem(tabName='tab2',
actionButton("display","Display"),
uiOutput('info')
)
)
)
)
server <- function(input, output) {
observeEvent(input$import,{
Product <- read.csv2("RulesData.csv", header=TRUE, sep=";")
output$ProductDataTable <- DT::renderDataTable({
DT::datatable(Product,selection = "single",
extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
rownames=FALSE,
options=list(dom = 'Bfrtip',
searching = T,
pageLength = 25,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
filter = 'bottom',
buttons = c('copy', 'csv','excel', 'print'),
paging = TRUE,
deferRender = TRUE,
scroller = TRUE,
scrollX = TRUE,
scrollY = 700
))
})
})
observeEvent(input$display,{
row_count <- input$ProductDataTable_rows_selected
output$info <- renderPrint({
cat('Row Selected: ')
cat(row_count, sep = ', ')
cat(Product[1,2], sep = ', ')
})
})
}
shinyApp(ui, server)
解决方案
如果这是您要查找的内容,请检查下面的代码:
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Data Table Example"),
dashboardSidebar(
sidebarMenu(
menuItem('Tabs', tabName='tabs',
menuSubItem('Tab 1', tabName='tab1'),
menuSubItem('Tab 2', tabName='tab2')
)
)
),
dashboardBody(
tabItems(
tabItem(tabName='tab1',
actionButton("import","Import"),
br(),
tags$div(tags$h3(tags$b(" Get Selected Row Values",align="middle",style="color: rgb(57,156,8)"))),
br(),
DT::dataTableOutput('ProductDataTable')
),
tabItem(tabName='tab2',
actionButton("display","Display"),
uiOutput('info')
)
)
)
)
server <- function(input, output) {
Product <- reactive({mtcars})
observeEvent(input$import,{
output$ProductDataTable <- DT::renderDataTable({
DT::datatable(Product(),selection = "single",
extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
rownames=FALSE,
options=list(dom = 'Bfrtip',
searching = T,
pageLength = 25,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
filter = 'bottom',
buttons = c('copy', 'csv','excel', 'print'),
paging = TRUE,
deferRender = TRUE,
scroller = TRUE,
scrollX = TRUE,
scrollY = 700
))
})
})
observeEvent(input$display,{
output$info <- renderPrint({
row_count <- input$ProductDataTable_rows_selected
data <- Product()[row_count, ]
cat('Row Selected: ')
cat(data[,1]) #display the selected row 1st col value
})
})
}
shinyApp(ui, server)
我曾以mtcars
数据集为例,问题是您的数据位于observer
(带有input$import
的数据集)中,由于您需要使用它进行其他分析,如显示第一列的行值(我没有很好地理解您的意思,因为您的代码说明了不同的情况),因此数据必须移出observer
并放入reactive
。
[更新]
我使用if
语句而不是observeEvent
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "Data Table Example"),
dashboardSidebar(
sidebarMenu(
menuItem('Tabs', tabName='tabs',
menuSubItem('Tab 1', tabName='tab1'),
menuSubItem('Tab 2', tabName='tab2')
)
)
),
dashboardBody(
tabItems(
tabItem(tabName='tab1',
actionButton("import","Import"),
br(),
tags$div(tags$h3(tags$b(" Get Selected Row Values",align="middle",style="color: rgb(57,156,8)"))),
br(),
DT::dataTableOutput('ProductDataTable')
),
tabItem(tabName='tab2',
actionButton("display","Display"),
uiOutput('info')
)
)
)
)
server <- function(input, output) {
Product <- reactive({
if(input$import == 0)
{
return()
}
isolate({
input$import
data <- mtcars # Here read Your data: read.csv2("RulesData.csv", header=TRUE, sep=";")
})
})
output$ProductDataTable <- DT::renderDataTable({
DT::datatable(Product(),selection = "single",
extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
rownames=FALSE,
options=list(dom = 'Bfrtip',
searching = T,
pageLength = 25,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
filter = 'bottom',
buttons = c('copy', 'csv','excel', 'print'),
paging = TRUE,
deferRender = TRUE,
scroller = TRUE,
scrollX = TRUE,
scrollY = 700
))
})
observeEvent(input$display,{
output$info <- renderPrint({
row_count <- input$ProductDataTable_rows_selected
data <- Product()[row_count, ]
cat('Row Selected: ')
cat(data[,1]) #display the selected row 1st col value
})
})
}
shinyApp(ui, server)
相关文章