使Shiny应用中的onClcik在用户单击表中的单元格时起作用。

huangapple go评论58阅读模式
英文:

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 &lt;- fluidPage(

  reactable::reactableOutput(&quot;irisTABLE&quot;),
  textOutput(&quot;&#39;cellDATA&quot;)
)

# Define server logic required to draw a histogram
server &lt;- function(input, output) {

  output$irisTABLE = renderReactable({
    reactable(iris,
              onClick = JS(&quot;
                                function(rowInfo, colInfo) {
                                Shiny.setInputValue(&#39;cell_data&#39;, colInfo.id + &#39;-&#39; + rowInfo.row.CapRate, { priority: &#39;event&#39; })
                                }
                               &quot;)
    )
  })

  output$cellDATA = renderText({
    paste0(&quot;The cell selected is: &quot;, 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 &lt;- strsplit(input$cell_data, &quot;-&quot;)
paste0(&quot;The cell selected is in row &quot;, row_col[[1]][2], &quot; and column &quot;, row_col[[1]][1])
}

library(reactable)
library(shiny)

iris = iris

ui &lt;- fluidPage(
  reactable::reactableOutput(&quot;irisTABLE&quot;),
  textOutput(&quot;cellDATA&quot;)
)

server &lt;- function(input, output) {
  
  output$irisTABLE = renderReactable({
    reactable(iris,
              onClick = JS(&quot;
                function(rowInfo, colInfo, e) {
                  Shiny.setInputValue(&#39;cell_data&#39;, colInfo.id + &#39;-&#39; + rowInfo.index)
                }
              &quot;)
    )
  })
  
  output$cellDATA = renderText({
    if (!is.null(input$cell_data)) {
      row_col &lt;- strsplit(input$cell_data, &quot;-&quot;)
      paste0(&quot;The cell selected is in row &quot;, row_col[[1]][2], &quot; and column &quot;, row_col[[1]][1])
    }
  })
}

shinyApp(ui = ui, server = server)

使Shiny应用中的onClcik在用户单击表中的单元格时起作用。

huangapple
  • 本文由 发表于 2023年2月19日 21:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定