创建动态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")
})
答案 0 :(得分:1)
从您的评论中,我假设您想要将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")
)
)
))