2

Manufacturer == sales_by_mfr$Manufacturer[index])我创建了一个可反应的表 1,当单击行(可扩展行)时,其中有一个额外的表(表 2 - 实际上有很多表,因为它过滤)。

library(reactable)
data <- MASS::Cars93[18:47, ] %>%
  mutate(ID = as.character(18:47), Date = seq(as.Date("2019-01-01"), by = "day", length.out = 30)) %>%
  select(ID, Date, Manufacturer, Model, Type, Price)

sales_by_mfr <- group_by(data, Manufacturer) %>%
  summarize(Quantity = n(), Sales = sum(Price))

reactable(
  sales_by_mfr,
#part for Table 2
  details = function(index) {
    sales <- filter(data, Manufacturer == sales_by_mfr$Manufacturer[index]) %>% select(-Manufacturer)
    tbl <- reactable(sales, outlined = TRUE, highlight = TRUE, fullWidth = FALSE)
    htmltools::div(style = list(margin = "12px 45px"), tbl)
  },
#End of Table 2
  onClick = "expand",
  rowStyle = list(cursor = "pointer")
)

我希望表 2 出现在新窗口中。IE 如果我点击制造商 ==“福特”行然后打开一个带有表 2 的窗口,同时当我点击Manufacturer == "Dodge" 行时会出现一个带有表 2 的新窗口(同时不关闭一个窗口Manufacturer == "Ford"

我尝试过这样的事情,但是,它并没有按照我想要的方式工作。


reactable(
  sales_by_mfr,
  columns = list(
    # Render a "show details" button in the last column of the table.
    # This button won't do anything by itself, but will trigger the custom
    # click action on the column.
    details = colDef(
      name = "",
      sortable = FALSE,
      cell = function() htmltools::tags$button("Show details")
    )
  ),
  onClick = JS("function(rowInfo, colInfo) {
    // Only handle click events on the 'details' column
    if (colInfo.id !== 'details') {
      return
    }

    // Display an alert dialog with details for the row
    window.alert('Details for row ' + rowInfo.index + ':\\n' + JSON.stringify(rowInfo.row, null, 2))

    // Send the click event to Shiny, which will be available in input$show_details
    // Note that the row index starts at 0 in JavaScript, so we add 1
    if (window.Shiny) {
      Shiny.setInputValue('show_details', { index: rowInfo.index + 1 }, { priority: 'event' })
    }
  }")
)

4

0 回答 0