执行计算和更改选项卡 [Shiny app]
Perform a calculation and change tab [Shiny app]
我需要执行计算(定义它的参数在边栏中),完成后,移动到绘图以查看最终结果 (tab2
) 而不是 tab1
。所以,我希望“预测”(操作)按钮按照顺序移动到第二个选项卡。
我试过类似的东西,但 Shiny 的世界对我来说很新鲜:
ui <- bootstrapPage(
fluidPage(
sidebarLayout(sidebarPanel(
uiOutput('manySliders'),
actionButton('add', 'Predict'),
br(), br(),
helpText('Press Quit to exit the application'),
actionButton('quit', 'Quit')),
mainPanel(tabsetPanel(id = 'tabs',
tabPanel('tab1', uiOutput('markdown')),
tabPanel('tab2', plotOutput('plot'))
)
)
))
server <- function(input, output, session){
...
new.d <- reactive({
input$add
isolate(observeEvent(input$add, {updateTabsetPanel(session, "tab1", selected = 'tab2')}))
...
}
请不要将 observeEvent
嵌套在 reactive
中。您可以使用 bindEvent
或 eventReactive
。此外 updateTabsetPanel
需要更新元素的 inputId
(不是当前活动的标签名):
library(shiny)
ui <- bootstrapPage(
fluidPage(
sidebarLayout(sidebarPanel(
uiOutput('manySliders'),
actionButton('add', 'Predict'),
br(), br(),
helpText('Press Quit to exit the application'),
actionButton('quit', 'Quit')),
mainPanel(tabsetPanel(id = 'tabs',
tabPanel('tab1', uiOutput('markdown')),
tabPanel('tab2', plotOutput('plot'))
)
)
)
)
)
server <- function(input, output, session){
observeEvent(input$add, {
updateTabsetPanel(session, inputId = "tabs", selected = 'tab2')
})
result <- eventReactive(input$add, {
return(1:10)
})
output$plot <- renderPlot({
plot(result())
})
}
shinyApp(ui, server)
我需要执行计算(定义它的参数在边栏中),完成后,移动到绘图以查看最终结果 (tab2
) 而不是 tab1
。所以,我希望“预测”(操作)按钮按照顺序移动到第二个选项卡。
我试过类似的东西,但 Shiny 的世界对我来说很新鲜:
ui <- bootstrapPage(
fluidPage(
sidebarLayout(sidebarPanel(
uiOutput('manySliders'),
actionButton('add', 'Predict'),
br(), br(),
helpText('Press Quit to exit the application'),
actionButton('quit', 'Quit')),
mainPanel(tabsetPanel(id = 'tabs',
tabPanel('tab1', uiOutput('markdown')),
tabPanel('tab2', plotOutput('plot'))
)
)
))
server <- function(input, output, session){
...
new.d <- reactive({
input$add
isolate(observeEvent(input$add, {updateTabsetPanel(session, "tab1", selected = 'tab2')}))
...
}
请不要将 observeEvent
嵌套在 reactive
中。您可以使用 bindEvent
或 eventReactive
。此外 updateTabsetPanel
需要更新元素的 inputId
(不是当前活动的标签名):
library(shiny)
ui <- bootstrapPage(
fluidPage(
sidebarLayout(sidebarPanel(
uiOutput('manySliders'),
actionButton('add', 'Predict'),
br(), br(),
helpText('Press Quit to exit the application'),
actionButton('quit', 'Quit')),
mainPanel(tabsetPanel(id = 'tabs',
tabPanel('tab1', uiOutput('markdown')),
tabPanel('tab2', plotOutput('plot'))
)
)
)
)
)
server <- function(input, output, session){
observeEvent(input$add, {
updateTabsetPanel(session, inputId = "tabs", selected = 'tab2')
})
result <- eventReactive(input$add, {
return(1:10)
})
output$plot <- renderPlot({
plot(result())
})
}
shinyApp(ui, server)