我想创建一个 R Shiny 模块,它生成一个输出,总结每个部门的最小、最大和平均销售额。如何向输出中的最小值和最大值添加超链接?通过单击该链接,它会将您定向到另一个选项卡,显示该特定部门内销售额为最小或最大的销售人员的姓名。
如何使用 R Shiny 实现这一点?
非常感谢!
如果能提供示例程序就太好了
这不是最好的解决方案,但它满足了您的要求。必须拿出数据和接口。
library(shiny)
library(DT)
ui <- fluidPage(
tabsetPanel(
id = "tabset",
type = "hidden", #You can comment this line out ti see the tabset panels
tabPanel(title = "output",
dataTableOutput(outputId = "DT")),
tabPanel(title = "foo",
verbatimTextOutput("Salesmanfoo",
placeholder = TRUE),
actionButton("Back1",
"Go Back")),
tabPanel(title = "bar",
verbatimTextOutput("Salesmanbar",
placeholder = TRUE),
actionButton("Back2",
"Go Back"))
)
)
server <- function(input, output) {
# Simple data frames since you didn't provide any data
df <- data.frame(department = c("foo", "bar"),
minsale = c(40, 20),
maxsale = c(90, 60))
sales_foo <- data.frame(name = c("John", "Alex", "Andrea"),
sales = c(75, 90, 40))
sales_bar <- data.frame(name = c("Maria","Josh"),
sales = c(60, 20))
output$DT <- renderDataTable({datatable(df,
selection = list(mode = "single",
target = "cell")) %>%
formatStyle(c("minsale", "maxsale"),
cursor = 'pointer')
})
observe({
# access click information
selection <- input$DT_cell_clicked
department <- df[selection$row, "department"]
# do nothing if not clicked yet, or the clicked cell is not in the 1st column
if(is.null(selection$value) || selection$col %in% c(0, 1)) return()
# switch tabsetPanel
updateTabsetPanel(inputId = "tabset",
selected = department)
# bad solution to output the names
if(department == "foo"){
output$Salesmanfoo <- renderText(sales_foo[sales_foo$sales == selection$value, "name"])
}
if(department == "bar"){
output$Salesmanbar <- renderText(sales_bar[sales_bar$sales == selection$value, "name"])
}
}) %>%
bindEvent(input$DT_cells_selected)
# to jump back to the main tabset panel
observe({
updateTabsetPanel(inputId = "tabset",
selected = "output")
}) %>%
bindEvent(c(input$Back1, input$Back2))
}
# Run the application
shinyApp(ui = ui, server = server)