问题
I try to implement one feature in my simple shiny app. My target is to create reactive widget which will be dependent on different widget. In my example I have two important widgets: radioButtons
and selectInput
. Depending on what I choose in radioButtons
, the output of widget selectInput
will be change.
Unfortunately I get an error: Error in as.name(input$xvar) :invalid type/length (symbol/0)
. Thanks for any help.
ui.R
library(shiny)
shinyUI(
fluidPage(
fluidRow(
column(3,
selectInput('prod','', prod),
radioButtons('level','',level, level[1]),
uiOutput('in_xvar')
),
column(9,
ggvisOutput('ggvis_plot')
)
)
))
server.R
library(shiny)
library(ggvis)
library(dplyr)
shinyServer(function(input, output) {
data0 <- reactive({
df <- test_data
df <- df %>%
filter(prod == input$prod)
})
data <- reactive({
df <- data0()
df <- df %>%
filter(level == input$level)
})
output$in_xvar <- renderUI({
choosen_list <- axis_x[input$level]
selectInput('xvar','', choosen_list)
})
ggvis_plot <- reactive({
x <- prop('x', as.name(input$xvar))
plot <- data() %>%
ggvis(x, ~value) %>%
layer_points(fill = ~part)
})
ggvis_plot %>% bind_shiny('ggvis_plot')
})
global.R
prod <- c('P1','P2','P3')
level <- c('L1','L2','L3')
part <- c('p1','p2','p3','p4','p5')
axis_x <- list(L1 = list('Ordering' = 'id'),
L2 = list('Ordering' = 'id', 'Part name' = 'part'),
L3 = list('Ordering' = 'id', 'Part name' = 'part'))
set.seed(123)
test_data <- data.frame(prod = sample(prod,300, replace = T),
level = sample(level, 300, replace = T),
part = sample(part, 300, replace = T),
value = rnorm(300))
test_data <- test_data %>%
group_by(prod) %>%
mutate(id = 1:n()) %>%
arrange(prod, id)
回答1:
You get the error because input$xvar
is initially NULL
, try adding an if/else in your ggvis_plot
:
ggvis_plot <- reactive({
if(is.null(input$xvar))
x <- prop('x', as.name("id"))
else
x <- prop('x', as.name(input$xvar))
plot <- data() %>%
ggvis(x, ~value) %>%
layer_points(fill = ~part)
})
来源:https://stackoverflow.com/questions/34619117/reactive-updates-in-shiny-app-selectinput-and-radiobuttons-ggvis