Error in If/While (Condition):Argument Is Not Interpretable as Logical

Why is my 'if' argument not interpretable as logical

We can use a & condition in filter

library(dplyr)
firsttwopitches %>%
filter(AtBatPitchSequence == 1, PitchType == "FA")

If we want to keep rows where 'AtBatPitchSequence' is not equal to 1, then add another expression with |

firsttwopitches %>% 
filter((AtBatPitchSequence == 1 & PitchType == "FA")|AtBatPitchSequence != 1)

There are two issues - 1) if/else are not vectorized, 2) related to the blocking of the code with {} especially when it is used in a pipe (%>%). A related issue is also in finding the column name AtBatPitchSequence outside the tidyverse functions i.e mutate, summarise etc. In that case we need to specify the data as well .$AtBatPitchSequence


The error/warning can be reproduced with the inbuilt dataset

data(iris)
head(iris) %>%
if(Species == 'setosa') {
filter(Petal.Length > 1.5)
}

Error in if (.) Species == "setosa" else { :
argument is not interpretable as logical
In addition: Warning message:
In if (.) Species == "setosa" else { :
the condition has length > 1 and only the first element will be used

Now, we can remove the error by blocking within {}, but note that the warning remains as if/else is not vectorized and this could give an incorrect output as well (Below output is correct, but it is only because there was only one row with the TRUE condition met)

head(iris) %>% 
{if(.$Species == 'setosa') {
filter(., Petal.Length > 1.5)
}}
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.4 3.9 1.7 0.4 setosa

Warning message:
In if (.$Species == "setosa") { :
the condition has length > 1 and only the first element will be used

If we use multiple expressions in filter (, will generate the &)

head(iris) %>% 
filter(Species == 'setosa', Petal.Length > 1.5)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1 5.4 3.9 1.7 0.4 setosa

Error in if/while (condition) {: missing Value where TRUE/FALSE needed

The evaluation of condition resulted in an NA. The if conditional must have either a TRUE or FALSE result.

if (NA) {}
## Error in if (NA) { : missing value where TRUE/FALSE needed

This can happen accidentally as the results of calculations:

if(TRUE && sqrt(-1)) {}
## Error in if (TRUE && sqrt(-1)) { : missing value where TRUE/FALSE needed

To test whether an object is missing use is.na(x) rather than x == NA.


See also the related errors:

Error in if/while (condition) { : argument is of length zero

Error in if/while (condition) : argument is not interpretable as logical

if (NULL) {}
## Error in if (NULL) { : argument is of length zero

if ("not logical") {}
## Error: argument is not interpretable as logical

if (c(TRUE, FALSE)) {}
## Warning message:
## the condition has length > 1 and only the first element will be used

Error code with Data table output: argument not interpretable as logical

I've fixed the logical issue, dataTableOutput() was correct, the issue arose with the inability to correctly access the Googlesheet title, I am able to read it now, just by listing the proper name of the specific sheet. Once I provided access for Googlesheets to Shiny to my private sheet it was able to work. Make sure gs_title() has the correct sheet name (title) in it!

R Shiny renderPlotly with multiple conditions

Try this

library(shiny)
library(tidyverse)
library(plotly)
# Define UI for application that draws an interactive ggplot
options(shiny.maxRequestSize=1024*1024^2)
ui = navbarPage("Data",
tabPanel("Uploading Files",
sidebarLayout(
sidebarPanel(
radioButtons(
"File_Type",
label = "Choose File type",
choices = list(".csv/txt" = 1, ".xlsx" = 2),
selected = 1,
inline = TRUE
),

fileInput('file2', 'Upload Your Data',
accept = c(
'text/csv',
'text/comma-separated-values,text/plain',
'.csv',
'.xlsx'
))),
mainPanel(
verbatimTextOutput("summary")
)

)),

tabPanel("Data Plots",
sidebarLayout(sidebarPanel(
selectInput("x", label = "Select x-axis Variable:",
choices = NULL),
selectInput("y", label = "Select y-axis Variable: ",
choices = NULL),
selectInput("color", label = "Select Grouping variable (optional): ",
choices = NULL)),
mainPanel(
plotlyOutput("TSM", height = 500)
)))
)

# Tell the server how to assemble inputs into outputs
server = function(input, output, session) {
# Code for uploading an MS-Excel file for plotting
# Get the uploaded file
myData = reactive({
req(input$file2)
inFile = input$file2
if (is.null(inFile)) return(NULL)
data = read.csv(inFile$datapath, header = TRUE)
data
})

observe({
data = req(myData())
updateSelectInput(session, 'x', choices = names(data), select=names(data)[3])
updateSelectInput(session, 'y', choices = names(data), select=names(data)[1])
updateSelectInput(session, 'color', choices = names(data), select=names(data)[4])
})

output$summary = renderPrint({
req(myData())
summary(myData())
})

df2 = reactive({
req(myData(),input$x, input$y)
myData() %>% dplyr::filter(Variable %in% input$y) %>%
group_by(.data[[input$x]], .data[[input$y]], .data[[input$color]]) %>%
dplyr::mutate(AvgTemp = mean(.data[[input$y]]))
})
observe({print(df2())})
#TSM
output$TSM = renderPlotly({
req(df2(), input$x, input$y, input$color)

df2() %>%
ggplot(aes(x = .data[[input$x]], y = AvgTemp, color = as.factor(.data[[input$color]]) )) +
labs(color = "Legend") +
geom_line() +
ggtitle("Timeseries Plot By Month")

})

}

# Run the application
shinyApp(ui = ui, server = server)

output



Related Topics



Leave a reply



Submit