Ich habe diese Shiny Code, der derzeit über RStudio verfügbar ist. Ich erwarte, dass es die Benutzereingabe von Checkbox nimmt und übergebe den Wert an get_table()
Parameter logit
.Wie übergebe ich die Checkbox in eine Funktion in 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)
Derzeit sieht die Apps wie folgt aus:
Nun, wenn ich die Box überprüft, es nicht um den Parameter logit
Funktion nicht passieren. Wenn es den Wert funktioniert, wird die Tabelle protokolliert.
Meine Frage ist, wie kann ich den Checkbox-Wert an den Parameter in shinyServer()
übergeben? Ich habe versucht, diese
out_table <- get_table(dat,logit=input$checkbox, wanted_cols=c("model","mpg","wt"), data_title="Cool Table")
aber gab Fehler:
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.)
Dank Aufruf . Wie können wir wissen, wann 'reactive()' oder nicht in 'shinyServer()' zu verwenden ist? Jede Faustregel? – neversaint
@neversaint Sie verwenden reactive(), wenn Sie eine Ausgabe haben, die sich ändern sollte, wenn sich der Wert Ihres reaktiven Werts ändert. Auf der folgenden Seite finden Sie weitere Erklärungen zur Reaktivität: http://shiny.rstudio.com/articles/reactivity-overview.html –
Gibt es einen Fall, in dem 'checkboxInput()' nicht reaktiv ist? – neversaint