我无法在闪亮的应用程序中的ggvis绘图中获取输入滑块.这些图表在没有输入滑块的情况下呈现正常但在添加它之后闪亮会抛出此错误:
Listening on http://xxxxxxxxxxxxxx
Error in eval(expr, envir, enclos) : could not find function "compdat" Run Code Online (Sandbox Code Playgroud)
server.R:
library(shiny)
library(ggvis)
data<-data.frame(var1=rnorm(30,5,2.3),var2=rbeta(30,1.5,.8),var3=rnorm(30,10,2.5))
shinyServer(function(input, output,session) {
compdat<-reactive({data[, c(input$xInp,input$yInp)]})
vis1 <-reactive({
compdat %>% ggvis(x= ~compdat()[,1],y= ~compdat()[,2]) %>%
layer_points(fill:="red") %>% layer_smooths(span=input_slider(.1,1,id="scores_ui"))
})
vis1 %>% bind_shiny("scores",controls_id="scores_ui")
vis2<-reactive({
compdat %>% ggvis(x= ~compdat()[,1],y= ~compdat()[,2]) %>%
layer_points(fill:="red") %>% ayer_smooths(span=input_slider(.1,1,id="loadings_ui"))
})
vis2 %>% bind_shiny("loadings",controls_id="loadings_ui")
})Run Code Online (Sandbox Code Playgroud)
ui.R:
shinyUI(fluidPage(
title="PCA Explorer",
h2("Principal Component Explorer"),
fluidRow(
column(6,ggvisOutput("scores"),
uiOutput("scores_ui")),
column(6,ggvisOutput("loadings"),
uiOutput("loadings_ui"))
),
br(),
fluidRow(
column(6,h3("Component Selection"),selectInput('xInp',"X Variable",names(data)),
selectInput('yInp',"Y Variable",names(data),selected=names(data)[[2]])),
column(6,h3("Summary of Selected Data Points"),verbatimTextOutput("diagn"))
)
))Run Code Online (Sandbox Code Playgroud)
有关如何使滑块渲染的任何见解都会很棒.我花了相当多的时间来挖掘这个问题.提前致谢
可以在此Movie Explorer示例中找到演示如何使用selectizeInput自定义X/Y轴变量的一个很好的示例.
然而,包装一个ggvis()反应环境中的功能具有显着的缺点(或错误),一旦你改变input$xInp$或者input$yInp,在layer_smooths()停止反应,你的滑块输入.
您的代码的另一个潜在问题data是不可见ui.R.您可能想要创建一个global.R包含data对象的文件.
下面我介绍两种方法,ggvis通过选择X/Y变量,如何与您的绘图进行交互.你可以找到它们server.R.
data <- data.frame(var1=rnorm(30,5,2.3),
var2=rbeta(30,1.5,.8),
var3=rnorm(30,10,2.5))
Run Code Online (Sandbox Code Playgroud)
library(shiny)
shinyUI(fluidPage(
title="PCA Explorer",
h2("Principal Component Explorer"),
fluidRow(
column(6,
ggvisOutput("scores"),
uiOutput("scores_ui")),
column(6,
ggvisOutput("loadings"),
uiOutput("loadings_ui"))
),
br(),
fluidRow(
column(6,
h3("Component Selection"),
selectInput('xInp',"X Variable", choices=names(data),
selected=names(data)[[1]]),
selectInput('yInp',"Y Variable", choices=names(data),
selected=names(data)[[2]])
),
column(6,
h3("Summary of Selected Data Points"),
verbatimTextOutput("diagn"))
)
))
Run Code Online (Sandbox Code Playgroud)
library(shiny)
library(ggvis)
shinyServer(function(input, output,session) {
# Approach 1: regenerate a compdat object once the input changes
# rename the X/Y variables to fixed names.
compdat <- reactive({
x <- data[, c(input$xInp, input$yInp)]
names(x) <- c("x", "y")
x
})
# NOTE that you use compdat here instead of compdat()
compdat %>% ggvis(x=~x, y=~y) %>%
layer_points(fill:="red") %>%
layer_smooths(span=input_slider(.1,1)) %>%
bind_shiny("scores", controls_id="scores_ui")
# Approach 2: wrap ggvis in a reactive environment
# This however, would stop to react to slider input
# once input$xInp or input$yInp changes.
vis2 <- reactive({
xvar <- prop("x", as.symbol(input$xInp))
yvar <- prop("y", as.symbol(input$yInp))
data %>% ggvis(x=xvar, y=yvar) %>%
layer_points(fill:="red") %>%
layer_smooths(span=input_slider(.1,1))
})
vis2 %>% bind_shiny("loadings", controls_id="loadings_ui")
})
Run Code Online (Sandbox Code Playgroud)
两种方法都应该有效(好吧,差不多).但是等等,一旦更改了X/Y变量,您可能会看到平滑层停止响应滑块更改.
要解决此问题,请考虑以下解决方案.
我之前提到的错误可以通过创建一个sliderInputin 来修复ui.R.
library(shiny)
shinyUI(fluidPage(
title="PCA Explorer",
h2("Principal Component Explorer"),
fluidRow(
column(6,
ggvisOutput("scores"),
uiOutput("scores_ui")
),
column(6,
ggvisOutput("loadings"),
uiOutput("loadings_ui"),
# Create a slider by Shiny, instead of by ggvis.
sliderInput('smooth_span',
h5("Smoothing span for plot 2"),
.1, 1, value=0.5)
)
),
br(),
fluidRow(
column(6,
h3("Component Selection"),
selectInput('xInp',"X Variable", choices=names(data),
selected=names(data)[[1]]),
selectInput('yInp',"Y Variable", choices=names(data),
selected=names(data)[[2]])
),
column(6,
h3("Summary of Selected Data Points"),
verbatimTextOutput("diagn"))
)
))
Run Code Online (Sandbox Code Playgroud)
library(shiny)
library(ggvis)
shinyServer(function(input, output,session) {
# Approach 1: regenerate a compdat object once the input changes
# rename the X/Y variables to fixed names.
compdat <- reactive({
x <- data[, c(input$xInp, input$yInp)]
names(x) <- c("x", "y")
x
})
# NOTE that you use compdat here instead of compdat()
compdat %>% ggvis(x=~x, y=~y) %>%
layer_points(fill:="red") %>%
layer_smooths(span=input_slider(.1,1)) %>%
bind_shiny("scores", controls_id="scores_ui")
# Approach 2: wrap ggvis in a reactive environment
# This however, would stop to react to slider input
# once input$xInp or input$yInp changes.
vis2 <- reactive({
xvar <- prop("x", as.symbol(input$xInp))
yvar <- prop("y", as.symbol(input$yInp))
smooth.span <- input$smooth_span
data %>% ggvis(x=xvar, y=yvar) %>%
layer_points(fill:="red") %>%
# FIXED: use the value from the input object, instead of a input_slider
layer_smooths(span=smooth.span)
})
vis2 %>% bind_shiny("loadings", controls_id="loadings_ui")
})
Run Code Online (Sandbox Code Playgroud)
包装ggvis成一个反应环境有一些缺点:
但是,它确实有一些优点:
ggvis,AFAIK当前是唯一的方式input$xInp.由于ggvis仅对ggvis对象计算和绑定数据一次,因此对轴标签所做的更改不会实时反映.但是,因为ggvis在反应环境中包装会导致整个图形重绘,所以标记也会在重绘中更新.