导航栏页面和 ggvis plot Shiny 的错误更新
navbar Page and wrong update of ggvis plot Shiny
您好
我刚刚创建了 Shiny 应用程序。但是当我想以 navbarPage
方式展示我的 Shiny 应用程序时出现了一个小问题。我的意思是,当我添加名为 Introduction
的新 navbarPage
时,一切都可以很好地处理 plot 直到......我回到 Introduction navbarPage
然后再回到 Plot navbarPage
,情节不顺利。发生什么事了?如何解决?
ui.R
library(dplyr)
library(shiny)
library(ggvis)
shinyUI(
navbarPage("",
tabPanel("Introduction",
fluidRow(
column(3,
wellPanel(
)
),
column(9,
br(),br(),br(),
h5("This screen will soon display a brief introduction to the project.")
)
)
),
tabPanel("Plot",
fluidRow(
column(3,
radioButtons("dataset", label = h4("Choose dataframe"),
choices = list("Item" = "df1", "Task" = "df2")),
selectInput("yvar", "Y-axis variable", axis_vars_y, selected = "number"),
uiOutput("slider")
),
column(9,
ggvisOutput("plot")
)
)
)
)
)
server.R
library(shiny)
library(dplyr)
library(magrittr)
library(lazyeval)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
df1 = df1,
df2 = df2)
})
axis_vara_y <- reactive({
switch(input$yvar,
number = 2,
number2 = 3)
})
output$slider <- renderUI({
sliderInput("inslider","Slider", min = round(min(datasetInput()[,axis_vara_y()]),0)-1,
max = round(max(datasetInput()[,axis_vara_y()]),0)+1,
value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1,
round(max(datasetInput()[,axis_vara_y()]),0)+1),
step = 0.5)
})
data <- reactive({
filteredData <- datasetInput()
axisData <- axis_vara_y()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
if (nrow(filteredData) == 0){
return (datasetInput())
}else{
return(filteredData)
}
})
data_two <- reactive({
data() %>%
mutate(id = 1:n())
})
vis <- reactive({
yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
yvar <- prop("y", as.symbol(input$yvar))
data_two %>%
ggvis(x = ~name, y = yvar) %>%
layer_points(size := 120,
fill = ~name,
fillOpacity := 0.6)
})
vis %>% bind_shiny("plot")
})
global.R
df1_number <-sample(seq(1,20,0.01),20,replace = T)
df2_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,5,0.01),20,replace = T)
df2_number2 <-sample(seq(1,5,0.01),20,replace = T)
df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2)
df2 <- data.frame(name = rep(letters[1:4],each = 5), number = df2_number, number2 = df2_number2)
axis_vars_y <- c("number" = "number", "number2" = "number2")
我从来没有把 ggvis 放在反应式中。奇怪的事情发生就像你经历过的一样。试试 this.
shiny::runGist("https://gist.github.com/corynissen/9cdf1275f394e7945eb6")
您必须在 data_two 反应式中分配 y 值,然后 运行 在反应式 () 之外的 ggvis。
您好
我刚刚创建了 Shiny 应用程序。但是当我想以 navbarPage
方式展示我的 Shiny 应用程序时出现了一个小问题。我的意思是,当我添加名为 Introduction
的新 navbarPage
时,一切都可以很好地处理 plot 直到......我回到 Introduction navbarPage
然后再回到 Plot navbarPage
,情节不顺利。发生什么事了?如何解决?
ui.R
library(dplyr)
library(shiny)
library(ggvis)
shinyUI(
navbarPage("",
tabPanel("Introduction",
fluidRow(
column(3,
wellPanel(
)
),
column(9,
br(),br(),br(),
h5("This screen will soon display a brief introduction to the project.")
)
)
),
tabPanel("Plot",
fluidRow(
column(3,
radioButtons("dataset", label = h4("Choose dataframe"),
choices = list("Item" = "df1", "Task" = "df2")),
selectInput("yvar", "Y-axis variable", axis_vars_y, selected = "number"),
uiOutput("slider")
),
column(9,
ggvisOutput("plot")
)
)
)
)
)
server.R
library(shiny)
library(dplyr)
library(magrittr)
library(lazyeval)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
df1 = df1,
df2 = df2)
})
axis_vara_y <- reactive({
switch(input$yvar,
number = 2,
number2 = 3)
})
output$slider <- renderUI({
sliderInput("inslider","Slider", min = round(min(datasetInput()[,axis_vara_y()]),0)-1,
max = round(max(datasetInput()[,axis_vara_y()]),0)+1,
value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1,
round(max(datasetInput()[,axis_vara_y()]),0)+1),
step = 0.5)
})
data <- reactive({
filteredData <- datasetInput()
axisData <- axis_vara_y()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
if (nrow(filteredData) == 0){
return (datasetInput())
}else{
return(filteredData)
}
})
data_two <- reactive({
data() %>%
mutate(id = 1:n())
})
vis <- reactive({
yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
yvar <- prop("y", as.symbol(input$yvar))
data_two %>%
ggvis(x = ~name, y = yvar) %>%
layer_points(size := 120,
fill = ~name,
fillOpacity := 0.6)
})
vis %>% bind_shiny("plot")
})
global.R
df1_number <-sample(seq(1,20,0.01),20,replace = T)
df2_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,5,0.01),20,replace = T)
df2_number2 <-sample(seq(1,5,0.01),20,replace = T)
df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2)
df2 <- data.frame(name = rep(letters[1:4],each = 5), number = df2_number, number2 = df2_number2)
axis_vars_y <- c("number" = "number", "number2" = "number2")
我从来没有把 ggvis 放在反应式中。奇怪的事情发生就像你经历过的一样。试试 this.
shiny::runGist("https://gist.github.com/corynissen/9cdf1275f394e7945eb6")
您必须在 data_two 反应式中分配 y 值,然后 运行 在反应式 () 之外的 ggvis。