Reputation: 1522
I'm constructing a dashboard with Shiny and for a certain plot I want the user let choose which variable the plot will show. So far I never had an issue with that but now, whyever, I always receive that the dataframe is not there to choose from.
I guess this might be due to the circumstance that this dataframe is created rather at the end of the entire code and when Shiny is building up the dashboard including the SelectVariable option, it does not know this dataframe, yet.
Does this make sense? On the other hand, I wonder then, why this had never been any issue before. If my guessing is true, how do I solve this?
ui.R
library(shiny)
ui<- fluidPage(
# Application title
titlePanel(""),
# Application title
titlePanel("HiRay 9 DB"),
tabsetPanel(
tabPanel("Histogram",
sidebarLayout(
sidebarPanel(width = 3, sliderInput("bins"
,"Number of bins:"
,min = 100
,max = 1000
,value = 500
),
varSelectInput("sel_variable"
, "Choose variable: "
, data = df_sorted
, selected = "x_ray_hours_balance"
# , multiple = FALSE
)
),
mainPanel(
plotOutput("histo", width = "100%", height = "500"),
plotOutput("ggpairs", width = "100%", height = "500")
)
)
),
tabPanel("Unsorted data",
mainPanel(
plotOutput("Unsorted_data", width = "150%", height = "650"),
plotOutput("Unsorted_data2", width = "150%", height = "450")
)
),
tabPanel("Sorted data",
mainPanel(
plotOutput("Sorted_data", width = "150%", height = "425"),
plotOutput("Sorted_data2", width = "150%", height = "425")
)
),
tabPanel("Weibull",
mainPanel(
div("Loading might take a while, please stay put..", style = "color:blue")
, plotOutput("Weibull", width = "150%", height = "800")
)
),
tabPanel("Survival",
mainPanel(
plotOutput("Survival", width = "150%", height = "800")
)
),
tabPanel("Survival ext",
mainPanel(
plotOutput("Survival_ext", width = "150%", height = "800")
)
),
tabPanel("Correlations",
mainPanel(
plotOutput("Correlations", width = "150%", height = "800")
)
),
tabPanel("PCA",
mainPanel(
plotOutput("PCA", width = "150%", height = "800")
)
)
)
)
server.R
# Read in data ----
df = data.frame(read.csv("file.csv"
, header = TRUE
, sep = ";"
, dec = ","
, na.strings = "---"
)
)
# clean data frame ----
df<- df %>%
clean_names()
df<- df %>% janitor::remove_empty(whic=c("rows"))
df<- df %>% janitor::remove_empty(whic=c("cols"))
df<- dplyr::distinct(df)
colnames(df)[1]<- "country"
df_unsorted<- df
# sort out data ----
### keep only status, x_ray_hours_balance, preventive_exchange, operating_hours, op days, fail code
df<- df[, c("status"
, "x_ray_hours_balance"
, "preventive_exchange"
, "position_in_unit"
, "operating_hours_balance"
, "operating_days"
, "failure_code"
)]
df<- df[df$x_ray_hours_balance != "---", ]
df<- df[!is.na(df$x_ray_hours_balance), ]
df<- df[!is.na(df$status), ]
# Make num columns numeric
df[, c(
"x_ray_hours_balance"
, "operating_hours_balance"
, "operating_days"
)]<- sapply(df[, c(
"x_ray_hours_balance"
, "operating_hours_balance"
, "operating_days"
)],as.numeric)
## keep x_ray > 0 ----
df<- subset(df, df$x_ray_hours_balance > 0)
## clean status ----
df<- df[df$status != "?", ]
#df<- df[df$position_in_unit != "?", ]
df_sorted<- df
# Plot ----
server<- function(input, output) {
output$histo <- renderPlot({
p_1<- ggplot(df_sorted, aes_string(x = input$sel_variable)) +
# p_1<- ggplot(df_sorted, aes(x = input$sel_variable)) +
# p_1<- ggplot(df_sorted, aes_string(x = x_ray_hours_balance )) +
# p_1<- ggplot(df_sorted, aes(x = x_ray_hours_balance )) +
geom_histogram(binwidth = input$bins
, fill = "darkgreen"
, colour = "forestgreen"
) + theme_bw()
p_1
})
}
When I replace df_sorted
with df
within the ggplot command, it works. The same/similar question arose already here: R Shiny: how to use a dataframe variable from server.R in ui.R
Upvotes: 0
Views: 63
Reputation: 18551
Looking at your code, your df
seems only be available within your server script. Below is a minimal example that reproduces your error. If you comment in the first line it will work:
library(shiny)
# without this line your script will fail - comment it in to make it work:
# df_iris <- iris
shinyApp(ui = fluidPage(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 10,
value = 1
),
varSelectInput("sel_variable",
"Choose variable: ",
data = df_iris,
selected = "Sepal.Length"
),
plotOutput("histo", width = "100%", height = "500")
), server = function(input, output) {
df_iris <- iris
output$histo <- renderPlot({
p_1 <- ggplot(df_iris,
aes(x = !! input$sel_variable)) +
geom_histogram(binwidth = input$bins) +
theme_bw()
p_1
})
})
Upvotes: 1