我有這個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)
目前的應用程序是這樣的:
現在,當我檢查了盒子,它不會將參數傳遞到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.)
感謝得到的反應值的值。我們如何知道何時在'shinyServer()'中使用'reactive()'?任何經驗法則? – neversaint
@neversaint你使用reactive(),如果你的輸出值應該隨着你的反應值的變化而改變。你可以在下面的頁面找到更多關於實際反應性的解釋:http://shiny.rstudio.com/articles/reactivity-overview.html –
有沒有一種情況'checkboxInput()'沒有反應? – neversaint