在 Shiny-Dashboard 中应用多个下拉底部

Apply multiple drop-down bottoms in Shiny-Dashboard

我想在 shiny-dashboard 中使用不同的下拉底部来过滤数据集,该数据集由第一个下拉字段读取。第一个下拉底部循环遍历数据文件夹中的所有文件名,加载并绘制数据(效果很好)。 在下一步中,我想应用评级作为过滤器,它由另一个下拉底部选择。但是,我收到以下错误,

if(dataRating =
                    ^
Warning: Error in sourceUTF8: Error sourcing C:\Temp\RtmpScI3tC\fileb32838a12cde
Stack trace (innermost first):
    1: runApp
Error in sourceUTF8(serverR, envir = new.env(parent = globalenv())) : 
  Error sourcing C:\Temp\RtmpScI3tC\fileb32838a12cde

我的:

library("shiny")

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Default"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'date',
                  label = 'Choose a date:',
                  choices = list.files(path = "C:/R_myfirstT/data",
                                       full.names = FALSE,
                                       recursive = FALSE)),
      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'rating',
                  label = 'Choose the rating:',
                  choices = c("All",0,1,2,3))
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput("plot")
    )
  )
)

服务器

 # Define server  ----
    server <- function(input, output) {

      #  Rating
      dataRating <- reactive({
        rating <- input$rating
        if (is.null(infile)){
          return(NULL)
        }

      })
      #
      dataset <- reactive({
        infile <- input$date
        if (is.null(infile)){
          return(NULL)
        }
        read.csv(paste0('C:/R_myfirstT/data',infile),header=TRUE, sep=";")
      })
    ###      OutPut
      if(dataRating ="All"){
        output$plot <- renderPlot({
        x <- dataset()$Marktwert
        hist(x, breaks = 40)
      })
    }
    }

Server.R 中没有 if 语句的仪表板看起来像

我想知道,

专业

在这种情况下我做错了什么?

一般:

如何使用 data.table 操作应用来自不同下拉底部的不同输入作为过滤器?

附录: 我将 if 语句更改为 if(dataRating() =="All")

 dataRating <- reactive({
    rating <- input$rating

 dataRating <- reactive({
    dataRating  <- input$rating

我找到了一个建议 here。但是,我收到另一个错误

Error in .getReactiveEnvironment()$currentContext() : 
      Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

示例数据如下:

  stand Rating  LGD  Marktwert   EL absolut 
6010    3   3   1142345261  1428757
6010    3   3   849738658   1028973
6010    1   3   1680222820  220554
6010    1   3   896459567   116673
6010    0   3   1126673222  72077
6010    1   3   704226037   93310
-   1   4   336164879   49299
6010    0   3   948607746   60443
6070    1   3   265014117   34170
6020    3   3   47661945    58551
6050    2   3   307011781   115959
6020    0   1   1064022992  20320
6010    0   3   831782040   52950
6080    3   3   19367641    20286
-   2   4   197857365   87608
6010    1   3   679828856   90884
6050    3   3   317092037   372362
6080    3   3   20223616    21929
6010    1   3   693736624   96899
6050    3   3   308447822   372915
6010    4   3   177281455   862068

不幸的是,我没有数据文件来测试自己,但使用 Gregor 的评论请尝试编辑此代码:

###      OutPut
  if(dataRating ="All"){
    output$plot <- renderPlot({
    x <- dataset()$Marktwert
    hist(x, breaks = 40)
  })
}

至:

###      OutPut
  output$plot <- renderPlot({
    if(dataRating() == "All"){
      x <- dataset()$Marktwert
      hist(x, breaks = 40)
    }
  })

注意: 你说得对 dataRating 作为反应值需要使用 dataRating() 来调用。但是,您需要在反应中调用它,而不是像您当前拥有的那样在外部调用它(如错误消息所示)。此外,在这种情况下你需要 == 。您的编辑:

dataRating <- reactive({
    rating <- input$rating

dataRating <- reactive({
    dataRating  <- input$rating

应该没有必要。

我试图测试对数据的猜测。请在您这边进行测试:

更新: 添加样本数据后我稍微编辑了一下。

library("shiny")

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Default"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'date',
                  label = 'Choose a date:',
                  choices = list.files(path = "C:/R_myfirstT/data",
                                       full.names = FALSE,
                                       recursive = FALSE)),
      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'rating',
                  label = 'Choose the rating:',
                  choices = c("All",0,1,2,3))
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Define server  ----
server <- function(input, output) {

  #  Rating
  dataRating <- reactive({
    input$rating
  })
  #
  dataset <- reactive({
    infile <- input$date
    if (is.null(infile)){
      return(NULL)
    }
    read.csv(paste0('C:/R_myfirstT/data/',infile),header=TRUE, sep=";")
  })
  ###      OutPut
  output$plot <- renderPlot({
    if(dataRating() =="All"){  
      x <- dataset()$Marktwert
      hist(x, breaks = 40)
    }
  })
}

shinyApp(ui, server)

一些评论:

"Error in : object 'infile' not found" 是由这个代码块引起的:

#  Rating
      dataRating <- reactive({
        rating <- input$rating
        if (is.null(infile)){
          return(NULL)
        }

      })

infile 在这里不存在。请记住,在 dataset 反应式中,您首先有 infile <- input$date

此外,为了给 dataRating 赋值,您需要像我一样将 input$rating 放在 reactive 的末尾,或者将 return(input$rating) 放在 return(input$rating) 的末尾。当您将 read.csv(paste0('C:/R_myfirstT/data',infile),header=TRUE, sep=";") 放在 dataset 反应代码块的末尾时,您做了类似的事情。