Error in match(el, set, 0L) : 'match' requires vector arguments? - r

In running the following code in Shiny in R :
client_report_type = reactive({ input$report_type })
if ( is.element(client_report_type,"Enterprise_user"))
...
I encountered the following error message:
Error in match(el, set, 0L) : 'match' requires vector arguments
Does anyone know what does it mean, and how to resolve the problem?
Thanks!

You don't need to put an input inside a reactive to get the value, but the input should be inside of a reactive expression. Anything outside a reactive expression will be execute only once when the shiny app starts. And if you try to use an input value outside a reactive expression there will be an error. Depending of what you are going to do with input$report_type you can put it in a reactive (of course), observe, or observeEvent.
Here are some basic examples:
reactive:
dat <- reactive({
if ( is.element(input$report_type,"Enterprise_user")) {
...
myData
} else {
NULL
}
})
observe:
observe({
if (is.null(input$report_type))
return()
if ( is.element(input$report_type,"Enterprise_user"))
...
})
observeEvent:
observeEvent(input$report_type, {
if ( is.element(input$report_type,"Enterprise_user"))
...
})
Here is great tutorial about shiny and reactivity: http://deanattali.com/blog/building-shiny-apps-tutorial/#reactivity-101

Related

In R Shiny how do I check the value of a reactive and print a message

I am trying to figure out how to debug in Shiny. I would like to print a message to the console if a reactive takes a specific value. I know I can put a message inside of the reactive but I am curious to know if/how I do it from outside of the of reactive call it self. I tried to use an observeEvent() and it seems to trigger every time the reactive changes. What is the right way to print a message based on the value in a reactive?
library(shiny)
ui <- fluidPage(
textInput("name_first", "What's your first name?"),
textInput("name_last", "What's your last name?"),
textOutput("greeting")
)
server <- function(input, output, server) {
dude <- reactive(paste(input$name_first, input$name_last))
# why does this not work?
observeEvent(dude() == "a zombie", {message("run")})
output$greeting <- renderText(dude())
}
shinyApp(ui, server)
You can use req() to trigger an event only if a reactive has a certain value.
observeEvent(dude(),{
req(dude() == "a zombie")
message("run")
})
For debugging shiny apps in general, you can have a look at the browser() function
A possibility:
isZombie <- reactive({
if(dude() == "a zombie") TRUE # no need of 'else NULL'
})
observeEvent(isZombie(), {message("run")})

why the R while loops can't implement with shiny frame work?

I am trying to create a simple text listing UI on the shiny dashboard. For this, I made an R function, which needs to store each text inputs to store in a list.
readtasks <- function() {
df <- list()
while(TRUE) {
tasks <- readline(prompt="type your tasks: ")
# stop reading if no text was typed in
if (tasks == '')
break
# add the read data to the bottom of the list
df <- append(df,tasks)
}
print(df)
}
The function works g,ood for my needs, but when it comes to shiny it shows so many errors. I am eagerly want to know, how this while loop can efficiently implement to shiny. I search a lot in google and StackOverflow about this.if possible please give me an example.
Thank you
I try to made a shiny app with the above function
ui<- fluidPage(
textInput(inputId = "text","add"),
actionButton("button",label = "enter"),
verbatimTextOutput("list")
)
server<- function(input,output,session){
df<- list()
while(TRUE) {
tasks <- input$text
# stop reading if no year was typed in
if (tasks == '')
break
# add the read data to the bottom of the dataframe
df <- append(df,tasks)
output$list<- renderprint({df})
}
}
shinyApp(ui = ui,server = server)
but it crashes with the following error
Listening on http://127.0.0.1:7598
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.)
61: stop
60: .getReactiveEnvironment()$currentContext
59: getCurrentContext
55: .subset2(x, "impl")$get
54: $.reactivevalues
52: server [#6]
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.)
It is really helpfull to know to execute the functions with while loops
Once again thank you

conditional for multiple events to trigger eventReactive in R Shiny [duplicate]

I want two different events to trigger an update of the data being used by various plots / outputs in my app. One is a button being clicked (input$spec_button) and the other is a point on a dot being clicked (mainplot.click$click).
Basically, I want to listed for both at the same time, but I'm not sure how to write the code. Here's what I have now:
in server.R:
data <- eventReactive({mainplot.click$click | input$spec_button}, {
if(input$spec_button){
# get data relevant to the button
} else {
# get data relevant to the point clicked
}
})
But the if-else clause doesn't work
Error in mainplot.click$click | input$spec_button :
operations are possible only for numeric, logical or complex types
--> Is there some sort of action-combiner function I can use for the mainplot.click$click | input$spec_button clause?
I know this is old, but I had the same question. I finally figured it out. You include an expression in braces and simply list the events / reactive objects. My (unsubstantiated) guess is that shiny simply performs the same reactive pointer analysis to this expression block as to a standard reactive block.
observeEvent({
input$spec_button
mainplot.click$click
1
}, { ... } )
EDIT
Updated to handle the case where the last line in the expression returns NULL. Simply return a constant value.
Also:
observeEvent(c(
input$spec_button,
mainplot.click$click
), { ... } )
I've solved this issue with creating a reactive object and use it in event change expression. As below:
xxchange <- reactive({
paste(input$filter , input$term)
})
output$mypotput <- eventReactive( xxchange(), {
...
...
...
} )
Here's the solution I came up with: basically, create an empty reactiveValues data holder, and then modify its values based on two separate observeEvent instances.
data <- reactiveValues()
observeEvent(input$spec_button, {
data$data <- get.focus.spec(input=input, premise=premise,
itemname=input$dropdown.itemname, spec.info=spec.info)
})
observeEvent(mainplot.click$click, {
data$data <- get.focus.spec(input=input, premise=premise, mainplot=mainplot(),
mainplot.click_focus=mainplot.click_focus(),
spec.info=spec.info)
})
This can still be done with eventReactive by putting your actions in a vector.
eventReactive(
c(input$spec_button, mainplot.click$click),
{ ... } )
The idea here is to create a reactive function which will execute the condition you want to pass in observeEvent and then you can pass this reactive function to check the validity of the statement. For instance:
validate_event <- reactive({
# I have used OR condition here, you can use AND also
req(input$spec_button) || req(mainplot.click$click)
})
observeEvent(validate_event(),
{ ... }
)
Keep Coding!

Error in .func() : object 'input' not found shiny

I want to plot something with renderDataTable. Thats how I send it to the ui.R
output$myTable <- renderDataTable(myTableSelection())
Thats how the ui.R recieves it:
... tabPanel("Title", value = 4, dataTableOutput("myTable ")))), ...
Before the table is rendered, some preprocessing of the data is necessary.
I keep getting this errormessage:
Error in .func() : object 'input' not found
To narrow down the error I kept commenting out lines that process the dataframe before sending it to renderDataTable.
I narrowed it down to these lines:
someName <- eventReactive(input$someInputA, {
if( (as.integer(input$someInputB) == 2) ) {
return(someDf[someDf$someColumn %in% input$someInputA ,])
} else {
print("Whatever")
}
})
Everywhere else in my code it works when I refer to a variable from the ui.R with input$whatever, but not here.
When I print the two inputs document with the code that wont execute like so
observe({
print(input$someInputA)
print(input$someInputB)
})
It gets printed properly.
I even tried to assign input$... to some variables in global.R but that did not help neither. Any ideas why?!

how to save the entered text in a shiny app

i want to know how to save the entered text by user in a shiny app to use it in the server side.
i want to check if the value that the user has entered is valid or not
in the user side there is this textInput:
textInput("entity1", "Enter a keyword")
and in the server side i want to check the value of the user using this code:
entity1 <- reactive({
if(input$actb >= 0 ){
withProgress(session, min=1, max=15, expr={
for(i in 1:15) {
setProgress(message = 'please wait',detail = 'it may take some time',value=i)
Sys.sleep(0.1)
}
})}
smallE= "[a-z]"
keyword = as.character(input$entity1)
if(match(input$entity1, smallE))
{
message("sorry you did not enter a valid keyword. please try again")
Sys.sleep(1)
}
else
entity1 <- readTweets()
})
I have tried to declare a global variable in the server side to save the input:
if(match(as.vector(userInput), smallE))
userInput is a global variable contains the value of entity1
but there is a error that keep showing saying that:
Error in as.vector(x, mode) :
cannot coerce type 'closure' to vector of type 'any'
any suggestions that may help me?
It is hard to tease apart with just this chunk of code. I would avoid calling out any globals, however, you can assign NULL values until a user has specified input values.
You are also calling your object and your reactive functions both "entity1", so that is a little confusing.
One other thing is that you define the object "keyword" but then never use it again.
And I think you might do better using %in% as your binary rather than match().
Not sure if any of this helps...but the error you are seeing is specifically with the as.vector() part of your code. I am not even sure why you are using that, as any string in the textInput field will come in as a vector already.
There exists a validate function, such as in this example:
validate(
need(input$searchTerm != "", "Please, enter the correct search term")
)

Resources