简体   繁体   中英

R shinydashboard not showing content conditioned on if and if…else statement

I have a shiny dashboard where I want to show the content based on selectInput("instrumenti") which has 4 options.

I manage to do that for my output "mymap" based on renderLeaflet with if, else if and else statments, but it does not work for Plotly output "Odnos ulaganja i stanovništva" based on renderPlotly.

Here is the code for UI and server on the mock data (iris data-set) which has the same problem as my original data: UI:


library(shiny)
library(shinydashboard)
library(plotly)
library(tidyverse)
library(leaflet)

Vrste<- c("setosa", "versicolor", "virginica")

ui <- dashboardPage(
  dashboardHeader(title = "mockapp"),
  dashboardSidebar(sidebarMenu(
                      menuItem("Geografski prikaz", tabName = "Geografski_prikaz", 
                               menuItem(selectInput("instrumenti", "Instrumenti",
                                                    choices = Vrste), tabName = "Submenu")))),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "Submenu",
              fluidRow(
                box(width=12, solidHeader = TRUE,
                    title = "Geografski prikaz ulaganja",
                    leafletOutput("mymap", height=550))),
              fluidRow(  
                box(title ="Odnos ulaganja i stanovništva", solidHeader = TRUE, plotlyOutput("scatter", width=4,height=220)),
                box(title ="Ulaganje po stanovniku", solidHeader = TRUE, plotlyOutput("bar", width=6,height=250)),
                tags$head(tags$style(HTML(' /* body */
                                .content-wrapper, .right-side {
                                background-color:   #FFFFFF;
                                }'
                )
                )
                ))))))

SERVER:

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

  output$scatter<-renderPlotly({
    if(input$instrumenti == "setosa"){
      plot_ly(iris,
              x = ~ Sepal.Length, 
              y = ~Sepal.Width) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
      
    } else if (input$instrumenti == "versicolor") {
      plot_ly(iris,
              x = ~ Sepal.Width, 
              y = ~Petal.Length) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
      
      
    } else {
      plot_ly(iris,
              x = ~ Petal.Length, 
              y = ~Petal.Width ) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
    }
  })
  
  
}

# Run the application 
shinyApp(ui = ui, server = server)

As you will see, you get the scatter-plot for the first option ( if(input$instrumenti == "setosa")), but if you change the choice in selectInput there is no graph anymore or any other option. Strangely, I don't get any error. Maybe someone can see an error. tnx

That is because you set your width = 4 which is too small. Try this

library(shiny)
library(shinydashboard)
library(plotly)
library(tidyverse)
library(leaflet)

Vrste<- c("setosa", "versicolor", "virginica")

ui <- dashboardPage(
  dashboardHeader(title = "mockapp"),
  dashboardSidebar(sidebarMenu(
    menuItem("Geografski prikaz", tabName = "Geografski_prikaz", 
             menuItem(selectInput("instrumenti", "Instrumenti",
                                  choices = Vrste), tabName = "Submenu")))),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "Submenu",
              # fluidRow(
              #   box(width=12, solidHeader = TRUE,
              #       title = "Geografski prikaz ulaganja",
              #       leafletOutput("mymap", height=550))),
              fluidRow(  
                box(title ="Odnos ulaganja i stanovništva", solidHeader = TRUE, plotlyOutput("scatter", width=300,height=220)),
                box(title ="Ulaganje po stanovniku", solidHeader = TRUE, plotlyOutput("bar", width=300,height=250)),
                tags$head(tags$style(HTML(' /* body */
                                .content-wrapper, .right-side {
                                background-color:   #FFFFFF;
                                }'
                )
                )
                ))))))


server <- function(input, output, session) {
  observe({print(input$instrumenti)})
  output$scatter<-renderPlotly({
    if(input$instrumenti == "setosa"){
      p <- plot_ly(iris,
              x = ~ Sepal.Length, 
              y = ~Sepal.Width) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
      
    } else if (input$instrumenti == "versicolor") {
      p <- plot_ly(iris,
              x = ~ Sepal.Width, 
              y = ~Petal.Length) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
      
      
    } else {
      p <- plot_ly(iris,
              x = ~ Petal.Length, 
              y = ~Petal.Width ) %>% 
        add_trace(type = 'scatter',  mode = 'markers', opacity = 0.7)
    }
    p
  })
  
}


shinyApp(ui = ui , server = server)

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM