Hello and thanks for the help. I am trying to make an app that updates the selections of one pickerinput depending on the selections of another. The idea is that if one pickerinput has something selected then the other removes all current selections (and vice versa), but I get an error when trying to update the pickerinputs
My code is the following:
library(shiny)
library(shinywidgets)
var1 <- c(1,2,3,4,1,2,3)
var2 <- c(1,2,3,3,1,2,2,1,2,1)
shinyApp(
ui = fluidPage(
pickerInput("primero",
"opciones a escoger",
choices = var1,
selected = var1,
multiple = TRUE
),
pickerInput("segundo",
"opciones a escoger 2",
choices = var2,
multiple = TRUE
)
),
server = function(input, output, session){
valor <- reactive({
c( input$primero)
})
observeEvent(input$primero,{
req(valor())
updatePickerInput(session,"segundo",
choices = var2,
selected =
if_else(length(valor())>0, NULL, head(var2,1))
)
})
}
)
Error message:
Warning: Error in : `false` must be NULL, not a double vector.
[No stack trace available]
Any ideas for suggestions? Thanks a lot
if_else requires that the class/type of both its second and third arguments must be the same. Further, since it is intended to be a vectorized logical if/else, having one of them be NULL does not make sense.
Looking more at the code, you are using it incorrectly: do not assume that it (or base::ifelse) is a drop-in replacement for if/else, they are very much different. Replace your observeEvent with:
observeEvent(input$primero,{
req(valor())
updatePickerInput(session,"segundo",
choices = var2,
selected =
if (!length(valor()) > 0) head(var2, 1)
)
})
I'm shortcutting it slightly here: when we have if and no else, when the conditional evaluates to FALSE then it returns NULL, which is what I think your if_else(length(valor()) > 0, NULL, ..) meant.
Related
I am making a shiny dashboard and I have a dataframe I created with a year column, district column, and columns for several vaccines. I want the user to be able to choose a vaccine from a drop down list. Using the input the code would then create a subset of the dataframe to display. I first need to reshape it before I display it though.
----- This is the UI section
fluidRow(
selectInput(inputId = "Immunization", label = "Select group", c("FLU","HEPA","HEPB")),
DTOutput('AdultTable'))
),
----- This is the code I have currently in the Server section. When I try to run it, it tells me:
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 consumer.
65: <Anonymous>
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 consumer.
vaccine_sel = reactive(input$Immunization)
requested_table = (requested_table = subset(Imm, select = c("REPORT_YEAR", "DISTRICT_ID", req(vaccine_sel()))))
requested_table = reshape(requested_table, idvar = "DISTRICT_ID", timevar = "REPORT_YEAR", direction = "wide")
output$AdultTable = renderDT(
requested_table, options = list(lengthChange = FALSE, searching = FALSE, pageLength = 15, paging = FALSE, scrollX = TRUE)
)
----- And If I try putting this into a reactive() environment, it still gives me an error:
Warning: Error in reactive: ... must be empty.
✖ Problematic argument:
• requestedtable = subset(Imm, select = c("REPORTYEAR", "DISTRICT_ID", req(vaccine_sel())))
55: <Anonymous>
Error in reactive(requested_table = subset(Imm, select = c("REPORT_YEAR"
... must be empty.
✖ Problematic argument:
• requestedtable = subset(Imm, select = c("REPORTYEAR", "DISTRICT_ID", req(vaccine_sel())))
---- If I hardcode "FLU" into it then it works just fine.
requested_table = requested_table = subset(Imm, select = c("REPORT_YEAR", "DISTRICT_ID", "FLU"))
---- It will give me the 3 columns I want and then I can reshape from there.
This is a clip of what the data looks like before the user chooses the disease
This is what it would hopefully look like if it works
You tried to do something that can only be done from inside a reactive consumer.
... is a polite hint (OK: error) that you're trying to use a reactive component (in your case: input$Immunization) outside a, well, reactive consumer like observe - without which Shiny wouldn't listen to changes of that component.
You could try along the following lines:
library(shiny)
library(dplyr)
library(DT)
df <- data.frame(vaccine = gl(2, 4, labels = LETTERS[1:2]),
value = rnorm(8)
)
ui <- fluidPage(
selectInput('vaccine', label = 'choose vaccine', choices = c('A' , 'B')),
DTOutput('table')
)
server <- function(input, output, session) {
observe({
output$table <- renderDT(df |> filter(vaccine == input$vaccine))
}) |> bindEvent(input$vaccine)
}
if (interactive()) shinyApp(ui, server)
I have similar issue to R Shiny selectInput, I would like to use the input to slice my data like this:
selectInput("category", "Choose a Category:",
choices = c('Any', levels(as.factor(unique(BD$DX_01_Cat))))),
uiOutput("secondSelection")
if (input$category != "Any"){
subsetSubsTab <<- subsetSubsTab[subsetSubsTab$DX_01_Cat==input$category];
output$secondSelection <- renderUI({
selectInput("subdiagnosis", "Choose a Subdiagnosis:", choices = c("Any", as.character(subsetSubsTab[subsetSubsTab$DX_01_Cat==input$category, DX_01_Sub])) , selected = "Any")
})
if (input$subdiagnosis != "Any"){
subsetSubsTab <<- subsetSubsTab[subsetSubsTab$DX_01_Sub==input$subdiagnosis];
}
but the last if statement does not work.
I get warning Warning: Error in if: argument is of length zero,
the subsetSubsTab actualize for a second and goes back. Could someone help please?
How do I pass NULL as a Variable Value in RSHINY?
In phyloseq, there is a plot called plot_net.
The most basic plot_net plot code looks like this:
data(enterotype)
#Eliminate samples with no entereotype denomination
enterotype = subset_samples(enterotype, !is.na(Enterotype))
plot_net(enterotype, maxdist = 0.1, point_label = NULL)
I am trying to create an RShiny app which allows a user to alter this graphic.
point_label has several different options (ex: "SecTech", "SampleID", NULL).
I already have all of the other values for this label, I am just not sure how to add NULL.
Here is what I did:
This might not run since it isn't in a shiny app but I included it as an example to illustrate the issue.
library(shiny)
library(phyloseq)
# Data: This data contains info about nodes and edges on Phyloseq data.
data(enterotype)
#Eliminate samples with no entereotype denomination. Make it a lesson to
always catalogue data correctly from the start.
enterotype = subset_samples(enterotype, !is.na(Enterotype))
# a is the collection of variable names for point_label
a <- sample_variables(enterotype)
theme_set(theme_bw())
# Define UI for application that draws a network plot
shinyUI(fluidPage(
# Application title
titlePanel("Network Plots"),
sidebarLayout(
sidebarPanel(
selectInput("labelBy",
"Select the point label category",
***choices = c(a, "NA" = NULL),***
selected = "NA")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("netPlot")#,
#plotOutput("networkPlot")
)
)
))
shinyServer(function(input, output) {
output$netPlot <- renderPlot({
plot_net(enterotype, maxdist = .1, point_label = input$labelBy)
})
})
shinyApp(ui = ui, server = server)
This line is my question:
choices = c(a, "NA" = NULL)
How do I add NULL to my list of choices. No matter how I tried it, NULL was always taken as a zero value and it does not appear as an option.
If I write NULL as "NULL', the phyloseq function plot_net doesn't take it.
It only takes the value point_label = NULL for no value.
I think that it is possible to create an if... else loop where if a user clicks NULL on a checkboxInput then the plot will be generated by a second line of code specifying that the value in point_label is NULL, but that can be really cumbersome if there are several variables with a possible NULL Value.
There probably is some obvious trick like placing a $ or % in front of the NULL value but I couldn't find it. If anyone could help it would be great!
I don't think there is a way to use NULL in selectInput. Here's an alternative which you almost worked out - Use "None" (or any other replacement value) in selectInput and switch it with NULL while plotting. This way you don't have to write multiple if...else.
# update on UI side
selectInput("labelBy",
"Select the point label category",
choices = c("None", a),
selected = "None")
# update on server side
output$netPlot <- renderPlot({
point_labels <- switch(input$labelBy, "None" = NULL, input$labelBy)
plot_net(enterotype, maxdist = .1, point_label = point_labels)
})
I am trying to use the following paste as a condition in a conditional panel.
paste0("input.selection", input.Counter).
The condition is dynamic and depends mainly on the input Counter, which is an action button. Hence, I am trying to produce for example a string like
input.selection1
for input.Counter = 1. Since the conditions are already in quotes, I always get an error back. I would very much appreciate any help here. More precisely, I used the following code:
ui.R
library(shiny)
shinyUI(fluidPage(,
sidebarPanel(
conditionalPanel(paste0("input.selection", input.Counter), uiOutput("test")),
actionButton("Counter", "next")),
))
server.R
shinyServer(function(input, output, session) {
output$test <- renderUI({
radioButtons(paste0("choice", input$Counter), label = h4(""), choices = list("A" = 1, "B" = 2, "C" = 3), selected = FALSE)
})
})
I find RShiny is quite difficult to use as I can't have the control over my html, for instance,
I want to create a check box below,
<input type="checkbox" id="species" name="species_particles" value="particles"/>
in my shiny ui.R,
checkboxInput(inputId = "species",
label = "Particles",
value = "particles")
I get this error,
ERROR: invalid 'y' type in 'x && y'
I don't know what it means and how to fix it. And it does not make sense. what y? what x?
what about the name attribute? how can I put that in checkboxInput()?
The argument to value needs to be logical and determines whether the checkbox is selected by default or not. For example,
checkboxInput(inputId = "species",
label = "Particles",
value = TRUE)
See ?checkboxInput for details.
The thread is somewhat older, but as no answer has been accepted yet, in order to assign values to the checkboxes, simply use checkboxGroupInput instead of checkboxInput.
library("shiny")
runApp(list(
## ui.R
ui = shinyUI(
fluidPage(
checkboxGroupInput(
inputId = "species"
, label = "Particeles"
, choices = c("Quarks" = "Quarks", "Leptons" = "Leptons"
, "Antiquarks" = "Antileptons"))
, uiOutput("tmp")
)
)
## server.R
, server = function (input, output, session) {
output$tmp <- renderUI({
HTML(paste("Currently selected:",
paste(input$species, collapse = ", ")))
})
}
))