R Shiny 单击 table 字段

R Shiny click on table field

我目前正在学习 R。我有一个显示时间表的小项目,用户可以选择输入主题。

将主题添加到时间表后,应该可以点击它打开模态对话框。 不幸的是我的代码不起作用。我在这里试过:

observeEvent(input$mytable_cells_selected, {
    showModal(modalDialog(
      title = "Somewhat important message",
      "This is a somewhat important message.",
      easyClose = TRUE,
      footer = NULL))
  })

谁能帮我看看我的错误在哪里?

ui <- fluidPage(
    theme = bs_theme(version = 4, bootswatch = "minty"),
      titlePanel(h1("My timetable", align = "center" )),
      sidebarLayout(
        position = c("left"),
        sidebarPanel(
          width = 4,
          selectInput("select1", label = h5("Event:"),
                      choices = c("math" , "sience", "sport") ,
                      selected = 1,
                      width = 400),
          actionButton("action", label = "Add")),
        mainPanel(
          width = 8,
          tableOutput('mytable')),
      ),
    )

和服务器:

server <- function(input, output, session) {
  
  timetable <- reactiveVal(
    data.frame(monday = c("","","","",""),
               tuesday = c("","","","",""),
               wednesday = c("","","","",""),
               thursday = c("","","","",""),
               friday = c("","","","",""))
  )
   
  output$mytable <- renderTable(timetable(), 
          bordered = TRUE, 
          spacing = c('l'), 
          width = "100%",
          striped = TRUE,
          align = 'c',
          rownames = TRUE,
          selection = list(target = 'cell'))
  
  observeEvent(input$action, { 
    tmp <- timetable()
    tmp[1, "monday"] <- input$select1
    timetable(tmp)
  })

  observeEvent(input$mytable_cells_selected, {
      showModal(modalDialog(
         title = "message",
         "This is a somewhat important message.",
         easyClose = TRUE,
         footer = NULL))
})
}

shinyApp(ui, server)

如评论中所述,可以使用DT库。这是一个完整的例子。

在您的 ui 中使用 dataTableOutput 作为您的数据 table。

server中,您可以在此处包含renderDataTable并自定义。在这种情况下,选择设置为单个单元格。

您可以使用input$my_table_cells_selected捕获选择事件(或者可以捕获点击事件)。在我的版本中,我对 my_table 使用了下划线。此信息将包括所选单元格的行值和列值。

请注意,DT 数据 table 可以是 editable 并允许其他交互,具体取决于您的需要。

library(shiny)
library(bslib)
library(DT)

ui <- fluidPage(
  theme = bs_theme(version = 4, bootswatch = "minty"),
  titlePanel(h1("My timetable", align = "center" )),
  sidebarLayout(
    position = c("left"),
    sidebarPanel(
      width = 4,
      selectInput("select1", label = h5("Event:"),
                  choices = c("math" , "sience", "sport") ,
                  selected = 1,
                  width = 400),
      actionButton("action", label = "Add")),
    mainPanel(
      width = 8,
      dataTableOutput('my_table')
    )
  )
)

server <- function(input, output, session) {
  
  timetable <- reactiveVal(
    data.frame(monday = c("","","","",""),
               tuesday = c("","","","",""),
               wednesday = c("","","","",""),
               thursday = c("","","","",""),
               friday = c("","","","",""))
  )
   
  output$my_table = renderDataTable(timetable(), selection = list(mode = "single", target = "cell"))
  
  observeEvent(input$action, { 
    tmp <- timetable()
    tmp[1, "monday"] <- input$select1
    timetable(tmp)
  })
  
  observeEvent(input$my_table_cells_selected, {
    req(input$my_table_cells_selected)
    showModal(modalDialog(
      title = "message",
      paste("This is a somewhat important message:", 
            input$my_table_cells_selected[1],
            input$my_table_cells_selected[2]),
      easyClose = TRUE,
      footer = NULL))
  })
  
}

shinyApp(ui, server)