[英]Shiny in R: How to properly use reactive, observe and renderUI?
I have a problem with my code. 我的代码有问题。 Every time I click a button my plot (built with ggvis) is showing up but vanishes immediately.
每当我点击一个按钮时,我的情节(用ggvis构建)就会显示出来,但会立即消失。 Since my code is very long, the following code reproduces my problem.
由于我的代码很长,下面的代码重现了我的问题。 I want to reuse the reactive data frame test0 in my render function and I guess this is exactly what causes my problem.
我想在我的渲染函数中重用反应数据帧test0,我想这正是导致我的问题的原因。 But this is essential to me.
但这对我来说至关重要。 The three steps (reactive, observe, render) are the same than in my code.
三个步骤(反应,观察,渲染)与我的代码相同。 I would very much appreciate your help!
我非常感谢你的帮助!
server.R server.R
library(shiny)
library(ggvis)
library(dplyr)
data(mtcars)
shinyServer(function(input, output) {
test0 <- reactive({
df <- mtcars %>% select(mpg, wt)
(input$NextCounter + 1)*df
})
observe({
df <- test0()
if (!is.null(df)) {
ggvis(df, x = ~wt, y = ~mpg) %>% bind_shiny("plotggvis")
}
})
output$test1 <- renderUI({
df <- test0()
ggvisOutput("plotggvis")
})
})
ui.R ui.R
library(shiny) 库(闪亮)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("NextCounter", "Next")
),
mainPanel(
uiOutput("test1")
)
)
))
this one working for me 这个适合我
library(shiny)
library(ggvis)
library(dplyr)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("NextCounter", "Next")
),
mainPanel(
ggvisOutput("plotggvis")
)
)
))
server <- shinyServer(function(input, output) {
data(mtcars)
test0 <- reactive({
df <- mtcars %>% select(mpg, wt)
(input$NextCounter + 1)*df
})
my_graph <- reactive({
df <- test0()
ggvis(df, x = ~wt, y = ~mpg)
})
my_graph %>% bind_shiny("plotggvis")
})
})
shinyApp(ui = ui, server = server)
You don't need to have a ggvisOutput
in the UI to solve your problem. 您不需要在UI中使用
ggvisOutput
来解决您的问题。 Actually the problem in your code is having the bind_shiny
function inside an observer that will be executed again every time your test0
data changes. 实际上,代码中的问题是在观察者中有
bind_shiny
函数,每次test0
数据发生变化时都会再次执行。 It is expected to bind your ggvis only once, otherwise it will have that behavior of showing up and vanishes immediately. 预计只会绑定你的ggvis一次,否则它会出现并立即消失的行为。 Also, one great feature of ggvis is having a nice transitions when data is changing, so you don't need to create a ggvis object every time your data changes, just make sure that you only bind that ggvis object once in your UI.
此外,ggvis的一个重要特性是在数据更改时有一个很好的转换,因此每次数据更改时都不需要创建ggvis对象,只需确保在UI中只绑定一次ggvis对象。
Below is a modified version of your code to solve your problem and show the animated transition of data. 下面是代码的修改版本,用于解决您的问题并显示数据的动画过渡。
library(shiny)
library(ggvis)
library(dplyr)
data(mtcars)
ui <- fluidPage(fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("NextCounter", "Next")
),
mainPanel(
uiOutput("test1")
)
)
))
server <- function(input, output) {
test0 <- reactive({
input$NextCounter
df <- mtcars %>% select(mpg, wt)
df[sample(nrow(df),nrow(df)/2), ]
})
output$test1 <- renderUI({
# bind the ggvis only once
test0 %>% ggvis(x = ~wt, y = ~mpg) %>% bind_shiny("plotggvis")
ggvisOutput("plotggvis")
})
}
shinyApp(ui, server)
You can also modify some ggvis parameters using input widgets by putting the ggvis
inside of a reactive expression. 您还可以通过将
ggvis
放在反应式表达式中来使用输入小部件修改一些ggvis参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.