解密(x)中的R Shiny Error:无法使用dplyr过滤器函数对环境进行分类

问题描述 投票:2回答:1

我试图过滤反应函数中的数据集,以便我可以上下移动滑块并更改数据集的大小。

我使用名为“sliderfordata”的滑块作为名为“yeardata”的反应函数的输入,以便我将该反应函数用作输出$ chart的数据输入。我正在使用的数据集是mtcars。

错误消息:: unclass(x)中的错误:无法取消环境

当我选择要作为数据上传的文件时会发生这种情况,该文件映射到反应func,从文件路径读取数据文件。

任何帮助表示赞赏!

服务器代码

library("dplyr")
server <- function(input, output, session) {


  yeardata <- reactive({
    mtcarslist <- reactivefunc()
    df <- mtcarslist %>%
      filter(mpg >= input$sliderfordata)


  })
  output$chart <- reactive({

    dataframe <- yeardata()
    gggraph <-
      ggplot(dataframe, aes(x = mpg , y = hp))
    gggraph <- gggraph + geom_point()
    gggraph
  })



  reactivefunc <- reactive(
    csv <- read.csv(input$file$datapath)
  )


  # scatter plot the mtcars dataset - mpg vs hp
  output$graph <- renderPlot({
    # filename = input$file$datapath
    # csv <- read.csv(filename)

    # If more than 1 category, you can do this (put in a if state)
    # If filepath is put in
    csvread <- reactivefunc()
    x_axis <- input$xaxis
    y_axis <- input$yaxis
    rangemin <- as.numeric(input$val1)
    rangemax <- as.numeric(input$val2)

    # csv <-
    #   csvread %>%
    #   filter(
    #     x_axis <- csvread$x_axis < as.numeric(input$val2)
    #     # y_axis <- csvread$y_axis < as.numeric(input$val2)
    #   )

    size <- input$size
    color <- input$color
    gg <-
      ggplot(reactivefunc(), aes_string(x = x_axis, y = y_axis, size = size, colour = color))
    gg <- gg + geom_point()
    gg
  })

  output$hist <- renderPlot({
      x_axis <- input$xaxis
      csvread <- reactivefunc()


      ggplot(reactivefunc(), aes_string(x= x_axis)) + geom_histogram(bins = input$bins)

  })

  # To display the mtcars dataset on the left side in the app
  output$data <- renderTable({
    reactivefunc()
  })


  output$mytable = DT::renderDataTable(reactivefunc(), selection = list(target = 'row+column'))


  # brushedPoints returns the row of data under the brush
  # brush information comes from ui to server using brushId and can be accessed using input$brushId

  output$data_brush <-  renderTable({
    n = nrow(brushedPoints(reactivefunc(), brush = input$plot_brush)) # row count will be 0 when no selection made by the brush
    if(n==0)  
      return()
    else
      brushedPoints(reactivefunc(), brush = input$plot_brush) # return rows
    # argument allRows = TRUE can also be used
    ## It will add another column (selected_) to the actual dataset. True indicates that data point 
    # corresponding to that row was under the brush. False means data corresponding to that row wasn't selected by brush



  })
}

UI代码:

library(shiny)
library(ggplot2)
library(shinydashboard)

## ui code starts here ## 
data <- read.csv(file.choose())
datacolnames <- colnames(data)
datarownames<- data[,1]


datacolnames <- datacolnames[-1]
length <- length(datacolnames)
length
data
# Works!@

  maxvector <- c()
  for (i in 2:length) {
    maxvector <- append(maxvector, max(data[,i]))
    i <- i + 1
  }
maxvector <- max(maxvector)
maxvector

minvector <- c()
for (i in 2:length) {
  minvector <- append(minvector, min(data[,i]))
  i <- i + 1
}
minvector <- min(minvector)
minvector













# max <- max(data[,c(:11]))
# min <- min(data[,2])
# max

ui <- 
  dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
      h4("Interactive plots - select data points in plot - return the rows of data that are selected by brush"),

      # brush argument will enable the brush, sends the data point information to the server side
      # at the server side the data points under the brush related information can be read through input$BRUSHID
      plotOutput(outputId = "graph", brush = "plot_brush"), # brush ID is plot_brush
      plotOutput("chart"),

      fluidRow(
        box(width = 5,title = "Charting", status = "warning", solidHeader = T,
            fileInput("file", "Upload the File"),
            h5("Max file size to upload is 5 MB"),
            radioButtons("sep", "Seperator", choices = c(Comma = ",", Period = ".")),
            selectInput("xaxis","Select the Model number", datacolnames),
            selectInput("yaxis", "Select the Type number", datacolnames),
            selectInput("size", "Select the Size", datacolnames),
            selectInput("color", "Select the Color", datacolnames),
            textInput("val1", "Type in the Beginning Value you Want to see", minvector),
            textInput("val2", "Type in the End Value you want to see", maxvector),
            sliderInput("bins", "Data Range", min = minvector, max = maxvector, 10),

            sliderInput("sliderfordata", "Range of X Data Values", min = 10, max = 100, value = 10)
            # sliderInput("y", "Range of Y Data Values", min = 10, max = 100, value = c(10,100))







            # Instead of sliders, we could have text inputs that map to the indexing
            # sliderInput("obs", "Data Range:",  
            #             min = minvector, max = maxvector, value = c(0,472))
        ),
        column(width=5, tags$b(tags$i("Rows corresponding to datapoints under brush")),  tableOutput("data_brush"), offset = 2)

      ),
      fluidPage(
        plotOutput("hist")
      ),


      # left side actual dataset and right side the rows for datapoints selected by brush
      # defined the width of each column and also some styling (bold & italics) using tags
      fluidPage(
        box(width = 12, tags$b(tags$i("Actual Dataset")), DT::dataTableOutput("mytable"))
      )


    )
  )

# download the data button
r shiny
1个回答
2
投票

问题似乎是使用reactive函数而不是renderPlot函数用于图表输出。对于任何有此问题并寻找可能答案的人。

© www.soinside.com 2019 - 2024. All rights reserved.