reactive updates in shiny app - one-way dependence

廉价感情. 提交于 2019-12-11 10:26:43

问题


In the previous problem with reactive updates @NicE helps me understand how it works. Nevertheless, there is another issue here. Based on my code (included below) every time I change radioButtons widget, the value in selectInput widget automatically come back to the id value, even if I do not want to. For example, when I change L1 -> L3 and then change Ordering -> Part name and then I want to go to L3 -> L2, selectInput widget automatically come back to the Ordering. I know why it happened but is there any solution to implement in order to avoid such automatization (if it is not necessary of course - although it is inevitable move in L3 -> L1). Thanks!

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({
                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)
        })

        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:


I'm not sure whether I understand your problem yet completely. But if the issue is that the third drop down box switches its value once you select another Radio Button, this code should help

output$in_xvar <- renderUI({
  choosen_list <- axis_x[input$level]
  selectInput('xvar','', choosen_list,selected=input$xvar)
})


来源:https://stackoverflow.com/questions/34631794/reactive-updates-in-shiny-app-one-way-dependence

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