selectInput Shiny 中不同数量的列名称

different number of column name in selectIput Shiny

我在创建动态 UI (selectInput) 时遇到了一个问题。我的意思是,我有两个 dataframes 和一个 selectInput 按钮,它们应该根据我选择的数据帧更改输出数量(列名)。 我只是得到错误:Error: == only defined for equally-sized data frames 当我选择 df2 数据帧时。谁能告诉我我做错了什么?
这是我的 if 函数:

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

根据您的评论,我假设您想将 y 轴更改为在 selectInput 框中选择的任何内容。要使用 ggvis 执行此操作,您需要更改传递给绘图的数据。 你可以试试下面的代码,我改变了你的几个变量:

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