这是我第一个尝试合并 Javascript 的闪亮应用程序,所以我知道我可能在这里遗漏了一些细微差别。我有一个应用程序,我试图让用户添加行,然后表格转到最后一页,在该页面上添加了带有操作按钮的行。这两个操作在单独的 observeEvents 中独立工作,但是当我尝试将它们放在同一个 observeEvent 块中时,session$sendCustomMessage 不会通过。该行已添加,但视觉对象并未转到表格的最后一页。代码如下:
ui<- fluidPage(theme = shinytheme("spacelab"), navbarPage( "App", tabPanel("Search",
mainPanel(
fluidRow(
column(3, selectInput("search_type_filter", label = "Type", choices = c("All", "Derm", "Onc"), selected = "All")),
column(3, selectInput("search_active_filter", label = "Active", choices = c("All", "No", "Yes"), selected = "All"))
),
DT::dataTableOutput("Table")
tags$script(HTML("Shiny.addCustomMessageHandler('LastPage', function(message) {
var target = $('#Table .dataTable');
target.DataTable().page('last').draw(false);
});")),
actionButton("updateButton", "Update", class = "btn btn-primary"),
actionButton("addRowButton", "Add Hashtag"),
actionButton("resetButton", "Reset")
)
)))
server <- function(input, output, session) {
HTData <- reactiveValues(data = getDataFromLake(datalake_path, filename, endp))
# Define reactive filtered data object
fData <- reactive({
data <- HTData$data
if (input$search_type_filter != "All") {
data <- data %>% filter(Type == input$search_type_filter)
}
if (input$search_active_filter != "All") {
data <- data %>% filter(Active == input$search_active_filter)
}
return(data)
})
#Render Table
output$Table <- DT::renderDataTable({
datatable(
fData(),
editable = TRUE
)
}, server = TRUE)
# Define function to add a new row to the data frame
add_row <- function(data) {
new_row <- c("", "", "", "") # Create a vector with empty values for each column
names(new_row) <- names(data) # Set the column names of the new row to match the existing data frame
rbind(data, new_row) # Concatenate the new row to the existing data frame and return the result
}
# Both in same block
observeEvent(input$addRowButton, {
HTData$data <- add_row(HTData$data) # Update the reactiveValues
session$sendCustomMessage('LastPage', 'placeholder')
})
# Execute in separate blocks
# observeEvent(input$addRowButton, {
# HTData$data <- add_row(HTData$data) # Update the reactiveValues
# })
#
# observeEvent(input$addRowButton, {
# HTData$data <- add_row(HTData$data) # Update the reactiveValues
# session$sendCustomMessage('LastPage', 'placeholder')
# })
我已经尝试延迟 session$sendCustomMessage 但我没有使用 shinyjs::delay() 函数得到正确的反应,这是另一个问题。我认为延迟是让他们在同一个 observeEvent 块中工作但无法配置适当解决方案的答案。 session$sendCustomMessage 的基础来自此页面/解决方案,但我的应用程序配置不同。 R Shiny DT 通过操作按钮导航到表格的最后一页