问题
I have a shiny app which displays 3 tabs.
In the Documents tab there is a table. When the user clicks on the setosa of the 1st row he is moving to the View tab (visible only when click happens) and sees a table. When the user clicks on the setosa of the 2nd row he is moving to the View tab and sees another table. Also those tables must be visible only on View tab.
The app seems to work fine but if I click on the 1st row setosa ,move to the View tab, return to the Documents tab again and try to click again the 1st row setosa nothing happens. I have to click the 2nd row setosa to make it work again. The same happens with the 2nd row setosa.
In a few words I cannot do the same action twice in a row and I feel that my if condition is responsible for this.
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(shinydashboardPlus)
library(DT)
library(shinyjs)
shinyApp(
ui = dashboardPagePlus(
header = dashboardHeaderPlus(title = span(strong("ArmorDoc"),style = "color: black;")
),
sidebar = dashboardSidebar(
),
body = dashboardBody(
useShinyjs(),
tags$hr(),
tabsetPanel(
id ="tabA",
type = "tabs",
tabPanel("Documents",icon = icon("accusoft"),
tags$hr(),
DTOutput("dt1")),
tabPanel("View", icon = icon("table"),
DTOutput("dt3")
),
tabPanel("Upload", icon = icon("table")
)
)
)),
server = function(input, output,session) {
observeEvent(input$tabA, {
if(input$tabA == "Documents"|input$tabA=="Upload"){
hideTab("tabA", "View")
}
})
observeEvent(input$dt1_cell_clicked, {
# alternative: input$dt1_cells_selected
if (req(input$dt1_cell_clicked$value) == "setosa") {
showTab("tabA", "View")
updateTabsetPanel(session, inputId = "tabA", selected = "View")
}
})
output$dt1 <- DT::renderDataTable({
DT::datatable(
iris[1:2,],
filter = "top",
options = list(searchHighlight = TRUE, search = list(search = ""),pageLength = 5,columnDefs = list(list(className = 'dt-left', targets = "_all"))),rownames= FALSE,
selection = list(mode = 'single', target = 'cell')
)
})
output$dt3 <- renderDT(server = F,
if(input$dt1_cell_clicked$row == 1&input$tabA=="View"){
datatable(iris)
}
else if(input$dt1_cell_clicked$row == 2&input$tabA=="View"){
datatable(mtcars)
}
else{
return(NULL)
}
)
}
)
回答1:
Answer can be found here. Its the same Proxy method in the observeEvent().
来源:https://stackoverflow.com/questions/62012913/if-statement-does-not-work-2-times-in-a-row-in-a-shiny-app