当我尝试引入动态 selectInput 字段时,Shiny R 中的 gvisBubbleChart Plot 停止显示
My gvisBubbleChart Plot in Shiny R stops displaying when I attempt to introduce a dynamic selectInput field
我在开发应用程序时遇到了这个问题,并在此处使用 Fruits df 以简化的脚本形式重现了它。
基本上,我有 select 输入框到 select 一年,这是 Fruits 中的一列。我创建了唯一的年份列表,并将其输入 select 输入框。
然后,理想情况下,我希望我的绘图仅显示我 select 编辑的那一年的记录。但是,正如您将在我的代码中看到的那样 - 第二个您取消注释 3 行来完成它,- 即使似乎没有任何错误,绘图也会停止显示。有谁知道这是为什么?提前致谢!!!
相关问题 - 在调试时我看到输入 $explore_year 首先是 "Null"。我正在尝试在代码中处理此问题,但不确定为什么 selected="2010" 不会自动处理它。
library(shiny)
library(googleVis)
library(DT)
listOfFruits <- sort(unique(Fruits$Year), decreasing = FALSE)
ui <- fluidPage(title = "Fruits Bug Recreated",
fluidRow(
column(3,
wellPanel(
uiOutput("choose_year"),
br()
)),
column(9,
tags$hr(),
htmlOutput("view")
)),
fluidRow(DT::dataTableOutput("tableExplore"))
)
server <- function(input, output) {
output$view <- renderGvis({
#Uncomment these 3 lines to see how the plot stops displaying.
# local_exloreYear <- input$explore_year
# if (is.null(local_exloreYear)) {local_exloreYear <- "2010"}
# FruitsSubset <- subset(Fruits, Year == local_exloreYear)
#------------I wanted to use the commented line below instead of the
#that follows
#gvisBubbleChart(FruitsSubset, idvar="Fruit",
#-------------
gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Year", sizevar="Profit",
options=list(
hAxis='{minValue:70, maxValue:125, title:"Sales"}',sortBubblesBySize=TRUE,
vAxis='{title: "Expenses",minValue:60, maxValue:95}'
))
})
# Drop-down selection box for dynamic choice of minutes in the plans to compare
output$choose_year <- renderUI({
selectInput("explore_year", "Select Year", as.list(listOfFruits),selected ="2010")
})
output$tableExplore <- DT::renderDataTable(DT::datatable({
FruitsSubset <- subset(Fruits, Fruits$Year == input$explore_year)
myTable <-FruitsSubset[,c(1,2,3,4,5,6)]
data <- myTable
data
},options = list(searching = FALSE,paging = FALSE)
))
}
shinyApp(ui = ui, server = server)
就像我在评论中写的那样,您可以通过以输入为非 NULL 为条件进行渲染来解决它。
output$view <- renderGvis({
if(!is.null(input$explore_year)){
...
}
})
尽管如此,我不认为您真的有意这样做,因为在其他渲染函数中它不是必需的,例如在 DT::renderDataTable()
中,您也可以在其中使用相同的输入(最初为 NULL)。
因此,我建议将其报告为错误。
我在开发应用程序时遇到了这个问题,并在此处使用 Fruits df 以简化的脚本形式重现了它。 基本上,我有 select 输入框到 select 一年,这是 Fruits 中的一列。我创建了唯一的年份列表,并将其输入 select 输入框。 然后,理想情况下,我希望我的绘图仅显示我 select 编辑的那一年的记录。但是,正如您将在我的代码中看到的那样 - 第二个您取消注释 3 行来完成它,- 即使似乎没有任何错误,绘图也会停止显示。有谁知道这是为什么?提前致谢!!!
相关问题 - 在调试时我看到输入 $explore_year 首先是 "Null"。我正在尝试在代码中处理此问题,但不确定为什么 selected="2010" 不会自动处理它。
library(shiny)
library(googleVis)
library(DT)
listOfFruits <- sort(unique(Fruits$Year), decreasing = FALSE)
ui <- fluidPage(title = "Fruits Bug Recreated",
fluidRow(
column(3,
wellPanel(
uiOutput("choose_year"),
br()
)),
column(9,
tags$hr(),
htmlOutput("view")
)),
fluidRow(DT::dataTableOutput("tableExplore"))
)
server <- function(input, output) {
output$view <- renderGvis({
#Uncomment these 3 lines to see how the plot stops displaying.
# local_exloreYear <- input$explore_year
# if (is.null(local_exloreYear)) {local_exloreYear <- "2010"}
# FruitsSubset <- subset(Fruits, Year == local_exloreYear)
#------------I wanted to use the commented line below instead of the
#that follows
#gvisBubbleChart(FruitsSubset, idvar="Fruit",
#-------------
gvisBubbleChart(Fruits, idvar="Fruit",
xvar="Sales", yvar="Expenses",
colorvar="Year", sizevar="Profit",
options=list(
hAxis='{minValue:70, maxValue:125, title:"Sales"}',sortBubblesBySize=TRUE,
vAxis='{title: "Expenses",minValue:60, maxValue:95}'
))
})
# Drop-down selection box for dynamic choice of minutes in the plans to compare
output$choose_year <- renderUI({
selectInput("explore_year", "Select Year", as.list(listOfFruits),selected ="2010")
})
output$tableExplore <- DT::renderDataTable(DT::datatable({
FruitsSubset <- subset(Fruits, Fruits$Year == input$explore_year)
myTable <-FruitsSubset[,c(1,2,3,4,5,6)]
data <- myTable
data
},options = list(searching = FALSE,paging = FALSE)
))
}
shinyApp(ui = ui, server = server)
就像我在评论中写的那样,您可以通过以输入为非 NULL 为条件进行渲染来解决它。
output$view <- renderGvis({
if(!is.null(input$explore_year)){
...
}
})
尽管如此,我不认为您真的有意这样做,因为在其他渲染函数中它不是必需的,例如在 DT::renderDataTable()
中,您也可以在其中使用相同的输入(最初为 NULL)。
因此,我建议将其报告为错误。