使用selectInput选项作为x和y绘图变量

时间:2016-06-27 18:35:56

标签: r excel shiny

library(shiny)
library(xlsx)

shinyUI(fluidPage(

 titlePanel("To-Fest"),
 sidebarLayout(
  sidebarPanel(
  fileInput('file1', 'Choose File', accept = ".xlsx"),

  #downloadButton('downloadData', 'Download'),
  tags$hr(),
  checkboxInput('header', 'Header', TRUE),
  tags$hr(),
  selectInput("select1", "Select X Variable", choices =c()),
  tags$hr(),
  selectInput("select2", "Select Y Variable", choices =c()),

  uiOutput("choose_columns")

  ),
    mainPanel(

    tabsetPanel(
      tabPanel("Data", tableOutput('contents')),
     tabPanel("Plot", plotOutput('plot1'))

    )
  )
  )
))
  library(shiny)
 library(xlsx)

shinyServer(function(input, output, session) {
  dsnames <- c()

 rawData <- reactive({
   filet <- input$file1
   if(is.null(filet)) return()
   rawData <- read.xlsx2(filet$datapath, sheetIndex = 1)
 })

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

 observe({
  dsnames <- names(rawData())
  cb_options <- list()
   cb_options[ dsnames] <- dsnames
  updateSelectInput(session, "select1",
                         label = "Select x-axis",
                         choices = cb_options,
                         selected = "")
  updateSelectInput(session, "select2",
                  label = "Select y-axis",
                  choices = cb_options,
                  selected = "")
 })

output$choose_dataset <- renderUI({
  selectInput("dataset", "Data set", as.list(dsnames))
})

 output$choose_columns <- renderUI({

  if(is.null(input$dataset))
  return()

  colnames <- names(contents)

  selectInput("columns", "Choose columns", 
                   choices  = colnames,
                   selected = NULL)
})

x <- reactive({
  xval <- rawData(input$select1)
  xval
})

 y <- reactive({
  yval <- rawData(input$select2)
yval
 })

 output$plot1 <- renderPlot({
  plot(x,y)
})

})

这些是我的ui和服务器文件,我正在尝试创建一个应用程序,您可以在其中上传Excel文件并使用列标题作为两个selectInput小部件中列出的选项。我已经让这部分工作了,但后来我想把两个选项相互映射为X和Y变量。我尝试为x和y制作反应函数但是我没有成功地使它工作。我也一直收到错误:

 cannot coerce type 'closure' to vector of type 'double'.

0 个答案:

没有答案