Reputation: 326
If I want to obtain the fisher test first I need a contigency table. I can do that for the Arthritis
package by simply:
library(vcd)
data(Arthritis)
freq <- as.data.frame.matrix(table(Arthritis$Treatment, Arthritis$Improved))
> freq
None Some Marked
Placebo 29 7 7
Treated 13 7 21
So I could do for example, a fisher test for:
Not marked Marked
Placebo 36 7
Treated 20 21
For now, what I want to do in shiny is allow the user to select two categorical variables (Treatment
and Improved
), and then filter by another one (Gender
) and obtain the contingency table.
I could use later this one to obtain the 2x2 frequency. But for now this is what I have:
# Shiny
library(shiny)
library(shinyWidgets)
library(shinyjqui)
# Data
library(vcd)
library(readxl)
library(dplyr)
library(arules) # Discretization
# Plots
library(ggplot2)
not_sel <- "Not Selected"
ui <- fluidPage(
titlePanel("Plotter"),
sidebarPanel(
fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
selectInput("num_var_2", "Variable Y axis", choices = c(not_sel)),
selectInput("biomarker", "Select Biomarker", choices = c(not_sel)), uiOutput("factor")
),
mainPanel(
tabsetPanel(
tabPanel(
verbatimTextOutput("test")
)
)
)
)
## Server ##
server <- function(input, output){
# Dynamic selection of the data. We allow the user to input the data that they want
data_input <- reactive({
#req(input$xlsx_input)
#inFile <- input$xlsx_input
#read_excel(inFile$datapath, 1)
Arthritis
})
# We update the choices available for each of the variables
observeEvent(data_input(),{
choices <- c(not_sel, names(data_input()))
updateSelectInput(inputId = "num_var_1", choices = choices)
updateSelectInput(inputId = "num_var_2", choices = choices)
updateSelectInput(inputId = "biomarker", choices = choices)
})
num_var_1 <- eventReactive(input$run_button, input$num_var_1)
num_var_2 <- eventReactive(input$run_button, input$num_var_2)
biomarker <- eventReactive(input$run_button, input$biomarker)
output$factor <- renderUI({
req(input$biomarker, data_input())
if (input$biomarker != not_sel) {
b <- unique(data_input()[[input$biomarker]])
pickerInput(inputId = 'selected_factors',
label = 'Select factors',
choices = c(b[1:length(b)]), selected=b[1], multiple = TRUE,
# choices = c("NONE",b[1:length(b)]), selected="NONE", If we want "NONE" to appear as the first option
# multiple = TRUE, ## if you wish to select multiple factor values; then deselect NONE
options = list(`actions-box` = TRUE)) #options = list(`style` = "btn-warning"))
}
})
data_stats_discrete <- reactive({
req(data_input(), input$num_var_1, input$num_var_2, input$biomarker)
# We filter by biomarker in case user selected, otherwise data_input() remains the same
if (input$biomarker != "Not Selected") df <- data_input()[data_input()[[input$biomarker]] %in% input$selected_factors,]
else df <- data_input()
df <- as.data.frame.matrix(table(.data[[input$num_var_1]], .data[[input$num_var_2]]))
df
})
output$test <- renderPrint(data_stats_discrete())
}
shinyApp(ui = ui, server = server)
As you can see in this RepEx, no dataframe is being selected in the data_stats_discrete
.
Upvotes: 0
Views: 85
Reputation: 21349
Change
df <- as.data.frame.matrix(table(.data[[input$num_var_1]], .data[[input$num_var_2]]))
to
df <- as.data.frame.matrix(table(df[[input$num_var_1]], df[[input$num_var_2]]))
Upvotes: 1