在下面的示例代碼中,我希望每次單擊“添加”按鈕都會將 1 添加到下面的小“資料”表中顯示的值。下面的代碼沒有“添加”按鈕,而是有“測驗添加”按鈕以這種方式作業,用于演示目的。我想讓“添加”做“測驗添加”當前所做的事情,然后洗掉“測驗添加”。
“顯示表格”按鈕按原樣作業:每次單擊都會在下面呈現一個更大的表格(不相關,這只是一個簡單的示例)。并且“添加”按鈕在每次單擊時都可以正確隱藏渲染的較大表格;但我還想單擊“添加”以添加 1,如上所述。
在下面的代碼中,我注釋掉了observeEvent("input.show == 'Add'",{x(x() 1)}),這是我嘗試單擊“添加”導致添加 1。我該如何糾正這個問題,所以observeEvent()本質上是由 觸發的input.show == 'Add'?
這張圖片有助于解釋:

代碼:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
br(),actionButton("add","Test add"), br(),br(),
radioGroupButtons(
inputId = "show",
label = NULL,
choices = c("Add","Show table"),
status = "primary",
selected = "Add"
),
br(),
fluidRow(tableOutput("data")),
fluidRow(
conditionalPanel(
"input.show == 'Show table'",
column(10,
column(4, h5(textOutput("text"))),
column(6, tableOutput("table")),
style = "border: 2px solid grey; margin-left: 15px;"
),
style = "display: none;"
)
)
)
server <- function(input, output, session) {
x = reactiveVal(0)
output$data <- renderTable(x())
output$table <- renderTable(iris[1:5, 1:3])
output$text <- renderText("Test show/hide in JS")
observeEvent(input$add,{x(x() 1)})
# observeEvent("input.show == 'Add'",{x(x() 1)})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
show每隔一次show單擊“添加”,可以輕松地看到創建值增加 1。
observeEvent(input$show, {
req(input$show == "Add")
x(x() 1)
})
問題是您必須單擊Show table一次然后單擊Add,然后該值會增加。如果連續點擊Add,數值只會增加一倍。為了解決這個問題,我們可以給Add按鈕系結一個新的 JS 事件,并將值發送給 R。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
br(), br(), br(), br(),
radioGroupButtons(
inputId = "show",
label = NULL,
choices = c("Add","Show table"),
status = "primary",
selected = "Add"
),
tags$script(HTML(
"
$(()=>{
var clicks = 0;
$('#show button').first().on('click', ()=> {
clicks ;
Shiny.setInputValue('add_button', clicks);
});
})
"
)),
br(),
fluidRow(tableOutput("data")),
fluidRow(
conditionalPanel(
"input.show == 'Show table'",
column(10,
column(4, h5(textOutput("text"))),
column(6, tableOutput("table")),
style = "border: 2px solid grey; margin-left: 15px;"
),
style = "display: none;"
)
)
)
server <- function(input, output, session) {
x <- reactiveVal(0)
output$data <- renderTable(x())
output$table <- renderTable(iris[1:5, 1:3])
output$text <- renderText("Test show/hide in JS")
observeEvent(input$add_button, {
x(input$add_button)
})
}
shinyApp(ui, server)
選擇
如果這個值只是用來顯示按鈕被點擊了多少次,或者如果你關心性能,我們可以純粹在 JS 中處理這個作業,這意味著不需要服務器互動。如果您有大量用戶,以下內容將有助于減輕您的服務器負擔。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
br(), br(), br(), br(),
radioGroupButtons(
inputId = "show",
label = NULL,
choices = c("Add","Show table"),
status = "primary",
selected = "Add"
),
tags$script(HTML(
"
$(()=>{
var clicks = 0;
$('#show button').first().on('click', ()=> {
clicks ;
$('#data td').text(` ${clicks} `);
});
})
"
)),
br(),
fluidRow(tableOutput("data")),
fluidRow(
conditionalPanel(
"input.show == 'Show table'",
column(10,
column(4, h5(textOutput("text"))),
column(6, tableOutput("table")),
style = "border: 2px solid grey; margin-left: 15px;"
),
style = "display: none;"
)
)
)
server <- function(input, output, session) {
output$data <- renderTable(0)
output$table <- renderTable(iris[1:5, 1:3])
output$text <- renderText("Test show/hide in JS")
}
shinyApp(ui, server)

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/515816.html
下一篇:如何使Widget可點擊?
