Question: Дефолтная tabPanel + Кнопки
0
gravatar for sabeloprosov
6 months ago by
sabeloprosov150
sabeloprosov150 wrote:

Добрый день!

Делаю приложение из двух вкладок. На первой 4 кнопки. Каждая из них по нажатии ведёт на вторую вкладку. Как только у меня получилось сделать так, что каждая кнопка работает как надо, столкнулся с тем, что по запуске приложения открывается по дефолту не первая вкладка, а вторая (хотя дефолтно в tabsetPanel указана первая). Полагаю, что проблема может быть в том, что кнопки автоматически прокликиваются при запуске. Как сделать так, чтобы и кнопки работали и вкладка открывалась первая, при том сама не переключалась до нажатия на кнопку.

Вот код:

library(shiny) library(ggplot2) library(stringr) library(dplyr) library(DT) library(tools) library(shinythemes)

ui <- navbarPage("Destination of your dream",tabsetPanel(id="tabs", tabPanel("Why?",fluidPage( titlePanel("What is the purpose of your journey? Click one of the buttons below!"), br(),

fluidRow(column(h3("Travel"), br(),actionButton(inputId = "but1", label = "I'm gonna take a nice trip to relax!",style='padding:100px; font-size:120%'),width=5, fluidRow(column(h3("Living"), br(),actionButton(inputId = "but2", label = "I'm gonna start a brand new chapter in my life!",style='padding:100px; font-size:120%'),width=10))), column(h3("Job"), br(),actionButton(inputId = "but3", label = "I'm gonna work hard to prove my dignity!",style='padding:100px; font-size:120%'),width=5, fluidRow(column(h3("Education"), br(),actionButton(inputId = "but4", label = "I'm gonna do my best discovering new things!",style='padding:100px; font-size:120%'),width=10))))),value = "first"),

tabPanel ("The Next Destination is...", fluidPage( theme=shinytheme("superhero"),

titlePanel("The coolest web application ever made by any creature"),

sidebarLayout(

sidebarPanel(

 wellPanel(

   h3("Education"),      


   selectInput(inputId = "educ", 
               label = "Choose your education level:",
               choices = c("High School" = "High School", 
                           "Bachelor" = "Bachelor's Degree", 
                           "Master" = "Master's Degree", 
                           "PhD" = "PhD" 
                          ), 
               selected = "High School"), 

   h3("Politics"),
   selectInput(inputId = "pol", 
               label = "Choose your political views:",
               choices = c("Centrist politics" = "Centrist politics", 
                           "Progressive politics" = "Progressive politics", 
                           "Conservative politics" = "Conservative politics" 
                        ), 
               selected = "Centrist politics"),  


   h3("Drinks"),
   selectInput(inputId = "drink", 
               label = "What's your favourite drink?",
               choices = c("Coffee" = "Coffee", 
                           "Tea" = "Tea", 
                           "Wine" = "Wine", 
                           "Beer"="Beer",
                           "Cocktails"="Cocktails"
               ), 
               selected = "Wine"),  



   h3("Music"),
   selectInput(inputId = "mus", 
               label = "What's your favoutite music?",
               choices = c("Acoustic" = "Acoustic music", 
                           "Electronic" = "Electronic music" 
               ), 
               selected = "Electronic music"),  


   h3("Special Meal Preferences"),
   radioButtons(inputId = "spec", 
               label = "Have you got any special eating habits?",
               choices = c("Yes" = "Yes", 
                           "No" = "No"

               ),selected = "No"), 

   conditionalPanel(condition = "input.spec=='Yes'",h3("Specify your eating habits",h5(selectInput(inputId = "veg",
                                                                                                label = "I am:",

                                                                                                choices = c("Vegan" = "Vegan", 
                                                                                                            "Vegeterian" = "Vegetarian", 
                                                                                                            "Pesceterian" = "Pesceterian",
                                                                                                            "Paleo"="Paleo",
                                                                                                            "Eating Fish"="Eating fish",
                                                                                                            "Eating Meat"="Eating meat"
                                                                                                ))))),

h3("Cuisine"), selectInput(inputId = "cui", label = "What's your favourite cuisine?", choices = c("Mexican" = "Mexican food", "Korean" = "Korean food", "Chinese" = "Chinese food", "Italian" = "Italian food", "Japanese" = "Japanese food"

           ), 
           selected = "Italian food"))),


  mainPanel(

  textOutput(outputId = "educc"),textOutput(outputId = "poll"), textOutput(outputId = "cuis"), textOutput(outputId = "vegi")
  , textOutput(outputId = "musi"), textOutput(outputId = "drinks")
 )

)),value = "second"),selected = "first"))

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

values=reactiveValues(one=0,two=0,three=0,four=0)

observeEvent( input$but1 ,{values$one=1 values$two=0 values$three=0 values$four=0})

observeEvent((values$one),{updateTabsetPanel(session = session, inputId = "tabs", selected = "second")})

observeEvent( input$but2 ,{values$one=0 values$two=1 values$three=0 values$four=0})

observeEvent((values$two),{updateTabsetPanel(session = session, inputId = "tabs", selected = "second")})

observeEvent( input$but3 ,{values$one=0 values$two=0 values$three=1 values$four=0})

observeEvent((values$three),{updateTabsetPanel(session = session, inputId = "tabs", selected = "second")})

observeEvent( input$but4 ,{values$one=0 values$two=0 values$three=0 values$four=1})

observeEvent((values$four),{updateTabsetPanel(session = session, inputId = "tabs", selected = "second")})

output$vegi= renderText({paste(ifelse(input$spec=="Yes",input$veg,""))})

output$educc <- renderText({ paste(input$educ) }

)

output$poll <- renderText({ paste(input$pol) })

output$drinks <- renderText({ paste(input$drink) })

output$musi <- renderText({ paste(input$mus) })

output$cuis <- renderText({ paste(input$cui) }

)

}

shinyApp(ui = ui, server = server)

shiny R rstudio • 131 views
ADD COMMENTlink modified 6 months ago • written 6 months ago by sabeloprosov150
0
gravatar for sabeloprosov
6 months ago by
sabeloprosov150
sabeloprosov150 wrote:

Вопрос решён!

Если кому вдруг понадобится аналогичная помощь:

observeEvent((values$three),{updateTabsetPanel(session = session, inputId = "tabs", selected = "second")},ignoreNULL = TRUE, ignoreInit = TRUE)

решается двумя последними аргументами

ADD COMMENTlink written 6 months ago by sabeloprosov150
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.03
Traffic: 1 users visited in the last hour