2017-04-01 44 views
1

我有這個Shiny代碼,目前可通過RStudio部署。 我期望它從複選框中接受用戶輸入並將值 傳遞給get_table()參數logit如何通過複選框輸入到一個函數shinyServer()

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 <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv" 
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) 

目前的應用程序是這樣的:

enter image description here

現在,當我檢查了盒子,它不會將參數傳遞到logit功能。 如果它工作的價值,它的表將是日誌。

我的問題是如何將複選框的值傳遞給參數shinyServer()?我想這

​​

但給了錯誤:

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.) 

回答

1

你在輸出中存儲了一些東西(output$value <- renderPrint(...)),但你從來沒有真正把這個輸出放到你的UI中。如果您將主面板更改爲:

mainPanel(
     DT::dataTableOutput('myplot_table'), 
     verbatimTextOutput('value') 
    ) 

至少您會看到顯示的值。這一個是TRUEFALSE

你的第二個錯誤是忘記你的out_table應該是被動的。否則,它只會計算一次,在應用程序初始化時。

所以在您的服務器上更改分配:

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

注意:您可以通過調用它作爲一個功能(即在調用datatableout_table()

+0

感謝得到的反應值的值。我們如何知道何時在'shinyServer()'中使用'reactive()'?任何經驗法則? – neversaint

+1

@neversaint你使用reactive(),如果你的輸出值應該隨着你的反應值的變化而改變。你可以在下面的頁面找到更多關於實際反應性的解釋:http://shiny.rstudio.com/articles/reactivity-overview.html –

+0

有沒有一種情況'checkboxInput()'沒有反應? – neversaint

1

你幾乎沒有!簡單地讓它反應,這樣它會根據它的反應值(複選框)改變:

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

output$myplot_table使用out_table()代替out_table