首页 文章

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

提问于
浏览
0

我想在闪亮的仪表板中使用不同的下拉式底部来过滤数据集,该数据集由第一个下拉字段读取 . 第一个下拉式底部循环遍历数据文件夹中的所有文件名,加载并绘制数据(它运行良好) . 在下一步中,我想将评级作为过滤器应用,该过滤器由另一个下拉列表底部选择 . 但是,我收到以下错误,

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

我的 Gui

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")
    )
  )
)

Server

# 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 语句的仪表板看起来像
this

我想知道,

specialy:

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

通常:

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

Addendum :我将 if -statement更改为 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

2 回答

  • 1

    我试图测试数据 . 请在您身边测试:

    Update: 我在添加样本数据后略微编辑了 .

    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)
    

    Some comments:

    “错误:对象'infile'未找到”是由此代码块引起的:

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

    infile 在这里不存在 . 请记住在 dataset 被动,你首先有 infile <- input$date .

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

  • 1

    不幸的是我没有数据文件来测试自己,但使用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)
        }
      })
    

    Notes: 你是正确的,需要使用 dataRating() 来调用 dataRating 作为无功值 . 但是你需要在当前拥有它的内部调用它而不是外部(如错误消息所示) . 此外,在这种情况下你需要 == . 你的编辑:

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

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

    不应该是必要的 .

相关问题