r - How to use function with passing function has reactive as input in shiny -
i have little problem. have build package called d3k can used across different dashboard. 1 of function follows:
conditionalrendervaluebox <- function(value, title, red_limit, yellow_limit){ rendervaluebox( valuebox(value, title, color = if(class(value)=="character" | is.na(value)){ "blue" }else if(value>red_limit ){ "red" }else if(value>yellow_limit){ "yellow" }else{ "green" } )) }
now trying pass value parameter in function, parameter reactive value.
server.r
library(lubridate) # library(googlevis) # library(readr) library(shinyjs) library(ggplot2) library(plotly) library(d3k) library(dplyr) server <- function(input, output, session) { v1 = reactive({ input$v1 }) f <- reactive({ if(is.na(v1())){ "wai" }else{ runif(1, 1, 10) } }) output$t <- conditionalrendervaluebox(f(), "possible value", 15, 10) }
ui.r
library(shinydashboard) library(shiny) library(shinyjs) library(plotly) ui <- dashboardpage( dashboardheader(title = "dashboard") ,skin = 'yellow' ,dashboardsidebar( tags$head( tags$style(html(" .sidebar { height: 90vh; overflow-y: auto; } " ) ) ), sidebarmenu( menuitem("r", tabname = "r", icon = icon("cog")) , selectinput("v1", label = h3("select box"), choices = list( 1, 11, 15), selected = 1), ) ) ,dashboardbody( tabitems( tabitem( tabname = "r" , br() , fluidrow( valueboxoutput("t") ) ) ) ) )
i not able see value box in shiny dashboard.
however, if use following code in plase of output$t in server , works
output$t <- rendervaluebox( valuebox(f(), "title", color = if(class(f())=="character" | is.na(f())){ "blue" }else if(f()>red_limit ){ "red" }else if(f()>yellow_limit){ "yellow" }else{ "green" } ))
then able see result expected
i find runs if define conditionalrendervaluebox
in script so:
library(lubridate) # library(googlevis) # library(readr) library(shinyjs) library(ggplot2) library(plotly) # library(d3k) don't have access package library(dplyr) library(shinydashboard) library(shiny) library(shinyjs) library(plotly) conditionalrendervaluebox <- function(value, title, red_limit, yellow_limit){ rendervaluebox( valuebox(value, title, color = if(class(value)=="character" | is.na(value)){ "blue" }else if(value>red_limit ){ "red" }else if(value>yellow_limit){ "yellow" }else{ "green" } } server <- function(input, output, session) { v1 = reactive({ input$v1 }) f <- reactive({ if(is.na(v1())){ "wai" }else{ runif(1, 1, 10) } }) output$t <- conditionalrendervaluebox(f(), "possible value", 15, 10) )) } ui <- dashboardpage( dashboardheader(title = "dashboard") ,skin = 'yellow' ,dashboardsidebar( tags$head( tags$style(html(" .sidebar { height: 90vh; overflow-y: auto; } " ) ) ), sidebarmenu( menuitem("r", tabname = "r", icon = icon("cog")) , selectinput("v1", label = h3("select box"), choices = list( 1, 11, 15), selected = 1) ) ) ,dashboardbody( tabitems( tabitem( tabname = "r" , br() , fluidrow( valueboxoutput("t") ) ) ) ) ) runapp(shinyapp(server=server,ui=ui))
i guessing problem how package exports function, it's hard me know without seeing code.
hope helps.
edit: hey don't know d3k
package , if you've gotten work, far can tell don't want write functions wrap render* shiny functions. app below won't work:
myfunc <- function(x) { rendertable({ head(x) }) } shinyapp( ui=fluidpage( selectinput("select","choose dataset",c("mtcars","iris")), tableoutput("table") ), server=function(input,output) { dataset <- reactive({ get(input$select) }) output$table <- myfunc(dataset()) })
the function runs once on start-up , renders initial table, never changes after because myfunc
doesn't understand reactivity render* functions do.
i think function should wrap valuebox
element , feed function rendervaluebox
so:
library(lubridate) # library(googlevis) # library(readr) library(shinyjs) library(ggplot2) library(plotly) # library(d3k) don't have access package library(dplyr) library(shinydashboard) library(shiny) library(shinyjs) library(plotly) conditionalrendervaluebox <- function(value, title, red_limit, yellow_limit){ #rendervaluebox( valuebox(value, title, color = if(class(value)=="character" | is.na(value)){ "blue" }else if(value>red_limit ){ "red" }else if(value>yellow_limit){ "yellow" }else{ "green" } ) #) } server <- function(input, output, session) { v1 = reactive({ input$v1 }) f <- reactive({ v1 <- v1() print("hey") if(is.na(v1)){ "wai" }else{ runif(1, 1, 10) } }) observe({ output$t <- rendervaluebox(conditionalrendervaluebox(f(), "possible value", 15, 10)) }) } ui <- dashboardpage( dashboardheader(title = "dashboard") ,skin = 'yellow' ,dashboardsidebar( tags$head( tags$style(html(" .sidebar { height: 90vh; overflow-y: auto; } " ) ) ), sidebarmenu( menuitem("r", tabname = "r", icon = icon("cog")) , selectinput("v1", label = h3("select box"), choices = list( 1, 11, 15), selected = 1) ) ) ,dashboardbody( tabitems( tabitem( tabname = "r" , br() , fluidrow( valueboxoutput("t") ) ) ) ) ) runapp(shinyapp(server=server,ui=ui))
Comments
Post a Comment