PANDA
PANDA

Reputation: 137

Create different results in different tabs using R Shiny

I am new to R shiny. I have created one web application by writing below code. Here i need some modification.

  1. I want to create two buttons(Ex: Output, Accuracy)
  2. When users clicks the Output button it should show the output values print(final1).
  3. When user clicks the Accuracy button, it should show the Confusion Matrix(cm) print(caret::confusionMatrix(cm2)).

Could anyone please help me. Below is the code:

runApp(
list(
ui = fluidPage(

  titlePanel("Upload & Results"),



  sidebarLayout(

    sidebarPanel(


      fileInput('file1', 'Choose Excel File',accept = c(".xlsx")

      )
    ),

    mainPanel(

      tableOutput('contents'))

  )
),
server = function(input, output){
  output$contents <- renderTable({

    req(input$file1)

    inFile <- input$file1





    df_test=read_excel(inFile$datapath, 1)
    df1_test =  subset(df_test, select=c("Number"    ,                    
                                         "Category"       ,"Country"   ,                     
                                         "State"  , "Region","PkValue" ,"ASTvalue" ,"DMValue" ,"Geo","Demo"))




    Probabilty_Score = predict(classifier, type = 'response',newdata = df1_test)
    Output = as.factor(ifelse(Probabilty_Score<0.55,0,1))

    cm2 = table(df_test$Result, Output)

    print(caret::confusionMatrix(cm2))

    final=df_test[,c("Number","Result")]
    final1=cbind(final,Output,Probabilty_Score)    

    print(final1)






  })
}
)
)

Upvotes: 0

Views: 356

Answers (2)

Saurabh Chauhan
Saurabh Chauhan

Reputation: 3221

We can add two buttons (as pointed in the question) in the UI code as follow:

  actionButton("output", "Output"),  # Button to get Output
  actionButton("accuracy", "Accuracy") # Button to get accuracy 

Now, we can observe the button click event as follow (without data I am not sure whether the following code is working or not):

observeEvent(input$output, {
   print(final1)    # Print the outvalues here as button Output is clicked
})

observeEvent(input$accuracy, {
   print(caret::confusionMatrix(cm2))   # Print confusion matrix here as button accuracy is clicked
})

Updated code:

  runApp(
  list(
    ui = fluidPage(

      titlePanel("Upload & Results"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose Excel File',accept = c(".xlsx")),
          actionButton("output", "Output"),  # Button to get Output
          actionButton("accuracy", "Accuracy") # Button to get accuracy 
        ),

        mainPanel(
          fixedRow(
            column(10,DT::dataTableOutput("contents")) 
          )
        )
     )
    ),
    server = function(input, output){
      output$contents <- renderDataTable({


        req(input$file1)
        inFile <- input$file1
        df_test=read_excel(inFile$datapath, 1)
        df1_test =  subset(df_test, select=c("Number"    ,                    
                                             "Category"       ,"Country"   ,                     
                                             "State"  , "Region","PkValue" ,"ASTvalue" ,"DMValue" ,"Geo","Demo"))


        Probabilty_Score = predict(classifier, type = 'response',newdata = df1_test)
        Output = as.factor(ifelse(Probabilty_Score<0.55,0,1))

        cm2 = table(df_test$Result, Output)

        final=df_test[,c("Number","Result")]
        final1=cbind(final,Output,Probabilty_Score)  

        observeEvent(input$output, {
          print(final1)    # Print the outvalues here as button Output is clicked
        })

        observeEvent(input$accuracy, {
          print(caret::confusionMatrix(cm2))   # Print confusion matrix here as button accuracy is clicked
        })

        # In the following line, return data.frame that you would like to see
        return(DT::datatable(iris, options = list(pageLength = 10)))
      })
    }
  )
)

Upvotes: 1

alex_555
alex_555

Reputation: 1102

As you didn't provide sample data, I can only give you more or less of a guess that (hopefully) works as intended. I know you wanted to have two actions buttons, but I consider adding an input switch from the shinyWidgets package the cleaner option. I added comments where I changed something in your code. Please try it out with your data and tell me if it's working.

library(shiny)
library(shinyWidgets) # You'll need this to get the switch widget
runApp(
  list(
    ui = fluidPage(
      titlePanel("Upload & Results"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose Excel File',accept = c(".xlsx")), # don't forget a "," here

          #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
          # Add a switch
          switchInput("input_switch",
                      "change",
                      value=T,
                      onLabel="Output",
                      offLabel="Accuracy")
          #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

        ),

        mainPanel(
          tableOutput('contents'))
      )
    ),
    server = function(input, output){

      output$contents <- renderTable({
        req(input$file1)
        inFile <- input$file1
        df_test=read_excel(inFile$datapath, 1)
        df1_test =  subset(df_test, select=c("Number"    ,                    
                                             "Category"       ,"Country"   ,                     
                                             "State"  , "Region","PkValue" ,"ASTvalue" ,"DMValue" ,"Geo","Demo"))

        Probabilty_Score = predict(classifier, type = 'response',newdata = df1_test)
        Output = as.factor(ifelse(Probabilty_Score<0.55,0,1))
        cm2 = table(df_test$Result, Output)

        final=df_test[,c("Number","Result")]
        final1=cbind(final,Output,Probabilty_Score)    

        #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        # Determine output depending on switch
        if(input$input_switch){
          print(caret::confusionMatrix(cm2))
        }else{
          print(final1)
        }
        #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      })
    }
  )
)

Upvotes: 1

Related Questions