R shinydashboard 不显示以 if 和 if…else 语句为条件的内容
R shinydashboard not showing content conditioned on if and if…else statement
我有一个闪亮的仪表板,我想在其中显示基于 selectInput("instrumenti") 的内容,它有 4 个选项。
我设法为基于 renderLeaflet 的输出“mymap”和 if、else if 和 else 语句做到了这一点,但它不适用于基于 renderPlotly 的 Plotly 输出“Odnos ulaganja i stanovništva”。
这是模拟数据(虹膜数据集)上的 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 <- 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)
如您所见,您会得到第一个选项的散点图(if(input$instrumenti == "setosa")),但是如果您更改 selectInput 中的选择,则不再有图表或任何其他图表选项。
奇怪的是,我没有收到任何错误。
也许有人可以看到错误。
发送
那是因为你设置的width = 4
太小了。试试这个
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)
我有一个闪亮的仪表板,我想在其中显示基于 selectInput("instrumenti") 的内容,它有 4 个选项。
我设法为基于 renderLeaflet 的输出“mymap”和 if、else if 和 else 语句做到了这一点,但它不适用于基于 renderPlotly 的 Plotly 输出“Odnos ulaganja i stanovništva”。
这是模拟数据(虹膜数据集)上的 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 <- 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)
如您所见,您会得到第一个选项的散点图(if(input$instrumenti == "setosa")),但是如果您更改 selectInput 中的选择,则不再有图表或任何其他图表选项。 奇怪的是,我没有收到任何错误。 也许有人可以看到错误。 发送
那是因为你设置的width = 4
太小了。试试这个
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)