r - Using observer to hold the resulted data subset in a vector -


i new shiny r.
can me solve issue below.

i trying plot data using dataset, , user defined option "all" added "selectlist" of "region" provided in ui.

when "all" option selected "selectlist", how can use below observer store information regions vector "l", same can used query based on other user inputs

observe({     if("all" %in% input$region) {       selected <- setdiff(allchoice, "all")       updateselectinput(session, "region", selected = selected)                  }     }) 

ref: how add user defined value select list of values dataset

ui.r

library(shiny) library("rmysql") library(ggplot2) library(plotly) library(dt) library(dplyr) dataset <- read.csv("dataset.csv", header=true) dataset$x <- null allchoice <- c("all", levels(dataset$region))  fluidpage(   title = "abc xyz",   hr(),   fluidrow(     titlepanel("abc xyz"),     sidebarpanel(             daterangeinput('daterange',                        label = 'date input',                        start = as.date("1967-01-01"), end = sys.date()),        selectinput("region", label = "region",                    choices = allchoice,                    selected = 1),        selectinput("gender", label = "gender",                    choices = unique(dataset$gender), multiple = true,                    selected = unique(dataset$gender)),        selectinput('x', 'x', names(dataset), names(dataset)[[2]]),       selectinput('y', 'y', names(dataset), names(dataset)[[8]]),     hr()     ),      mainpanel(       column(12, plotlyoutput("plot1")),       hr(),      column(12, plotlyoutput("plot2"))     )   ) ) 

server.r

library(ggplot2) library("rmysql") library("mgcv") library(plotly) function(input, output, session) {     dataset <- read.csv("dataset.csv", header=true)     dataset$x <- null      dataset$date <- as.date(dataset$date)      if(input$region == "all"){       l <- observe({       if("all" %in% input$region) {         selected <- setdiff(allchoice, "all")         updateselectinput(session, "region", selected = selected)               }      })     }     else{       l <- reactive(subset(dataset, region %in% input$region))     }     k <- reactive({subset(l(), date >= as.date(input$daterange[1]) & date <= as.date(input$daterange[2]))})     n <- reactive(subset(k(), gender %in% input$gender))      #output plots     output$plot1 <- renderplotly({     p <- ggplot(n(), aes_string(x=input$x, y=input$y)) + geom_point(alpha=0.4)     ggplotly(p)     })      output$plot2 <- renderplotly({     q <- ggplot(n(), aes_string(x=input$x, y=input$y)) + geom_smooth()     ggplotly(q)     }) } 

error facing -

warning: error in .getreactiveenvironment()$currentcontext: operation not allowed without active reactive context. (you tried can done inside reactive expression or observer.) stack trace (innermost first):     46: .getreactiveenvironment()$currentcontext     45: .subset2(x, "impl")$get     44: $.reactivevalues     43: $ [d:\demo\server.r#36]     42: server $ [d:\demo\server.r#36]      1: runapp error in .getreactiveenvironment()$currentcontext() :    operation not allowed without active reactive context. (you tried can done inside reactive expression or observer.) 

note: vocabulary above may off, please correct me if i'm wrong, totally new world of r. in advance.

edit 1:

listening on http://127.0.0.1:5128     recommend use dev version of ggplot2 `ggplotly()`     install with: `devtools::install_github('hadley/ggplot2')`     warning in origrenderfunc() :     ignoring explicitly provided widget id "2988253b22c1"; shiny doesn't use them     recommend use dev version of ggplot2 `ggplotly()`     install with: `devtools::install_github('hadley/ggplot2')`     `geom_smooth()` using method = 'gam'     warning in origrenderfunc() : ignoring explicitly provided widget id "29885be33e8"; shiny doesn't use them 

and when that, getting many exceptions , same exceptions above again. worried if same affect application in long run, can suggest that?

thanks again.

you have not provided example data can guess , via looking @ error says whats problem: no active reactive context, assume in part:

if(input$region == "all"){       l <- observe({       if("all" %in% input$region) {         selected <- setdiff(allchoice, "all")         updateselectinput(session, "region", selected = selected)               }      })     }     else{       l <- reactive(subset(dataset, region %in% input$region))     } 

[!] not understand need observer...i think should work totally fine if use if...else... statement.

[!] , additionally have no idea why @ first wanna vector of choices (except "all") , use selected choice in selectinput, may ask for? , else statement should give subset of data based on input$region.

so shortly saying: if gives updatedselectinput , else gives dataset --> not make sense @ all..

and should simple that, if "all" selected there no need subset dataset, if other choice "all" selected subset of dataset should happen:

l <- reactive({ if(input$region == "all"){ dataset }else{ dataset <- subset(dataset, region %in% input$region) }}) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -