反应性变化 min/max 范围 + 2 个数据帧闪亮
reactive change min/max range + 2 dataframes Shiny
我的 Shiny 应用程序有一个问题。首先,我有两个 dataframes
,其中有两个 numeric
列(number
和 number2
)。我也有动态uisliderInput
。闪亮的应用程序工作正常,直到...当我选择 Item
数据框时,在 Y-axis variable
中选择 number
并在 sliderInput
中设置 range
例如15 和 18,之后我想将 Y-axis variable
更改为 number2
我收到错误消息:
Error in eval(substitute(expr), envir, enclos) : wrong result size (2), expected 0 or 1
我知道问题是因为 number2
列中的数字介于 1 和 10 之间,而之前的设置不包括该数字。谁能告诉我如何改进它?
ui.R
library(ggvis)
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
radioButtons("dataset", label = h4("Choose dataframe"),
choices = list("Item" = "df1", "Task" = "df2")),
selectInput("yvar", "Y-axis variable", axis_vars_y, selected = "number"),
uiOutput("slider")
),
mainPanel(
ggvisOutput("plot")
)
)
))
server.R
library(shiny)
library(dplyr)
library(magrittr)
library(lazyeval)
df1_number <-sample(seq(1,20,0.01),20,replace = T)
df2_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,10,0.01),20,replace = T)
df2_number2 <-sample(seq(1,10,0.01),20,replace = T)
df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2)
df2 <- data.frame(name = rep(letters[1:4],each = 5), number = df2_number, number2 = df2_number2)
axis_vars_y <- c("number" = "number", "number2" = "number2")
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
df1 = df1,
df2 = df2)
})
axis_vara_y <- reactive({
switch(input$yvar,
number = 2,
number2 = 3)
})
output$slider <- renderUI({
sliderInput("inslider","Slider", min = min(datasetInput()[,axis_vara_y()]),
max = max(datasetInput()[,axis_vara_y()]),
value = c(min(datasetInput()[,axis_vara_y()]),
max(datasetInput()[,axis_vara_y()])),
step = 0.5)
})
data <- reactive({
filteredData <- datasetInput()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axis_vara_y()] >= input$inslider[1],
filteredData[,axis_vara_y()] <= input$inslider[2])
}
filteredData
})
data_two <- reactive({
data() %>%
mutate(id = 1:n())
})
vis <- reactive({
yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
yvar <- prop("y", as.symbol(input$yvar))
data_two %>%
ggvis(x = ~name, y = yvar) %>%
layer_points(size := 120,
fill = ~name,
fillOpacity := 0.6,
key := ~id)
})
vis %>% bind_shiny("plot")
})
更新-------------------------------- ---------------------------------------------- --------------
当我设置的范围不包含 number
列中的任何值(例如 13 到 14 之间)时,会发生相同的 error
。
如您所知,您收到错误是因为过滤后的数据集没有行。一个简单的解决方法是 return 完整数据集。这将重置 inputSlider
以便您继续工作而不会出错。你只需要改变你的反应data
功能。
data <- reactive({
filteredData <- datasetInput()
axisData <- axis_vara_y()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
# the new part to reset the slider
if(nrow(filteredData) == 0){
return(datasetInput())
}else{
return(filteredData)
}
})
我冒昧地稍微简化了代码,因为您可以分配反应式数据语句,而不是让闪亮的应用程序多次调用它们。
我的 Shiny 应用程序有一个问题。首先,我有两个 dataframes
,其中有两个 numeric
列(number
和 number2
)。我也有动态uisliderInput
。闪亮的应用程序工作正常,直到...当我选择 Item
数据框时,在 Y-axis variable
中选择 number
并在 sliderInput
中设置 range
例如15 和 18,之后我想将 Y-axis variable
更改为 number2
我收到错误消息:
Error in eval(substitute(expr), envir, enclos) : wrong result size (2), expected 0 or 1
我知道问题是因为 number2
列中的数字介于 1 和 10 之间,而之前的设置不包括该数字。谁能告诉我如何改进它?
ui.R
library(ggvis)
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
radioButtons("dataset", label = h4("Choose dataframe"),
choices = list("Item" = "df1", "Task" = "df2")),
selectInput("yvar", "Y-axis variable", axis_vars_y, selected = "number"),
uiOutput("slider")
),
mainPanel(
ggvisOutput("plot")
)
)
))
server.R
library(shiny)
library(dplyr)
library(magrittr)
library(lazyeval)
df1_number <-sample(seq(1,20,0.01),20,replace = T)
df2_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,10,0.01),20,replace = T)
df2_number2 <-sample(seq(1,10,0.01),20,replace = T)
df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2)
df2 <- data.frame(name = rep(letters[1:4],each = 5), number = df2_number, number2 = df2_number2)
axis_vars_y <- c("number" = "number", "number2" = "number2")
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
df1 = df1,
df2 = df2)
})
axis_vara_y <- reactive({
switch(input$yvar,
number = 2,
number2 = 3)
})
output$slider <- renderUI({
sliderInput("inslider","Slider", min = min(datasetInput()[,axis_vara_y()]),
max = max(datasetInput()[,axis_vara_y()]),
value = c(min(datasetInput()[,axis_vara_y()]),
max(datasetInput()[,axis_vara_y()])),
step = 0.5)
})
data <- reactive({
filteredData <- datasetInput()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axis_vara_y()] >= input$inslider[1],
filteredData[,axis_vara_y()] <= input$inslider[2])
}
filteredData
})
data_two <- reactive({
data() %>%
mutate(id = 1:n())
})
vis <- reactive({
yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
yvar <- prop("y", as.symbol(input$yvar))
data_two %>%
ggvis(x = ~name, y = yvar) %>%
layer_points(size := 120,
fill = ~name,
fillOpacity := 0.6,
key := ~id)
})
vis %>% bind_shiny("plot")
})
更新-------------------------------- ---------------------------------------------- --------------
当我设置的范围不包含 number
列中的任何值(例如 13 到 14 之间)时,会发生相同的 error
。
如您所知,您收到错误是因为过滤后的数据集没有行。一个简单的解决方法是 return 完整数据集。这将重置 inputSlider
以便您继续工作而不会出错。你只需要改变你的反应data
功能。
data <- reactive({
filteredData <- datasetInput()
axisData <- axis_vara_y()
if(!is.null(input$inslider)){
filteredData <- filteredData %>%
filter(filteredData[,axisData] >= input$inslider[1],
filteredData[,axisData] <= input$inslider[2])
}
# the new part to reset the slider
if(nrow(filteredData) == 0){
return(datasetInput())
}else{
return(filteredData)
}
})
我冒昧地稍微简化了代码,因为您可以分配反应式数据语句,而不是让闪亮的应用程序多次调用它们。