R 在shiny中渲染ggvis控件

R 在shiny中渲染ggvis控件,r,shiny,ggvis,R,Shiny,Ggvis,在一个闪亮的应用程序中,我很难在ggvis绘图中渲染输入滑块。在没有输入滑块的情况下,打印渲染良好,但添加后会引发以下错误: Listening on http://xxxxxxxxxxxxxx Error in eval(expr, envir, enclos) : could not find function "compdat" 服务器.R: library(shiny) library(ggvis) data<-data.frame(var1=rnor

在一个闪亮的应用程序中,我很难在ggvis绘图中渲染输入滑块。在没有输入滑块的情况下,打印渲染良好,但添加后会引发以下错误:

Listening on http://xxxxxxxxxxxxxx
Error in eval(expr, envir, enclos) : could not find function "compdat" 
服务器.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")

    })

任何关于如何使滑块渲染的见解都将非常有用。我花了相当多的时间四处寻找来解决这个问题。提前感谢

这里有一个很好的示例,演示了如何使用selectizeInput自定义X/Y轴变量

但是,将
ggvis()
函数包装在反应式环境中有一个突出的缺点(或缺陷),即一旦更改
input$xInp$
input$yInp
,层平滑()就会停止对滑块输入作出反应

代码的另一个潜在问题是
数据对
ui.R
不可见。 您可能想创建一个包含
数据的
全局.R
文件

下面我将介绍两种方法,说明如何通过选择X/Y变量与
ggvis
绘图交互。您可以在
server.R
中找到它们

全球 服务器.R 服务器.R
库(闪亮)
图书馆(ggvis)
shinyServer(功能(输入、输出、会话){
#方法1:一旦输入发生变化,重新生成compdat对象
#将X/Y变量重命名为固定名称。
compdat%
图层平滑(span=input\u滑块(.1,1))%>%
绑定(“分数”,控制分数)
#方法2:在反应性环境中包装ggvis
#然而,这将停止对滑块输入作出反应
#一旦输入$xInp或输入$yInp更改。
vis2%bind_Shining(“加载”,控件id=“加载用户界面”)
})
最后的话 将
ggvis
包装到反应式环境中有几个缺点:

  • 性能降低:因为一旦相关输入变量发生变化,整个图形需要重新绘制
  • 缺少过渡动画:因为整个图形都被重新绘制了,所以您没有看到在方法1中看到的良好过渡效果
但是,它确实有几个优点:

  • 更高的灵活性:如果您想动态更改X-Y轴标签(例如,取决于您的
    输入$xInp
    ),那么在反应式环境中包装
    ggvis
    是目前唯一的方式。由于
    ggvis
    只计算一次数据并将其绑定到
    ggvis
    对象,因此对轴标签所做的更改不会实时反映出来。然而,由于在反应式环境中包装
    ggvis
    会导致整个图形重新绘制,因此标签也会在重新绘制中进行更新

感谢您关注这一点。我实现了这两种方法,并且认为我当时更喜欢方法1。TC。


    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"))

      )
    ))
data <- data.frame(var1=rnorm(30,5,2.3),
                 var2=rbeta(30,1.5,.8),
                 var3=rnorm(30,10,2.5))
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"))
    )
))
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")
})
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"))
    )
))
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")
})