It is my first time uploading anything or asking anything.
I am new to R Shiny. I need to build a Shiny website where I will upload a time-series CSV file and do some simple graphing.
For the X variable, I want the Date column to be used.
The Date column is in character, so I tried to change it to DateTime type in R.
At the same time, I wanted to select the range of the data from the Date column and use that for plotting.
I tried to subset the dataset.
I am getting an error that says "getdate" cannot be found. "getdata" is a reactive function.
library(shiny)
library(ggplot2)
ui <- (fixedPage(
headerPanel(title = "Covid-19 Data of MA"),
titlePanel("File Input"),
sidebarLayout(
sidebarPanel(
fileInput("csvfile","Upload the file", multiple = FALSE,
accept = c("text/csv","text/comma-separated-values","text/plain",".csv"),
width = NULL, buttonLabel = "Browse...",
placeholder = "No file selected"),
helpText("Default max. file size is 5MB"),
tags$hr(),
h5(helpText("Select the read.table parameters below")),
checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
checkboxInput(inputId = "stringAsFactors", "String As Factors", value = FALSE),
br(),
radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='', Space=''), selected = ','),
width = 3
),
mainPanel(width = 9,
tabsetPanel(
tabPanel("About File", tableOutput("filedf")),
tabPanel("Data", tableOutput("table")),
tabPanel("Interactive Graph",
fluidRow(
selectInput(inputId = "column_name",
label = "Choose a X Variable:",
choices=NULL,
selected=NULL,
multiple = FALSE,
width="450px"),
dateRangeInput("date-range-input",label="Select Date Range for the Y Variable",
start = min(getdata()$Date),
end = max(getdata()$Date),
min = min(getdata()$Date),
max = max(getdata()$Date),
format = "yyyy-mm-dd",
separator = "-"
)
),
plotOutput("graph"))),
tableOutput("modified_dset"),
uiOutput("tb")
)
)
))
# ________________________________________________________________________________________________________________________________________
server <- (function(session, input, output){
getdata <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
})
# format="%Y-%m-%d"
modified_dset <- eventReactive(input$date-range-input, {
getdata()[, input$date-range-input] <- lubridate::date_decimal(getdata()[,input$date-range-input])
})
# Update SelectInput Dynamically
observe({
updateSelectInput(session, "column_name", label="Choose a X Variable:", choices = names(getdata()),selected=NULL)
})
# Data file info
output$filedf <- renderTable({
if(is.null(modified_dset())){return ()}
input$csvfile
})
# Interactive Graph
output$graph <- renderPlot({
if(is.null(modified_dset())){return ()}
dataset <- modified_dset()
subset_data = subset(dataset, dataset$Date>=input$date-range-input[1] & dataset$Date<=input$date-range-input[2])
ggplot(data = subset_data, aes_string(x = Date, y = input$column_name, group = 1))+
geom_line(col = "blue", lwd =1)+labs(x = "Date", y = input$column_name)
})
# Table of the Dataset
output$table <- renderTable({
if(is.null(modified_dset())){return ()}
getdata()
})
})
shinyApp(ui = ui, server = server)
question from:
https://stackoverflow.com/questions/65839539/problem-with-reactive-function-trying-to-change-the-type-of-the-column-of-a-dat