reactive updates in shiny app selectInput and radioButtons - ggvis

孤街醉人 提交于 2019-12-11 00:39:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!