I updated my Shiny library to version 1.1.0 and I noticed some very strange behavior with selectInput/selectizeInput and observeEvent/eventReactive.
The problem occurs when I press backspace and clear the contents of the drop-down menu. In the previous Shiny version the backspace coupled with a eventReactive, the reactive expression wouldn't evaluate (I guess it treated this as a NULL) observe and reactive would evaluate which is desired.
req() is also behaving weird, in Example 1 below if we press the backspace and clear the input, renderTable is triggered but when req(input$variable) is empty the table disappears. In the previous version if Shiny I believe the table would simply remain the same.
Reproducing Code:
Example 1
shinyApp(
ui = fluidPage(
selectizeInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
observeEvent(input$variable,{
cat("Printing: ",input$variable,"\n")
})
output$data <- renderTable({
req(input$variable)
Sys.sleep(2)
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
or
Example 2
This looks like okay behavior but if you notice the renderTable is still being called when the backspace is pressed. If this was an expensive computation it would be undesirable behavior.
shinyApp(
ui = fluidPage(
selectInput("variable", "Variable:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear")),
tableOutput("data")
),
server = function(input, output) {
observeEvent(input$variable,{
cat("Printing: ",input$variable,"\n")
})
output$data <- renderTable({
req(input$variable)
Sys.sleep(2)
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
My desired behavior: When the backspace is pressed to clear the menu observeEvents and eventReactive are not triggered.
It seems like the current behavior triggers an event on backspace but the input value remains the same. This behavior could actually be an unintended change that happenend when the JavaScript function Shiny.onInputChange was updated. The NEWS on shinys github site claims the following under Version 1.1.
NEW FEATURES
[...]
Introduced two changes to the (undocumented but widely used) JavaScript function Shiny.onInputChange(name, value). First, we changed the function name to Shiny.setInputValue (but don't worry--the old function name will continue to work). Second, until now, all calls to Shiny.onInputChange(inputId, value) have been "deduplicated"; that is, anytime an input is set to the same value it already has, the set is ignored. With Shiny v1.1, you can now add an options object as the third parameter: Shiny.setInputValue("name", value, {priority: "event"}). When the priority option is set to "event", Shiny will always send the value and trigger reactivity, whether it is a duplicate or not.
The current version of selectInput seems to take advantage of this new {priority: "event"} option but that is just speculation.
Workaround
You can adapt your server code to correctly handle this new behavior by deduping the inputs yourself.
dedupedValue <- reactiveVal()
observe({ dedupedValue(input$value) })
Then you use dedupedValue() instead of input$value in the rest of your server code. This will also work with older versions of shiny.
NOTE: If you try to use reactive instead of observe in the above code it will not work.
Long term solution
Maybe it would be best to set this question on hold until the shiny devs took a look at your GitHub issue. As outlined above, the cause of this is probably an interface change on the JavaScript side of shiny. If this indeed created code breaking changes, I am sure the devs will provide a fix to ensure backwards compability.
About req
This is basically unrelated to the issue at hand but came up with your question: If you want req to retrain the old output if the condition is not "truthy", you should call it as
req(condition, cancelOuput = TRUE)
Related
I have a shiny app that I am building where the user selects a report from a radio button menu, and then the table displays on the page. I would like to add an option for the user to simultaneously view all reports. I have found something close to what I want from this thread https://gist.github.com/wch/5436415/ , but I can't quite seem to implement it properly. Basically, I think what I have to do is:
In the UI, make a call to uiOutput() to reactively update the User Interface. I will need multiple calls to htmlOutput() if the user selects the "all" button, but only one call to htmlOutput() if the use simply selects one report. For the record, I am creating my tables with the kable() function, which is why I call htmlOutput() instead of tableOutput().
In the server function, I need to make a call to renderUI() that provides the instructions on how many htmlOutput() calls there will be and which reports will be in each call.
Create a loop that then makes a call to renderText that then sends the html code for the htmlOutput function to interpret.
I can get most of the way there. I can get Shiny to have reactive tables, and output individual reports, but I am struggling to get the looping range to reactively update so that I see all three tables in my testing app. Here is what I have:
library(shiny)
library(shinydashboard)
library(knitr)
library(kableExtra)
data("cars")
data("iris")
data("airquality")
UI<-dashboardPage(
dashboardHeader(),
dashboardSidebar(sidebarMenu(
menuItem("Options", radioButtons(inputId = "Tables", label="test", choices= c("cars", "iris", "airquality", "all"))
) )),
dashboardBody(
uiOutput(outputId = "TABLE"),
textOutput("N")
)
)
server<-function(input, output){
TBL<-list("cars", "iris", "airquality")
T1<-knitr::kable(head(cars))
T2<-knitr::kable(head(iris))
T3<-knitr::kable(head(airquality))
tmp<-list(cars=T1, iris=T2, airquality=T3)
TABLES<-reactive({
ifelse(input$Tables=="all", tmp, tmp[input$Tables])
})
val<-reactive({
tmp<-TABLES()
length(tmp)})
n<-isolate(val())
output$TABLE<-renderUI({
req(input$Tables)
TAB<-TABLES()
TBL<-names(TAB)
tbls<-lapply(1:length(TBL), function(i){
tblnm<-paste("tbl", i, sep="_")
htmlOutput(tblnm)})
do.call(tagList, tbls)
})#Close Render UI
for(i in 1:n){
local({
j<-i
tblnm<-paste("tbl", j, sep="_")
output[[tblnm]]<-renderText(kables(TABLES()))
})
}
output$N<-renderText(n)
}#Close Server
shinyApp(ui = UI, server = server)
Here is where I think I am going wrong:
I included a textOutput() for the value of n, and despite having the reactive() call to get the length of TABLES, when I isolate() I still get the value of 1 even when I select the "all" report button, which should give me 3. Am I misinterpreting how isolate() works? Is there another way to get a value out of a reactive() function that can be used outside of a *Output() or reactive() function? Any guidance would be much appreciated. Thanks so much.
I think your server function is needlessly complex.
render functions are a reactive context themselves, so no need to define variables which only exist in those contexts as specifically reactive.
server<-function(input, output){
TBL<-list("cars", "iris", "airquality")
T1<-knitr::kable(head(cars))
T2<-knitr::kable(head(iris))
T3<-knitr::kable(head(airquality))
tmp<-list(cars=T1, iris=T2, airquality=T3)
output$TABLE<-renderUI({
if(input$Tables=="all"){ind <- names(tmp)}else{ind <- input$Tables}
lapply(tmp, HTML)[ind]
})
output$N <- renderText({
if(input$Tables=="all"){ind <- names(tmp)}else{ind <- input$Tables}
length(ind)
})
}
I have the following app, which is just a collapse panel containing an rhandsontable table.
library(shiny)
library(shinyBS)
library(rhandsontable)
ui <- function() {
fluidPage(
bsCollapsePanel(
"Test",
rHandsontableOutput("table")
)
)
}
server <- function(input, output, session) {
output$table <- renderRHandsontable({
rhandsontable(
data.frame(
a = 1:2,
b = 2:3
)
)
})
}
shinyApp(ui, server)
It works as expected: the panel starts with its contents hidden, and if we click it the panel opens and we see the table.
However, there is a noticeable "lag" between the panel opening and the table appearing. I assume this is because the table hadn't been initialized until then, and so all the work actually creating the table only happens at that moment.
If we then close the panel and reopen it, there is no such lag and we can even gradually see the table as the panel reopens.
I don't know if this is a feature or a bug, or who's "fault" it is: rhandsontable, for being lazy in starting up? shinyBS, for being lazy starting its contents up? shiny in general, for only triggering redraws immediately when needed? I'd assume it's rhandsontable, since more basic elements like textInput() don't have this problem, but can't know for sure.
So, is there a way to force this initialization of the table when the app starts up, instead of only when the panel expands?
I've thought of setting the panel to start open and then hack the server to close the panel on startup, but I'm not entirely sure how that'd work... or if it'd even work (if it closes prior to the first redraw, what difference will it make? if it's after the first redraw, that'd imply a flicker on startup, right?).
I think this should do it:
server <- function(input, output, session) {
output$table <- renderRHandsontable({
rhandsontable(
data.frame(
a = 1:2,
b = 2:3
)
)
})
outputOptions(output, "table", suspendWhenHidden = FALSE)
}
Is there a way to check if a download button is disabled using the shinyjs R package? I want to use shinyjs or something similar because they have very easy syntax. This is my package:
server.R:
library(shiny)
library(shinyjs)
library(shinyBS)
shinyServer(function(input, output) {
observe({
shinyjs::disable("download1")
if(shinyjs::is.disabled("download1")){ ## This is what I am trying to do
# Do something
}
})
})
ui.R
shinyUI(fluidPage(
downloadButton("download1")
))
Not directly (well, not easily*).
Buttons can only be disabled when you decide to disable them, so you can have some sort of a reactive variable that holds whether or not the button should be disabled, and whenever you disable the button, you also change the value of that variable. In order to make sure they stay in sync, every time you want to disable the button you can set the variable to mirror that, and then you can use shinyjs::toggleState(condition = variable) so that the disabled state will mirror what the variable says.
Example code to illustrate what I mean:
library(shiny)
ui <- fluidPage(
shinyjs::useShinyjs(),
numericInput("num", "When divisible by 3, disable the button", 1),
actionButton("btn", "button")
)
server <- function(input, output, session) {
values <- reactiveValues(disable = FALSE)
observe({
values$disable <- (input$num %% 3 == 0)
})
observe({
shinyjs::toggleState("btn", condition = !values$disable)
})
}
shinyApp(ui = ui, server = server)
In this app, whenever you want to disable the button, simply set values$disable to FALSE and to enable the button set it to TRUE. To check if the button is currently on or off at any point in time, you can look at the value of values$disable.
*I'm guessing that you wanted a more direct approach to ask the app a question in real time "hey shiny, is button X currently disabled?". You can do that, but it would involve writing custom javascript code to check for the button state, and for custom code to ask javascript that question and to listen for its response. This would work and be guaranteed to be correct, but it's likely overkill for most cases.
I am trying to include a selectizeInput widget in a Shiny app. However, one aspect of its behavior is problematic: each time I make a selection, the box containing the choices closes.
I took a look at the example app here: http://shiny.rstudio.com/gallery/selectize-examples.html. Specifically, input number 2: Multi-select. The selection window remains open in this example, yet I see no differences between that code and mine which would account for the variance in behavior.
For the sake of a reproducible example, I have put together the following code:
ui <- fluidPage(uiOutput("example"))
server <- function(input, output, session){
output$example <- renderUI({
selectizeInput(
inputId="people",
label=NULL,
choices=paste("A", 1:50, sep="_"),
multiple = TRUE,
selected=input$people
)
})
} # close server
shinyApp(ui = ui, server=server)
My guess is that I'm missing something obvious, so here's a chance for an easy answer for someone that knows their way around Shiny. Any help will be greatly appreciated.
When you remove the selected=input$people line, it works as intended.
Note: After coming up with the answer I reworded the question to make if clearer.
Sometimes in a shiny app. I want to make use of a value selected by the user for a widget, as well as the previous value selected for that same widget. This could apply to reactive values derived from user input, where I want the old and the new value.
The problem is that if I try to save the value of a widget, then the variable containing that value has to be reactive or it will not update every time the widget changes. But, if I save the the value in a reactive context it will always give me the current value, not the previous one.
How can I save the previous value of a widget, but still have it update every time the user changes the widget?
Is there a way that does not require the use of an actionButton every time the user changes things? Avoiding an actionButton can be desirable with adding one is otherwise unnecessary and creates excess clicking for the user.
Seeing as the session flush event method seems to be broken for this purpose, here is an alternative way to do it using an observeEvent construct and a reactive variable.
library(shiny)
ui <- fluidPage(
h1("Memory"),
sidebarLayout(
sidebarPanel(
numericInput("val", "Next Value", 10)
),
mainPanel(
verbatimTextOutput("curval"),
verbatimTextOutput("lstval")
)
)
)
server <- function(input,output,session) {
rv <- reactiveValues(lstval=0,curval=0)
observeEvent(input$val, {rv$lstval <- rv$curval; rv$curval <- input$val})
curre <- reactive({req(input$val); input$val; rv$curval})
lstre <- reactive({req(input$val); input$val; rv$lstval})
output$curval <- renderPrint({sprintf("cur:%d",curre())})
output$lstval <- renderPrint({sprintf("lst:%d",lstre())})
}
options(shiny.reactlog = TRUE)
shinyApp(ui, server)
Yielding:
Update This answer was posted before the advent of the reactiveValues/observeEvent model in shiny. I think that #MikeWise 's answer is the better way to do this.
After some playing around this is what I came up with. The ui.r is nothing special
ui.r
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput(inputId="XX", label="Choose a letter",choices=letters[1:5])
),
mainPanel(
textOutput("Current"),
textOutput("old")
)
)
))
"Current" will display the current selection and "old" displays the previous selection.
In the server.r I made use of three key functions: reactiveValues, isolate and session$onFlush.
server.r
library(shiny)
server <- function(input, output,session) {
Values<-reactiveValues(old="Start")
session$onFlush(once=FALSE, function(){
isolate({ Values$old<-input$XX })
})
output$Current <- renderText({paste("Current:",input$XX)})
output$old <- renderText({ paste("Old:",Values$old) })
}
The server.r works like this.
First, Values$old is created using the reactiveValues function. I gave it the value "Start" to make it clear what was happening on load up.
Then I added a session$onFlush function. Note that I have session as an argument in my server function. This will run every time that shiny flushes the reactive system - such as when the selectizeInput is changed by the user. What is important is that it will run before input$XX gets a new value - so the value has changed at the selectizeInput but not at XX.
Inside the session$onFlush I then assign the outgoing value of XX to Values$old. This is done inside an isolate() as this will prevent any problems with input$XX gets updated with the new values. I can then use input$XX and Values$old in the renderText() functions.