The title practically states most of the issue.
I have a data.frame named df. In regular non-shiny non-reactive environment, the following code works -
df[,1] <- as.factor(df[,1])
However, in shiny environment (written below) I get this [ERROR: cannot xtfrm data frames]
library(shiny)
library(shinydashboard)
library(tidyverse)
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
fileInput("upload", NULL, accept = c("text/csv", "text/comma-separated-values,text/plain", ".csv")),
plotOutput("plot")
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output, session){
datable <- reactiveVal()
observeEvent(input$upload,{
datable(
vroom::vroom(input$upload$datapath, delim=",")
)
}
)
plotable <- reactive({
req(datable())
df <- datable()
df[,1] <- as.factor(df[,1])
df2 <- gather(df, key = "group", value = "values", -names(df)[1])
df2$proportions <- df2$values / ave(df2$values, df2$group, FUN = sum)
ggbarplot(data=df2,
x = "group",
y = "proportions",
fill = names(df2)[1],
facet.by = names(df2)[1],
color = "white"
)+
xlab("")+
ylab("")+
theme(legend.position="none")
})
output$plot <- renderPlot(plotable())
}
shinyApp(ui, server)
Is there a way to solve this? Thank you in advance.