[英]change ggvis plot layer dynamically
I would like to change the layer of a ggvis
plot using a selectInput
widget using a dynamic interface. 我想使用带有动态界面的
selectInput
小部件来更改ggvis
图的图层。 The problem is that when I choose a different layer after creating the plot, it changes but it just disappear really quick. 问题在于,在创建图后选择其他图层时,它会发生变化,但消失得很快。 Below is a simplified version of the code to show the problem that omit all the extra dynamic content.
下面是代码的简化版本,以显示忽略所有额外动态内容的问题。 I just plot some number of values from a dataset.
我只是从数据集中绘制一些值。 I added a couple of
selectInput
widgets to let the user choose what type of plot and when to show the plot. 我添加了几个
selectInput
小部件,以使用户可以选择绘图的类型以及何时显示绘图。 Please note that I need to have all the elements inside of a renderUI
. 请注意,我需要将所有元素包含在
renderUI
。
library(shiny)
library(ggvis)
runApp(list(
ui = shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel( uiOutput("controls") ),
mainPanel( uiOutput("Plot_UI" )
)
)
)
),
server = function(input, output, session) {
dat <- reactive(iris[sample(nrow(iris),input$numbers),])
buildPlot <- function(layer = 'points'){
if (layer=='points'){
dat %>%
ggvis(~Sepal.Width, ~Sepal.Length) %>%
layer_points() %>%
bind_shiny("ggvis1")
} else {
dat %>%
ggvis(~Sepal.Width, ~Sepal.Length) %>%
layer_bars() %>%
bind_shiny("ggvis1")
}
}
output$controls <- renderUI({
div(
sliderInput("numbers", label = "Number of values to plot?", min = 1, max = 150, value = 75),
selectInput('plot_type', 'Plot Type', c("points","bars")),
selectInput("show", 'Show plot?', c('No','Yes'))
)
})
output$Plot_UI <- renderUI({
if (!is.null(input$show) && input$show == 'Yes'){
cat("Plot_UI -> Build plot\n")
buildPlot(input$plot_type)
div(
uiOutput("ggvis_ui"),
ggvisOutput("ggvis1")
)
}
})
}
))
The only way to see the plot again is by selecting to not show the plot and later select show the plot again using the "Show plot" selectInput
. 再次查看图的唯一方法是选择不显示图,然后使用“显示图”
selectInput
选择再次显示图。
I don't know if this is a bug or I'm doing it incorrectly. 我不知道这是否是错误或执行不正确。
I think the problem is that your trying to render and update the div at the same time. 我认为问题在于您尝试同时渲染和更新div。
library(shiny)
library(ggvis)
runApp(list(
ui = shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel( uiOutput("controls") ),
mainPanel( uiOutput("Plot_UI" )
)
)
)
),
server = function(input, output, session) {
dat <- reactive(iris[sample(nrow(iris),input$numbers),])
buildPlot <- function(layer = 'points'){
if (layer=='points'){
dat %>%
ggvis(~Sepal.Width, ~Sepal.Length) %>%
layer_points() %>%
bind_shiny("ggvis1")
} else {
dat %>%
ggvis(~Sepal.Width, ~Sepal.Length) %>%
layer_bars() %>%
bind_shiny("ggvis1")
}
}
output$controls <- renderUI({
div(
sliderInput("numbers", label = "Number of values to plot?", min = 1, max = 150, value = 75),
selectInput('plot_type', 'Plot Type', c("points","bars")),
selectInput("show", 'Show plot?', c('No','Yes'))
)
})
observeEvent(input$show,{
if (!is.null(input$show) && input$show == 'Yes'){
output$Plot_UI <- renderUI({
cat("Plot_UI -> Build plot\n")
div(
uiOutput("ggvis_ui"),
ggvisOutput("ggvis1")
)
})
}
if (!is.null(input$show) && input$show == 'No'){
output$Plot_UI <- renderUI({ div() })
}
})
observe({
if (!is.null(input$show) && input$show == 'Yes'){
invalidateLater(100,session)
renderPlot()
}
})
renderPlot <- function(){
if(is.null(input$plot_type)) return(NULL)
buildPlot(input$plot_type)
}
} #
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.