在主面板中渲染条件面板时在 R shiny 中,单击操作按钮时如何更改视图?

In R shiny when rendering conditional panels in main panel, how to change view when action button is clicked?

在下面的几乎 MWE 代码中 运行,当侧边栏第二个矩阵输入面板被隐藏并且用户通过单击“费率”查看主面板中的 table 值时" 操作按钮,我希望随后单击侧边栏面板中的“显示”操作按钮,除了显示第二个矩阵输入网格的主要作用外,还会触发主面板中“绘图”的重新渲染(单选按钮自动切换到“绘图”)。另请参阅下图。

单击“显示”actionButton 时如何合并此“转到”逻辑?我已经在条件面板中用 if/else 语句胡闹了,但还没有运气。

我删除了几个折磨人的 interpolation/input 验证函数(matrixValidatevectorMultivectorMultiFinal)以使这个应用程序功能齐全,因为它们令人困惑并且与问题无关.但是,此已发布表单中的应用程序确实可以在主面板中呈现虚拟 plot/table 并说明手头的问题。

MWE 代码:

library(shiny)
library(shinyMatrix)
library(shinyjs)

matrix5Default <- function(){vectorBase(60,0.05)}

matrix4Input <- function(x){
  matrixInput(x, 
              value = matrix(c(0.05), 1, 1, dimnames = list(c("Base rate"),NULL)),
              label = "Pre-vector rates (Y variables):",
              rows = list(extend = FALSE,  names = TRUE),
              cols = list(extend = FALSE, names = FALSE, editableNames = FALSE),
              class = "numeric")}

matrix5Input <- function(x,y,z){
  matrixInput(x,
              value = matrix(c(y,z),1,2,dimnames=list(NULL,c("X","Y"))),
              rows = list(extend = TRUE,  names = FALSE),
              cols = list(extend = FALSE, names = TRUE, editableNames = FALSE),
              class = "numeric")}  

vectorBase <- function(x,y){
  a <- rep(y,x)
  b <- seq(1:x)
  c <- data.frame(x = b, y = a)
  return(c)}

ui <- 
  pageWithSidebar(
    headerPanel("Model"),
    sidebarPanel(uiOutput("Panels")),
    mainPanel(
      tabsetPanel(
        tabPanel("Rates", value=2,
                 fluidRow(
                   radioButtons(
                     inputId = 'Tab2',
                     label = h5(strong(helpText("Outputs:"))),
                     choices = c('Plot','Rates'), 
                     selected = 'Plot',
                     inline = TRUE)), 
                 conditionalPanel(condition = "input.Tab2 == 'Plot'",plotOutput("plot1")),
                 conditionalPanel(condition = "input.Tab2 == 'Rates'",tableOutput("table1")),
        ),  # close tab panel
        id = "tabselected"
      )
    ) 
  ) 

server <- function(input,output,session)({
  
  periods     <- reactive(input$periods)
  base        <- reactive(input$base)
  ratesInput  <- reactive(input$ratesInput)

  output$Panels <- renderUI({
    tagList( 
      conditionalPanel(
        useShinyjs(),
        condition="input.tabselected==2",
        matrix4Input("base"),
        uiOutput("ratesTotal"),
        helpText("Generate rates vectors:"),
        actionButton('showBtn','Show'),actionButton('hideBtn','Hide'),
        hidden(uiOutput("ratesVectors")),
      )) 
  }) 

  output$ratesVectors <- renderUI({matrix5Input("ratesInput",input$periods,input$base[1,1])})
  
  observeEvent(input$showBtn,{shinyjs::show("ratesVectors")})
  observeEvent(input$hideBtn,{shinyjs::hide("ratesVectors")})
  
  output$plot1 <-renderPlot({plot(matrix5Default())})
  output$table1 <- renderTable({matrix5Default()})
    
  })

shinyApp(ui, server)

尝试 updateRadioButtons(),如下所示。它对我有用。

  observeEvent(input$showBtn,{
    shinyjs::show("ratesVectors")
    updateRadioButtons(session, "Tab2", selected = "Plot")
  })