0

I have one problem with create dynamic UI (selectInput). I mean, I have two dataframes and one selectInput button which should change number of output (column name) depending on dataframe which I choose. I just get error: Error: == only defined for equally-sized data frames when I choose df2 dataframe. Could anyone tell me what I do wrong?
This is my if function:

  output$xvars <- renderUI({
    if (datasetInput() == df1){
      axis_vars_x <- colnames(df1[c(1,2)])
      selectInput("xvar", "X-axis variable", axis_vars_x, selected = "id")
    }
    else{
      axis_vars_x <- colnames(df2[1])
      selectInput("xvar", "X-axis variable", axis_vars_x, selected = "id")
    }
  })

ui.R

library(dplyr)
library(shiny)
library(ggvis)

shinyUI(fluidPage(

titlePanel("Old Faithful Geyser Data"),

sidebarLayout(
 sidebarPanel(
  radioButtons("dataset", label = h4("Product level"),
               choices = list("Item" = "df1", "Task" = "df2")),
  uiOutput("xvars"),
  ),
 mainPanel(
  ggvisOutput("plot")
  )
 )
))

server.R

library(shiny)
library(dplyr)

df1 <- data.frame(id = c(1,2,3,4,5), number = c(20,30,23,25,34), ds =  c(1,2,3,42,2))
df2 <- data.frame(id = c(1,2), number = c(33,40), ds = c(1,2))

shinyServer(function(input, output) {

 datasetInput <- reactive({
  switch(input$dataset,
         df1 = df1,
         df2 = df2)
 })

 output$xvars <- renderUI({
    if (datasetInput() == df1){
      axis_vars_x <- colnames(df1[c(1,2)])
      selectInput("xvar", "X-axis variable", axis_vars_x, selected = "id")
    }
    else{
      axis_vars_x <- colnames(df2[1])
      selectInput("xvar", "X-axis variable", axis_vars_x, selected = "id")
    }
  })

 data <- reactive({
 df <- datasetInput()
 })

vis <- reactive({
 data %>%
  ggvis(~id, ~number) %>%
  layer_points(fill = ~factor(id)) %>%
  scale_nominal("fill", range = c("red","blue","green","yellow","black"))

})
vis %>% bind_shiny("plot")
})
Nicolabo
  • 1,337
  • 12
  • 30
  • use `identical(df1,datasetInput())`, instead of '==' see [this](http://stackoverflow.com/questions/10592148/compare-if-two-objects-in-r-are-equal) – NicE Feb 10 '15 at 13:39
  • also you code doesn't run as is, you have an extra comma after `uiOutput("xvars")`... – NicE Feb 10 '15 at 13:40
  • OK thanks once again! I wondering how to use it to manipulate y-axis. I mean, instead of `axis_vars_x <- colnames(df1[c(1,2)])` I use `axis_vars_x <- colnames(df1[c(2,3)])` to get numeric values. How to change y-axis depending on my choice in `selectInput`. Also use `if(!is.null(input$xvar))` concept and then `var <- prop("y", as.symbol(input$xvar))`? – Nicolabo Feb 10 '15 at 13:58
  • @NicE Could you tell me how introduce this variable to the `output` result? – Nicolabo Feb 10 '15 at 23:54

1 Answers1

1

From you comments, I assumed you wanted to change the y-axis to whatever was selected in the selectInput boxes. To do this with ggvis you need to change the data you pass to the plot. You can try the following code, I changed a few of your variables:

server.R

library(shiny)
library(dplyr)

df1 <- data.frame(id = c(1,2,3,4,5), number = c(20,30,23,25,34), ds =  c(1,2,3,42,2))
df2 <- data.frame(id = c(1,2), number = c(33,40), ds = c(1,2))

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           "df1" = df1,
           "df2" = df2)
  })

  output$yvars <- renderUI({
    if (identical(df1,datasetInput())){
      axis_vars_y <- colnames(df1[-1])
      selectInput("yvar", "X-axis variable", axis_vars_y, selected = "id")
    }
    else{
      axis_vars_y <- colnames(df2[-1])
      selectInput("yvar", "X-axis variable", axis_vars_y, selected = "id")
    }
  })

  yVarName<-reactive({
    yValue<-"number"
    if(!is.null(input$yvar)){
      yValue<-input$yvar
    }
    yValue
  })

  data <- reactive({
    df<-datasetInput()
    yValue<-"number"
    if(!is.null(input$yvar)){
      yValue<-input$yvar
    }
    df <- datasetInput()[,c("id",yValue)]
    names(df)<-c("id","yVar")
    df
  })


  vis <- reactive({

    data %>%
      ggvis(~id, ~yVar) %>%
      layer_points(fill = ~factor(id)) %>%
      scale_nominal("fill", range = c("red","blue","green","yellow","black")) %>%
      add_axis("y", title = yVarName())

  })
  vis %>% bind_shiny("plot")
})

ui.R

library(dplyr)
library(shiny)
library(ggvis)

shinyUI(fluidPage(

  titlePanel("Old Faithful Geyser Data"),

  sidebarLayout(
    sidebarPanel(
      radioButtons("dataset", label = h4("Product level"),
                   choices = list("Item" = "df1", "Task" = "df2")),
      uiOutput("yvars")
    ),
    mainPanel(
      ggvisOutput("plot")
    )
  )
))
NicE
  • 21,165
  • 3
  • 51
  • 68