Hi,
I am trying to do some data manipulation after uploading the data file to the Shiny app. The app allows the user to choose from the variable names for the variable to be manipulated. Here I am just dividing the variable by 100. However, I get the error saying that the data cannot be displayed because the variable is non-numeric even though from the data structure I can see that the chosen variable is numeric.
Warning: Error in : error in evaluating the argument 'x' in selecting a method for function 'head': In argument:
potency = input$potencyx/100
.
Caused by error ininput$potencyx / 100
:
! non-numeric argument to binary operator
When I try enclosing the argument with as.numeric(), the dataset is displayed. However, all the values are NA.
I have no idea what went wrong... I am using an example dataset with numeric variables. Any test dataset I use seems to give the same error.
The code is as below:
library(shiny)
library(tidyverse)
ui <- fluidPage(
titlePanel("Potency vs Concentration"),
fluidRow(
# Accept CSV, XLSX, TSV as file uploads
column(6, fileInput("upload", NULL, accept = c(".csv",".xlsx",".tsv"))),
# "Empty inputs" - they will be updated after the data is uploaded
column(6, selectInput('potencyx', 'Potency', "", selected = "")),
),
fluidRow(
# Get numeric input of how many rows to display
column(6, numericInput("n", "Rows", value = 5, min = 1, step = 1)),
# "Empty inputs" - they will be updated after the data is uploaded
column(6, selectInput('concx', 'Concentration', "", selected = "")),
),
# Display first N rows
tableOutput("head"),
# Display data structure
verbatimTextOutput("str"),
# Display data after manipulation
tableOutput("head2")
)
server <- function(input, output, session) {
data <- reactive({
req(input$upload)
ext <- tools::file_ext(input$upload$name)
df <- switch(ext,
csv = vroom::vroom(input$upload$datapath, delim = ","),
tsv = vroom::vroom(input$upload$datapath, delim = "\t"),
xlsx = readxl::read_excel(input$upload$datapath),
validate("Invalid file; Please upload a .csv or .xlsx or .tsv file")
)
# Update inputs
updateVarSelectInput(session, inputId = 'potencyx', label = 'Potency',
data = df, selected=NULL)
updateVarSelectInput(session, inputId = 'concx', label = 'Concentration',
data = df, selected=NULL)
return(df)
})
# Show data before manipulation
output$head <- renderTable({
req(input$upload)
head(data(), input$n)
})
# Show data structure
output$str <- renderPrint({
req(input$upload)
str(data())
})
# Data manipulation
data2 <- reactive({
df2 <- data() |>
mutate(potency = as.numeric(input$potencyx)/100,
lnPotency = log(potency),
)
return(df2)
})
# Show data after manipulation
output$head2 <- renderTable({
req(input$upload,input$potencyx)
head(data2(), input$n)
})
}
shinyApp(ui, server)