samedi 1 avril 2017

How to pass checkbox input into a function in shinyServer()

I have this code, which is currently deployable via RStudio. I expect it to take the user input from Checkbox and pass the value to get_table() parameter logit.

library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
library(DT)
#-------------------------
# Function 
#-------------------------
get_table <- function (dat, logit=FALSE, wanted_cols = c("model","mpg","wt"), data_title="Foo table") {

  # This function creates a table 
  # an perform log function if logit=TRUE

  dat_out <- select(dat, one_of(wanted_cols)) 
  if(logit) {
    dat_out <- select(dat, one_of(wanted_cols[2:length(wanted_cols)])) 
    dat_out <- log(dat_out)
    dat_out <- bind_cols(dat[1], dat_out)

  }
  return(dat_out)

}

#-------------------------
# Begin Shiny App code (ui + server)
#-------------------------
# I literally want to use file as input 
infile <- "http://ift.tt/2mY4Bal"
dat <- read_delim(infile,delim=",", col_types = cols())  
get_table(dat,logit=TRUE, wanted_cols=c("model","mpg","wt"), data_title="Cool Table")

ui <- shinyUI(
  fluidPage(

    #  Application title
    titlePanel("Checkbox"),

    # Sidebar with sliders that demonstrate various available
    # options
    sidebarLayout(
      sidebarPanel(
        checkboxInput("checkbox", label = "Log Scale", value = FALSE)        
      ),

      # Show a table summarizing the values entered
      mainPanel(
        DT::dataTableOutput('myplot_table')
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {

  output$value <- renderPrint({ input$checkbox })
  out_table <- get_table(dat,logit=FALSE, wanted_cols=c("model","mpg","wt"), data_title="Cool Table")
  output$myplot_table <- DT::renderDataTable(
    DT::datatable(out_table, options = list(pageLength = 10))
  )
})

shinyApp(ui = ui, server = server)

Currently the Apps looks like this:

enter image description here

Now, when I checked the box, it doesn't pass the parameter to logit function. If it works the value it the table will be log.

My question is how can I pass the checkbox value to the parameter in shinyServer() ? I tried this

out_table <- get_table(dat,logit=input$checkbox, wanted_cols=c("model","mpg","wt"), data_title="Cool Table")

but gave error:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    47: .getReactiveEnvironment()$currentContext
    46: .subset2(x, "impl")$get
    45: $.reactivevalues
    44: $ [/Users/pduboois/Desktop/Test/toy_app/etc/allinone_app.R#11]
    43: get_table [/Users/pduboois/Desktop/Test/toy_app/etc/allinone_app.R#11]
    42: server [/Users/pdubooisa/Desktop/Test/toy_app/etc/allinone_app.R#57]
     1: runApp
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)




Aucun commentaire:

Enregistrer un commentaire