Reactive not displaying appropriate graphs with working data filtering - shiny

server code:
silver_state <- fread("./Data/silver_state.csv")
silver <- silver_state %>% arrange(total_drug_cost)
state_cast <- reactive({
if(input$sort == "alphabetical"){
silver <- silver
}
else if(input$sort == "descending"){
silver <- silver_state %>% arrange(desc(total_drug_cost))
silver$nppes_provider_state <- factor(silver$nppes_provider_state,
levels = silver$nppes_provider_state[order(silver$total_drug_cost)])
}
else{
silver <- silver_state %>% arrange(total_drug_cost)
silver$nppes_provider_state <- factor(silver$nppes_provider_state,
levels = silver$nppes_provider_state[order(silver$total_drug_cost)])
}
})
output$compare <- renderPlot({
ggplot(silver) +
geom_bar(aes(x = nppes_provider_state, y = total_drug_cost), position
= position_stack(reverse = TRUE), stat = "identity") +
coord_flip() +
labs(title = "Total Cost of Drugs per State", y = "Total Drug Cost",
x = "State")
})
}
shinyServer(my.server)
The data filtering runs fine on its own however, it is not passing through the inputs correctly? It has to be something surrounding how we are structuring the reactive function. Could it have anything to do with using multiple tabs? Thank you.

state_cast is not used anywhere and shouldn't really exist. It looks like it's being abused as a side-effect-only function. Just move its contents into renderPlot().
Additionally, you have a silver <- silver that doesn't seem to do anything.
I also recommend you use the Reindent Lines and Reformat Code buttons, because the indentation in the state_cast makes it a bit difficult to read.

Related

How to remove specific number of events in a tbl_regression (gtsummary package)

I'm using tbl_regression from the gtsummary package to show the results of cox proportional hazards models. Due to circumstances regarding sensitive personal information, I am not allowed to show strata with a number of observations less than 5. I can, however, still show the estimates, CIs etc. for those strata, but not how many persons have had the event if the number is less than 5. In these number of events strata with less than 5 observations, I would like to insert just a line to indicate this.
From what I have read, the modify_table_body function is perhaps the correct function to achieve this. However, I cannot manage to find out how to use it correctly. Is there any way to define that the regression table should not show N_event less than 5 but still show HRs, CIs, person years ect. for those given stratas?
Below is my preliminary code in which I thought maybe should be followed by "%>% modify_table_body()".
Thank you in advance for your help!
Best,
Mathilde
cox_cat_cns2 <- coxph(Surv(TTD_year, Dod_status) ~ Highest_Edu_Household + Diag_year + Age_household_mom_num + Age_household_dad_num + Country_origin_household, data = data_cox_cat_cns)
cox_cat_cns_adj_table <- tbl_regression(cox_cat_cns2,
label = c(Highest_Edu_Household ~ "Highest parental education",
Diag_year ~ "Year of diagnosis",
Age_household_mom_num ~ "Mother's age at diagnosis",
Age_household_dad_num ~ "Father's age at diagnosis",
Country_origin_household ~ "Parents' country of origin"),
exponentiate = TRUE) %>%
add_nevent(location = "level") %>%
bold_labels() %>%
italicize_levels() %>%
modify_table_styling(
columns = estimate,
rows = reference_row %in% TRUE,
missing_symbol = "Ref.") %>%
modify_footnote(everything() ~ NA, abbreviation = TRUE) %>%
modify_table_styling(
column = p.value,
hide = TRUE) %>%
modify_header(
label = "",
stat_nevent = "**Events (N)**",
exposure ~ "**Person years**")
You can 1. define a new function to "style" the number of events that collapses any counts less than 5 as "<5", then 2. use that function to style the column in the resulting table. Example below!
library(gtsummary)
#> #Uighur
library(survival)
library(dplyr)
style_number5 <- function(x, ...) {
ifelse(
x < 5,
paste0("<", style_number(5, ...)),
style_number(x, ...)
)
}
style_number5(4:6)
#> [1] "<5" "5" "6"
tbl <-
trial %>%
slice(1:45) %>%
coxph(Surv(ttdeath, death) ~ stage, data = .) %>%
tbl_regression(exponentiate = TRUE) %>%
add_nevent(location = "level") %>%
modify_fmt_fun(stat_nevent ~ style_number5)
Created on 2022-04-21 by the reprex package (v2.0.1)

ShinyApp - reactive inferno

I have two input variables, and changing one will cause the change of the other one.
Further to that, if the value of input is outside limits it should default to min (if below) or max (if above) value.
All works fine as long as up and down arrows are being used.
The moment I am typing value 1 in Input1 it goes crazy.
Same if I am deleting Input2, even before I am typing anything...
I am aware that it must have something to do with reactive values, but can not fix it...
Any suggestion will be very much appreciated!
library(shiny)
ui <- fluidPage(
fluidRow(
uiOutput("Input1"),
numericInput("Input2", "Input 2",
min = 50, max = 150,
value = 100, step = 1)),
tableOutput("result")
)
#########################################################
server <- function(input, output, session) {
global <- reactiveValues(numVal = 10, numMin = 5, numMax = 15)
numVal <- reactive({
if(!is.null(input$Input1)){
if(input$Input1 < global$numMin) return(global$numMin)
if(input$Input1 > global$numMax) return(global$numMax)
return(input$Input1)
}else{
return(global$numVal)
}
})
output$Input1 <- renderUI(numericInput("Input1", "Input 1",
min = global$numMin, max = global$numMax,
value = numVal(), step = 0.1))
# when Input1 change, update Input2
observeEvent(input$Input1, {
updateNumericInput(session = session,
"Input2",
value = format(round(input$Input1*10, 0), nsmall = 0))
})
# when Input2 change, update Input1
observeEvent(input$Input2, {
updateNumericInput(session = session,
"Input1",
value = format(round(input$Input2*0.1, 1), nsmall = 1))
})
inputdata <- reactive({
data <- data.frame(Coef = as.numeric(input$Input1))
data
})
output$result <- renderTable({
data = inputdata()
resultTable = as.character(round((data$Coef + 10)*100, digits=2))
resultTable
})
}
#########################################################
shinyApp(ui, server)
You are on the brink of getting into a race condition:
Input 1 changes Input 2 changes Input 1 changes Input 2...
So foremost you should reconsider your design. You can use debounce / throttle to avoid some of the race consition by telling Shiny not too fire too quickly and as the updates are bijective you may achieve what you want, but I would really think about your design b/c these circle dependencies are almost never a good idea.
Having said that here is a solution which behaves better (N.B. I removed the dynamic rendering of the second input element as it has nothing to do wiht the problem at hand). It is not perfect, b/c you will eventually end up in a racing condition, but you can soften this situation by playing w/ the debouncing factors.
library(shiny)
ui <- fluidPage(
fluidRow(
numericInput("Input1", "Input 1",
min = 5, max = 15, value = 10, step = .1),
numericInput("Input2", "Input 2",
min = 50, max = 150,
value = 100, step = 1)),
tableOutput("result")
)
server <- function(input, output, session) {
## debounce both input, i.e. they are firing onyl if no change within 1sec happens
## c.f. ?debounce
getI1 <- reactive(input$Input1) %>%
debounce(1000)
getI2 <- reactive(input$Input2) %>%
debounce(1000)
observeEvent(input$Input1, {
updateNumericInput(session = session,
"Input2",
value = format(round(getI1() * 10, 0), nsmall = 0))
})
observeEvent(input$Input2, {
updateNumericInput(session = session,
"Input1",
value = format(round(getI2() * 0.1, 1), nsmall = 1))
})
inputdata <- reactive({
data <- data.frame(Coef = as.numeric(input$Input1))
data
})
output$result <- renderTable({
data = inputdata()
resultTable = as.character(round((data$Coef + 10)*100, digits=2))
resultTable
})
}
shinyApp(ui, server)

Simple Shiny selectInput not working with Intersect

Is there any reason this wouldn't work? I simply want to see which terms are found in the two selected columns. I figured intersect would do the job, but I'm not seeing results. If this looks alright, perhaps I have some other syntax error along the way? Do the inputs need to be in different sidebar panels?
selectInput("data1", "Choose you Input:", choices = colnames(data), selected = "PD.Risk.Factor"),
selectInput("data2", "Choose you Input:", choices = colnames(data), selected = "AD.Risk.Factor")),
Output:
p2 = intersect(x = input$data1, y = input$data2)
print(p2)
Welcome to SO! Please provide a reprex the next time - this will help to get help.
For our problem. What your snippet does is to compare not the columns of your data frame but the the strings as returned by selectInput. What you want to do is to use these strings to retrieve the corresponding columns in the data.
library(shiny)
sample_dat <- data.frame(x = 1:10, y = 5:14, z = 9:18)
ui <- fluidPage(selectInput("col1", "Column 1:", names(sample_dat), "x"),
selectInput("col2", "Column 1:", names(sample_dat), "y"),
verbatimTextOutput("result"))
server <- function(input, output, session) {
output$result <- renderPrint({
list(on_strings = list(col1 = input$col1,
col2 = input$col2,
intersect = intersect(input$col1, input$col2)),
on_cols = list(col1 = input$col1,
col2 = input$col2,
intersect = intersect(sample_dat[[input$col1]],
sample_dat[[input$col2]])))
})
}
shinyApp(ui, server)

Outputting the results from bife object to Latex in Rmarkdown?

I'm estimating a fixed-effects probit model using the bife package in R. I'm trying to extract the output into something I can use with either stargazer or texreg so I can output them into a paper using Rmarkdown to create a LaTeX object. I'm aware I can manually extract the coefficients and standard errors, etc., but I'm wondering if there isn't a more efficient way to coerce this object into something that'd work with either package.
Here's a reproducible example:
install.packages("bife")
library(bife)
data("iris")
iris$big <- ifelse(iris$Sepal.Length > median(iris$Sepal.Length),1,0)
output <- bife(big ~ Sepal.Width + Petal.Length | Species, data=iris, "logit")
I think I found an alternative solution for this one, even if it is probably too late
Basically, first, I went on the repository of the package "texreg" and found this function:
extract.bife <- function(model,
include.loglik = TRUE,
include.deviance = TRUE,
include.nobs = TRUE,
...) {
s <- summary(model)
coefficient.names <- rownames(s$cm)
co <- s$cm[, 1]
se <- s$cm[, 2]
pval <- s$cm[, 4]
gof <- numeric()
gof.names <- character()
gof.decimal <- logical()
if (include.loglik == TRUE) {
lik <- logLik(model)
gof <- c(gof, lik)
gof.names <- c(gof.names, "Log Likelihood")
gof.decimal <- c(gof.decimal, TRUE)
}
if (include.deviance == TRUE) {
gof <- c(gof, deviance(model))
gof.names <- c(gof.names, "Deviance")
gof.decimal <- c(gof.decimal, TRUE)
}
if (include.nobs == TRUE) {
n <- s$nobs["nobs"]
gof <- c(gof, n)
gof.names <- c(gof.names, "Num. obs.")
gof.decimal <- c(gof.decimal, FALSE)
}
tr <- createTexreg(
coef.names = coefficient.names,
coef = co,
se = se,
pvalues = pval,
gof.names = gof.names,
gof = gof,
gof.decimal = gof.decimal
)
return(tr)
}
So for your example, just apply it on your model and use the function texreg and you may have a Latex-"like" output
tr <- extract.bife(output)
texreg(tr)
I hope it will help!
Best

rankall : returning the correct data frame to rank hospitals on performance

this is a solution(not working well) to a coursera problem. I'm trying to rank a data frame containing the names of hospitals based on their performance on 3 different conditions. (I found another to this question at How to subset a row from list based on condition). I think I'm not subsetting right and I don't return the correct data frame at the end. really new to programming and R. thank you for your help.
rankall <- function(outcome, num = 'best'){
data <- read.csv('outcome-of-care-measures.csv', colClasses = 'character')
data[,11] <- as.numeric(data[,11])
data[,17] <- as.numeric(data[,17])
data[17] <- as.numeric(data[,23])
states <- sort(unique(data$State))
conditions <- data[c(11,17,23)]
if(!state %in% states){stop('invalid state')}
if(!outcome %in% conditions){stop('invalid outcome')}
for (i in 1:length(states)){
statedata <-data[data$State == state[i],]
if(outcome == 'heart attack'){column <- (statedata[,11]}
if(outcome == 'heart failure') {column <-(statedata[,17]}
if(outcome == 'pneumonia') {column <- statedata[,23]}
rankedhospitals <- c()
rankcondition <- rank(column, na.last = NA)
if (num == 'best'){num <- 1}
if(num == 'worst'){num <- nrow(rankcondition)}
rankedhospitals[i] <- statedata$Hospital.Name[order(column, statedata$Hospital.Name)[num]]
rankedhospitals <- cbind(rankedhospitals,states[num,2])
}
return (c('rankedhospitals', 'states'))
}