1

I want to use different drop-down bottoms in the shiny-dashboard to filter the data set, which is read by the first drop-down field. The first drop-down bottom loops over all file names in the data-folder, loads and plots the data (it works well). In the next step, I want to apply ratings as a filter, which is chosen by another drop-down bottom. However, I get the following error,

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

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

and the 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)
      })
    }
    }

The dashboard without if statement in Server.R looks like this

I would like to know,

specialy:

what did I do wrong in this case?

generally:

how can I apply different inputs from different drop-down bottoms with data.table operation as filters?

Addendum: I changed the if-statement to if(dataRating() =="All") and

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

to

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

I found a suggestion here. However, I get another error

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

Sample data looks like:

  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
maniA
  • 1,437
  • 2
  • 21
  • 42
  • 2
    I can't test this right now but `renderPlot({if (dataRating() == "All") ... })` should do the job for you. – Gregor de Cillia Jul 12 '18 at 14:27
  • @GregordeCillia: Applying your suggestion, I do not get any error. However, I do not obtain any Plot at all! Why actually? – maniA Aug 01 '18 at 09:37

2 Answers2

1

Unfortunately I don't have the data files to test myself but using Gregor's comment please try editing this code:

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

to:

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

Notes: You are correct that dataRating being a reactive value needs to be called using dataRating(). However you need to call this inside a reactive not outside as you currently have it (as the error message suggests). Furthermore you need == in this case. Your edit:

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

to

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

should not be necessary.

Eli Berkow
  • 2,628
  • 1
  • 12
  • 22
  • Hi Eli, and thanks a lot and sorry for the late reaction. I tried your suggestion. However, I get "Error in : object 'infile' not found" – maniA Aug 01 '18 at 09:46
  • Can you give us a minimal reproducible example then to test? – Eli Berkow Aug 01 '18 at 09:49
  • you mean, you need a small bunch of the data? Because the code is completely there! – maniA Aug 01 '18 at 10:27
  • Exactly! We have no way of testing your code without the appropriate data. Column names, types, etc. are vital. You can anonymise it if you want but we need some small dataset. – Eli Berkow Aug 01 '18 at 10:29
  • I added a data-sample in the origin question! – maniA Aug 01 '18 at 13:26
1

I attempted to test taking a guess at the data. Please test this on your side:

Update: I edited slightly after sample data was added.

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:

"Error in : object 'infile' not found" is caused by this code block:

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

      })

infile does not exist here. Remember in the dataset reactive you have infile <- input$date first.

Furthermore, in order to assign a value to dataRating you either need to put input$rating at the end of the reactive like I did or put return(input$rating). You did similar when you placed read.csv(paste0('C:/R_myfirstT/data',infile),header=TRUE, sep=";") at the end of the dataset reactive code block.

Eli Berkow
  • 2,628
  • 1
  • 12
  • 22
  • you are totaly right with 'infile' I recognized by myself too, thanks a lot! – maniA Aug 01 '18 at 13:33
  • 1
    Please try my code above and let me know. It worked for me using your dataset, thanks. – Eli Berkow Aug 01 '18 at 14:19
  • wow true. It works very nice. However, It does not help me to achieve my intention. I am going to use: http://shiny.rstudio.com/gallery/basic-datatable.html In the case, that I have a question, how can I address it to you? – maniA Aug 03 '18 at 07:42
  • Keep using the comments here or add a new stackoverflow question – Eli Berkow Aug 07 '18 at 08:02