Ggvis rendering in brilliant

I'm having trouble getting an input slider to display in ggvis graphics in a brilliant app. The graphs are displayed perfectly without an input slider, but after adding its shine it throws this error:

Listening on http://xxxxxxxxxxxxxx
Error in eval(expr, envir, enclos) : could not find function "compdat" 

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

    })

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

      )
    ))

Any information on how to get a slider for rendering would be great. I spent a lot of time understanding this. thanks in advance

+4
source share
1 answer

A good example demonstrating how you can change the X / Y axis variables with selectizeInput can be found here in this example in Google Explorer .

ggvis() ( ), input$xInp$ input$yInp, layer_smooths() .

, data ui.R. , global.R, data.

, ggvis, X/Y. server.R.

global.R

data <- data.frame(var1=rnorm(30,5,2.3),
                 var2=rbeta(30,1.5,.8),
                 var3=rnorm(30,10,2.5))

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

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

server.R

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

(, ). , , X/Y.

, .


2

, , ​​ sliderInput ui.R.

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

server.R

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

ggvis :

  • : .
  • : , , 1.

:

  • : ggvis - AFAIK, , X-Y (, input$xInp). ggvis ggvis , , , . , ggvis , .
+9