1

I have the following dataframe

  df<-data.frame("A"<-c(1:5), 'B'<-c("a", 'b', 'c', 'd', 'e'))
  names(df)<-c('A', "B")

I have created a select box in R shiny UI and have also written the code for the server as below.

 ui<- fluidPage(
 selectInput(inputId = "input", label = "No of days elapsed", choices = 
 c(0:1000), selected = T, multiple =F  
 ),mainPanel(verbatimTextOutput("Output1")))

 server<-function(input, output){text<-reactive({number<-input$input
 number<-as.numeric(number)

  Y<-function(X){if(X<5){
  A<-df[!df$A==X,]} else {A<-df[df$A==(X-3),]}
  return (A)}
  out1<-Y(number)
  })
    renderPrint({text()$B})
    }
    shinyApp(ui, server)

The code generates the select box box. However, When i use the box and choose a number, there is no output. Also no error appears. I am not sure where my error is

2 Answers 2

2

There are a few things wrong with your code:

  • You call verbatimTextOutput("Output1"), but output$Output1 is not defined.
  • You use verbatimTextOutput, while it seems you want to output a table, so you should use dataTableOutput.
  • Also, formatting ;)

A working example is shown below. Hope this helps!


Y<-function(X){if(X<5){
  A<-df[!df$A==X,]} else {A<-df[df$A==(X-3),]}
  return (A)}

df<-data.frame("A"<-c(1:5), 'B'<-c("a", 'b', 'c', 'd', 'e'))
names(df)<-c('A', "B")

ui<- fluidPage(
  selectInput(inputId = "input", label = "No of days elapsed", choices = 
                c(0:1000), selected = T, multiple =F  ),
  mainPanel(dataTableOutput("Output1"))
)

server<-function(input, output){
  text<-reactive({
    number<-input$input
    number<-as.numeric(number)
    out1<-Y(number)
  })
  output$Output1 <- renderDataTable({text()})
}
shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes Sir. I have added some details.
1

Here is where I made a mistake.

     ui<- fluidPage(
      selectInput(inputId = "input", label = "No of days elapsed", choices = 
      c(0:1000), selected = T, multiple =F  
    ),mainPanel(verbatimTextOutput("Output1")))


      server<-function(input, output){text<-reactive({number<-input$input
      number<-as.numeric(number)

       Y<-function(X){if(X<5){
       A<-df[!df$A==X,]} else {A<-df[df$A==(X-3),]}
        return (A)}
       out1<-Y(number)
        } )
       **output$Output1<-renderPrint({text()$B})**
        }
       shinyApp(ui, server)

That generated the output. I had not assigned the output correctly

2 Comments

Ah, I see now what you were trying to do. My replacement of your verbatimtextoutput with datatableoutput was incorrect, glad you were able to resolve the issue :)
Actually your output looks impressive. I Wanted a dataframe generated by renderprint but after assigning the function inside. So that I managed with my code. However, I was able to get an alternate version also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.