Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

Problem with reactive function, trying to change the type of the column of a dataset that is being imported (R Shiny)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is that you are trying to call a reactive inside your ui, which should essentially be static. All reactives must stay within server. Your error lies in using

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 = "-"
                                  )

within ui. The solution is to add this to server

output$myDateInput <- renderUI({
   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 = "-"
                                  )
)}

Then remove that dateRangeInput from your ui code and replace it with:

uiOutput("myDateInput")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...