[英]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.我有一个闪亮的仪表板,我想在其中显示基于 selectInput("instruenti") 的内容,它有 4 个选项。
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.我设法为基于 renderLeaflet 的输出“mymap”和 if、else if 和 else 语句做到这一点,但它不适用于基于 renderPlotly 的 Plotly 输出“Odnos ulaganja i stanovništva”。
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:这是模拟数据(iris 数据集)上的 UI 和服务器的代码,它与我的原始数据存在相同的问题: 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.正如您将看到的,您将获得第一个选项的散点图(if(input$instrumenti == "setosa")),但如果您更改 selectInput 中的选择,则不再有图形或任何其他选项。 Strangely, I don't get any error.
奇怪的是,我没有收到任何错误。 Maybe someone can see an error.
也许有人可以看到错误。 tnx
tnx
That is because you set your width = 4
which is too small.那是因为您设置的
width = 4
太小了。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.