1
votes
library(shiny)
library(shinydashboard)
library(leaflet)
library(data.table)
library(ggplot2)
library(usl)

ui <- pageWithSidebar(
  headerPanel("CSV Viewer"),
  sidebarPanel(
    fileInput('file1', 'Choose CSV File',
              accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
    tags$hr(),
    checkboxInput('header', 'Header', TRUE),
    fluidRow(
      column(6,radioButtons("xaxisGrp","X-Axis:", c("1"="1","2"="2"))),
      column(6,checkboxGroupInput("yaxisGrp","Y-axis:", c("1"="1","2"="2")))
    ),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';',Tab='\t'), ','),
    radioButtons('quote', 'Quote',
                 c(None='','Double Quote'='"','Single Quote'="'"),'"'),
    uiOutput("choose_columns")
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Data", tableOutput('contents')),
      tabPanel("Plot",plotOutput("plot")),
      tabPanel("Summary",uiOutput("summary"))

    )
  )
)


####server

server <- function(input, output,session) {
  dsnames <- c()
  u<-
  data_set <- reactive({
    inFile <- input$file1
    data(specsdm91)
    if (is.null(inFile))
      return(specsdm91)

    data_set<-read.csv(inFile$datapath, header=input$header, 
                       sep=input$sep, quote=input$quote)
  })

  output$contents <- renderTable({data_set()})

  observe({
    dsnames <- names(data_set())
    cb_options <- list()
    cb_options[ dsnames] <- dsnames
    updateRadioButtons(session, "xaxisGrp",
                       label = "X-Axis",
                       choices = cb_options,
                       selected = "")
    updateCheckboxGroupInput(session, "yaxisGrp",
                             label = "Y-Axis",
                             choices = cb_options,
                             selected = "")
  })
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })

  usl.model <- reactive({

      df <- data_set()
     # print(df)
      df2 <- df[,c(input$xaxisGrp, input$yaxisGrp)]

      #gp <- NULL
      if (!is.null(df)){

        xv <- input$xaxisGrp
        yv <- input$yaxisGrp
        print(xv)
        print(yv)
        if (!is.null(xv) & !is.null(yv)){

          if (sum(xv %in% names(df))>0){ # supress error when changing files

            usl.model <- usl(as.formula(paste(yv, '~', xv)), data = df)
           return(usl.model())

          }
        }
      }
      #return(gp)
    }
  )


  ##plot
  output$plot = renderPlot({

   plot(usl.model())

  } )

  ##
 # output$summary <- renderUI({

  #  summary(usl.model())

  #}) 

  ##

  output$choose_columns <- renderUI({

    if(is.null(input$dataset))
      return()
    colnames <- names(contents)
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  }) 
}
shinyApp(ui, server)

As you can see I have the df printed. Any ideas?

1

1 Answers

3
votes

EDIT: Please give a reproducible example of your data. Without that, it's hard to imagine what the data types are. In general, I think you have potentially two problems:

  1. What is gp? You need to return not gp but usl.model
  2. Your renderUI is not passing a UI object (i.e. not something the output function will know what to do with).

Your problem is that usl.model is not actually being saved anywhere, because it's being called within renderPlot, which returns only the plot. Since you want usl.model to be consumed by two functions, you should take one of the following approaches.

Approach 1:

Define a reactive function for usl.model and reference it in your two output functions.

usl.model <- reactive({
    # some calculations probably ending in
    usl.model <- usl(as.formula(paste(yv, '~', xv)), data = df)
    usl.model
})

This allows you to reference your model output as usl.model() (the parenthesis are important!), e.g.

output$plot <- renderPlot( plot(usl.model(), add=TRUE) )

Approach 2

Create a reactiveValues() variable to store your usl.model as calculated in the plot function.

usl.model <- reactiveValues(data = NULL)

output$plot = renderPlot({
    # some calculations probably ending in
    usl.model$data <- usl(as.formula(paste(yv, '~', xv)), data = df)
})

You can then refer to your model output anywhere as usl.model$data

Approach two is possibly worse because it requires the plot function to be run first.