updateTabsetPanel无法正常工作

时间:2017-05-22 14:49:09

标签: r shiny

有谁知道为什么这个简单的代码不起作用? 我要做的是:只要用户点击运行按钮(structure),就会激活input$runButton标签。单击运行按钮时,input$runButton的值会更新,但标签不会更改为structure

这是一个简单的可重复示例:

server.R

function(input, output, session) {

  #RUN button
  observeEvent(input$runButton, {
    updateTabsetPanel(session, "allResults", 'structure')
  })

  #VAR SELECTION
  output$inputVars <- renderText({
    if (input$runButton == 0)
      return()
    print("Vars Selected")

  })

  #STRUCTURE RESULT
  output$structure <- renderText({
    if (input$runButton == 0)
      return()
    print("Structure Results")

  })


}

ui.R

fluidPage(

  titlePanel("Periscope Structure"),

  br(),

  sidebarPanel(
    fileInput(inputId="inFile", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),

    checkboxInput("header", "Header", TRUE),
    numericInput("level", "Structure Level", 3, min = 2, max = 10),
    br(),
    actionButton("runButton", strong("Run!"))

  ),

  mainPanel(
      tabsetPanel(id = "allResults",
        tabPanel('Variable Selection', textOutput('inputVars')),
        tabPanel('Structure Result', textOutput('structure')))
      )
)

谢谢!

2 个答案:

答案 0 :(得分:1)

请注意,您需要将value分配给TabPanel,以便您可以使用updateTabsetPanel调用将其设为有效,请尝试以下操作:

require(shiny)

ui <- fluidPage(

  titlePanel("Periscope Structure"),

  br(),

  sidebarPanel(
    fileInput(inputId="inFile", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    ),

    checkboxInput("header", "Header", TRUE),
    numericInput("level", "Structure Level", 3, min = 2, max = 10),
    br(),
    actionButton("runButton", strong("Run!"))

  ),

  mainPanel(
    tabsetPanel(id = "allResults",
                tabPanel(value  = "inputVars",'Variable Selection', textOutput('inputVars')),
                tabPanel(value  = "structure",'Structure Result', textOutput('structure')))
  )
)

server <- function(input, output, session) {

  #RUN button
  observeEvent(input$runButton, {
    updateTabsetPanel(session, "allResults", 'structure')
  })

  #VAR SELECTION
  output$inputVars <- renderText({
    if (input$runButton == 0)
      return()
    print("Vars Selected")

  })

  #STRUCTURE RESULT
  output$structure <- renderText({
    if (input$runButton == 0)
      return()
    print("Structure Results")

  })


}

runApp(shinyApp(ui, server), launch.browser = TRUE)

答案 1 :(得分:0)

请注意,如果您使用闪亮模块,则必须引用正确的会话。例如,如果单个选项卡是模块,则该选项卡的会话将无法切换到另一个选项卡

要解决此问题,您实际上可以将所有选项卡的“父”(容器)会话传递到选项卡模块的构造函数中,然后使用

示例的粗略草图

shinyUI(function(request) {
    source('page/search.R', local = T)
    source('page/app.R', local = T)
    fluidPage(
        tabsetPanel(id = 'inTabset',
            tabPanel(id = 'search', 'Search', searchUI('search'), value = 'search'),
            tabPanel(id = 'app', 'App', appUI('app'), value = 'app')
        )
    )
})

shinyServer(function(input, output, session) {
    source('page/search.R', local = T)
    source('page/app.R', local = T)
    searchSession = callModule(searchServer, 'search')
    callModule(appServer, 'app', session, searchSession)
})

闪亮的模块

appUI = function(id) {
    ns = NS(id)
    tagList(
        actionButton(ns('sendToHeatmap'), 'Send ortholog groups to heatmap')
    )
}
appServer = function(input, output, session, parentSession, searchSession) {
    # listen to a button press and switch to tab
    observeEvent(input$sendToSearch, {
        updateTextInput(searchSession, 'searchBox', 'funsearchterm')
        updateTabsetPanel(parentSession, 'inTabset', selected = 'search')
    })
}