Home > Net >  Extract a value from reactive data frame in shiny
Extract a value from reactive data frame in shiny

Time:03-07

I am not sure how I extract a value from a reactive data frame and use it for calculation. The reactive output did not show up so I could not calculate what I want it the end. When I run the script below, I got an error as "$ operator is invalid for atomic vector"

Exercise<-c(A,B,C)
Var1<-c(60,90,50)
Var2<-c(0.5,0.7,0.3)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(numericInput(inputId = "Time1",
                              label = "Duration:",
                              min = 0,
                              max = 120,
                              value = 1),
                 selectInput(
                   inputId = "Drill1",
                   label = "Drill1",
                   choices = Exercise,
                   selected = "1")
    ),
    mainPanel(h3(textOutput("Power"))
    )
  )
)


server <- function(input, output) {
  d<- reactive({
    res<-T1 %>%
      filter(Exercise == input$Drill1)
    res
  })
  
  output$Power <- renderPrint({
    dataset <-d()
    Int<-dataset$Var1[dataset$Exercise == input$Drill1]
    results<-Time1*Int
    results
  })
}

I really appreciate your help in advance.

CodePudding user response:

I tidied up your code a little bit and ran it in a new R session. Unfortunately, I couldn't reproduce this issue. The app below runs fine on my machine.

library(shiny)
library(dplyr)

T1 <- data.frame(
  Exercise = c("A", "B", "C"),
  Var1 = c(60, 90, 50),
  Var2 = c(0.5, 0.7, 0.3)
)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      numericInput(
        inputId = "Time1",
        label = "Duration:",
        min = 0,
        max = 120,
        value = 1
      ),
      selectInput(
        inputId = "Drill1",
        label = "Drill1",
        choices = T1$Exercise
      )
    ),
    mainPanel(h3(textOutput("Power")))
  )
)


server <- function(input, output) {
  d <- reactive({
    filter(T1, Exercise == input$Drill1)
  })
  
  output$Power <- renderPrint({
    dataset <- d()
    Int <- dataset$Var1[dataset$Exercise == input$Drill1]
    input$Time1*Int
  })
}

shinyApp(ui, server)
  • Related