英文:
Getting onClcik to work in a Shiny App when user clicks on a cell in a table
问题
我正在尝试构建一个Shiny App,并使onClick
函数输出用户单击的单元格行/列。
如何修复代码以显示单元格的行/列?
Shiny App
library(reactable)
library(shiny)
library(htmlwidgets)
iris = iris
ui <- fluidPage(
reactable::reactableOutput("irisTABLE"),
textOutput("'cellDATA")
)
# 定义绘制直方图所需的服务器逻辑
server <- function(input, output) {
output$irisTABLE = renderReactable({
reactable(iris,
onClick = JS("
function(rowInfo, colInfo) {
Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.row.CapRate, { priority: 'event' })
}
")
)
})
output$cellDATA = renderText({
paste0("选定的单元格是:", input$cell_data)
})
}
# 运行应用程序
shinyApp(ui = ui, server = server)
英文:
I am trying to build a Shiny App and get the onClick
function to output the cell row/column the user clicked on.
How can I fix the code to display the cell row/column?
Shiny App
library(reactable)
library(shiny)
library(htmlwidgets)
iris = iris
ui <- fluidPage(
reactable::reactableOutput("irisTABLE"),
textOutput("'cellDATA")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$irisTABLE = renderReactable({
reactable(iris,
onClick = JS("
function(rowInfo, colInfo) {
Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.row.CapRate, { priority: 'event' })
}
")
)
})
output$cellDATA = renderText({
paste0("The cell selected is: ", input$cell_data)
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案1
得分: 1
将output$cellDATA
更改为
if (!is.null(input$cell_data)) {
row_col <- strsplit(input$cell_data, "-")
paste0("所选单元格位于行 ", row_col[[1]][2], " 和列 ", row_col[[1]][1])
}
`library(reactable)
library(shiny)
iris = iris
ui <- fluidPage(
reactable::reactableOutput("irisTABLE"),
textOutput("cellDATA")
)
server <- function(input, output) {
output$irisTABLE = renderReactable({
reactable(iris,
onClick = JS("
function(rowInfo, colInfo, e) {
Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.index)
}
")
)
})
output$cellDATA = renderText({
if (!is.null(input$cell_data)) {
row_col <- strsplit(input$cell_data, "-")
paste0("所选单元格位于行 ", row_col[1][2], " 和列 ", row_col[1]1)
}
})
}
shinyApp(ui = ui, server = server)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/4IbOm.gif
英文:
Change theoutput$cellDATA
to
if (!is.null(input$cell_data)) {
row_col <- strsplit(input$cell_data, "-")
paste0("The cell selected is in row ", row_col[[1]][2], " and column ", row_col[[1]][1])
}
library(reactable)
library(shiny)
iris = iris
ui <- fluidPage(
reactable::reactableOutput("irisTABLE"),
textOutput("cellDATA")
)
server <- function(input, output) {
output$irisTABLE = renderReactable({
reactable(iris,
onClick = JS("
function(rowInfo, colInfo, e) {
Shiny.setInputValue('cell_data', colInfo.id + '-' + rowInfo.index)
}
")
)
})
output$cellDATA = renderText({
if (!is.null(input$cell_data)) {
row_col <- strsplit(input$cell_data, "-")
paste0("The cell selected is in row ", row_col[[1]][2], " and column ", row_col[[1]][1])
}
})
}
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论