在下面的 MWE 代碼中,input2用戶可以通過單擊輸入 2 的“顯示”單選按鈕來選擇性地呼叫該物件。默認設定是隱藏input2。但是,當第一次呼叫該應用程式時,input2在被隱藏之前快速閃爍observeEvent。
這種閃爍在代碼的非 MWE 版本中更為明顯。
有一個相關的帖子在 R 閃亮中,如何在不使用 renderUI 的情況下首次呼叫應用程式時消除側邊欄中所有條件面板的閃爍?解決了這個問題conditionalPanel。但是這里沒有conditionalPanel。
我不想renderUI用來解決這個問題!!由于renderUI有缺點,我不想重新介紹。
MWE代碼:
library(shiny)
library(shinyjs)
f <- function(action,i){as.character(checkboxInput(paste0(action,i),label=NULL))}
actions <- c("show", "reset")
tbl <- t(outer(actions, c(1,2), FUN = Vectorize(f)))
colnames(tbl) <- c("Show", "Reset")
rownames(tbl) <- c("Input 2", "Input 3")
ui <- fluidPage(
useShinyjs(),
tags$head(
tags$style(HTML(
"td .checkbox {margin-top: 0; margin-bottom: 0;}
td .form-group {margin-bottom: 0;}"
))
),
br(),
sidebarLayout(
sidebarPanel(
numericInput("input1", "Input 1:", 10, min = 1, max = 100),
h5(strong("Add inputs:")),
tableOutput("checkboxes"),
numericInput("input2", "Input 2:", 10, min = 1, max = 100),
),
mainPanel()
)
)
server <- function(input, output, session){
output[["checkboxes"]] <-
renderTable({tbl},
rownames = TRUE, align = "c",
sanitize.text.function = function(x) x
)
observeEvent(input[["show1"]], {
if(input[["show1"]] %% 2 == 1){shinyjs::show(id = "input2")} else
{shinyjs::hide(id = "input2")}
})
}
shinyApp(ui, server)
uj5u.com熱心網友回復:
在事件回圈中需要一些時間,直到observerEvent第一次被呼叫。默認情況下,它將顯示在最開始的位置。這導致閃光。只需隱藏input2在server函式的最開始:
server <- function(input, output, session) {
# Avoid flashing
shinyjs::hide(id = "input2")
output[["checkboxes"]] <-
renderTable(
{
tbl
},
rownames = TRUE,
align = "c",
sanitize.text.function = function(x) x
)
observeEvent(input[["show1"]], {
if (input[["show1"]] %% 2 == 1) {
shinyjs::show(id = "input2")
} else {
shinyjs::hide(id = "input2")
}
})
}
uj5u.com熱心網友回復:
你也可以使用 hidden
hidden(numericInput("input2", "Input 2:", 10, min = 1, max = 100))
和toggle:
observeEvent(input[["show1"]], {
toggle("input2")
},ignoreNULL = FALSE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/356508.html
上一篇:如何將帶有串列串列的行的表轉換為具有更多行和更多列的data.frame?
下一篇:如何按照名稱模式改變多列?
