How to use built-in data instead of uploaded data

时间:2017-08-30 20:23:44

标签: r shiny

I have an app like below. I want to either read-in data from file upload or use the built-in data. I thought I could put an action button and if somebody hit it the input data will mount on and it goes to the next levels. My problem is later in my real app, some widgets such as Date().timeIntervalSinceReferenceDate have to be updated and I want to be empty until user decided whether to use uploaded data or the built-in one.

DateComponents

1 个答案:

答案 0 :(得分:0)

这样的事情应该做:

library(shiny)

x <- mtcars 

ui <- fluidPage(
  fileInput(inputId = "uploadcsv", "", accept = '.csv'),
  actionButton(inputId = "a", label = "action button"),
  selectInput("select",label = h3("Select box"),choices = "",selected = 1)
)

server <- function(input, output, session) {

  data <- reactive({
    infile <- input$uploadcsv

    if (is.null(infile))
      return(NULL)

    read.csv(infile$datapath, header = TRUE, sep = ",")
  })

  v <- reactiveValues()
  v$DataToUse <- NULL

  observeEvent(input$uploadcsv,{
    if(!is.null(input$uploadcsv)){
      v$DataToUse <- data()
    }
  })

  observeEvent(input$a,v$DataToUse <- x)

  observeEvent(v$DataToUse,{
    req(v$DataToUse)
    if (max(v$DataToUse$cyl) %% 4 == 0){
      numberofinterval <- max(v$DataToUse$cyl) %/% 4
    } else {
      numberofinterval <- (max(v$DataToUse$cyl) %/% 4)+1
    }

    NumPeriod <- seq(0, numberofinterval)

    updateSelectInput(session, inputId = "select",
                      choices = NumPeriod,
                      selected = NumPeriod)
  })

}
shinyApp(ui = ui, server = server)

enter image description here