I really need help with this issue. So, I'm trying to modulize an app, in the first module, I'm uploading two different files, in this case, observed data and simulated data. later on, on the next module (the plot module), they should create a plot on each other, so the observed data should be geom_point() and simulated data should be geom_line(). The datasets are connected to the plot module and can be selected with selectedInput() but when I try to plot them, I get "unused argument (list())", How can I fix this problem, I have tried different ways, but haven't solved it, so, I would be grateful if I can get some help here.
library(shiny)
library(ggplot2)
UploadUI <- function(id) {
ns <- NS(id)
tabPanel(
"Upload File",
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(ns("file1"), "Observed data",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"
)
),
fileInput(ns("file2"), "sim data",
accept = c(
"text/csv",
"text/comma-separated-values,text/plain",
".csv"
)
),
tags$br(),
checkboxInput(ns("header"), "Header", TRUE),
radioButtons(
ns("sep"),
"Separator",
c(
Comma = ",",
Semicolon = ";",
Tab = "\t"
),
","
),
radioButtons(
ns("quote"),
"Quote",
c(
None = "",
"Double Quote" = '"',
"Single Quote" = "'"
),
'"'
)
),
mainPanel(
tableOutput(ns("contents")),
br(),
tableOutput(ns("contents_2"))
)
)
)
}
UploadServer <- function(id) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath,
header = input$header, sep = input$sep,
quote = input$quote
)
return(df)
})
data_2 <- reactive({
req(input$file1)
inFile <- input$file1
df_2 <- read.csv(inFile$datapath,
header = input$header, sep = input$sep,
quote = input$quote
)
return(df_2)
})
output$contents <- renderTable({
data()
})
output$contents_2 <- renderTable({
data_2()
})
# return data
return(list(data, data_2))
}
)
}
###############################################
PlotUI <- function(id) {
ns <- NS(id)
tabPanel("Visualisation",
sidebarLayout(
sidebarPanel(
p("Observed data"),
fluidRow(
column(6,
selectInput(ns('obsxcol'), 'X Variable', "",width=140)
),
column(6,
selectInput(ns('obsycol'), 'Y Variable', "", selected = "",width=140)
)
),
p("Simulated data"),
fluidRow(
column(6,
selectInput(ns('simxcol1'), 'X Variable', "",width=140)
),
column(6,
selectInput(ns('simycol1'), 'Y Variable', "", selected = "",width=140),
)
),
fluidRow(
column(6,
checkboxInput(ns("logxobs"), "logX", FALSE, width=140)
),
column(6,
checkboxInput(ns("logyobs"), "logY", FALSE, width=140)
)
),
hr(),
fluidRow(
actionButton(ns("plot_vis_1"), "Plot!")
)
),
mainPanel(
plotOutput(ns('plotsim_1'))
)
)
)
}
PlotServer <- function(id, df, df_2) {
stopifnot(is.reactive(df))
stopifnot(is.reactive(df_2))
moduleServer(id, function(input, output, session) {
ns <- session$ns
observeEvent(df(),{
updateSelectInput(session, inputId = 'obsxcol', label ='X vairable',
choices = names(df()), selected = names(df()))
updateSelectInput(session, inputId = 'obsycol', label ='Y vairable',
choices = names(df()), selected = names(df())[2])
})
observeEvent(df_2(),{
updateSelectInput(session, inputId = 'simxcol1', label ='X vairable',
choices = names(df_2()), selected = names(df_2()))
updateSelectInput(session, inputId = 'simycol1', label ='Y vairable',
choices = names(df_2()), selected = names(df_2())[2])
})
plot_1<-reactive({
graph2 <- ggplot() +
geom_point(data = df, aes(.data[[input$obsxcol]], .data[[input$obsycol]]), size=1) +
geom_line(data = df_2, aes(.data[[input$simxcol1]]/60, .data[[input$simycol1]])) +
theme_bw()
if(input$logyobs){
graph2 <- graph2 + scale_y_log10()
}
if(input$logxobs){
graph2 <- graph2 + scale_x_log10()
}
graph2
}) |> bindEvent(input$plot_vis_1)
output$plotsim_1 <-renderPlot({
plot_1()
})
}
)
}
ui <- shinyUI(fluidPage(
titlePanel("Column Plot"),
tabsetPanel(
UploadUI("upload"),
PlotUI("plot")
)
))
server <- shinyServer(function(input, output, session) {
upload <- UploadServer("upload")
PlotServer("plot",df = upload[[1]], df_2=upload[[2]])
})
shinyApp(ui, server)