[英]Shiny App Issue in R "output object not found"
I am facing an issue while creating my Shiny App.我在创建 Shiny App 时遇到问题。
The error message is错误信息是
Error in output$raw_data <- renderTable({ : object 'output' not found
output$raw_data <- renderTable 中的错误({:找不到对象“输出”
which occurs for all output codes.所有输出代码都会出现这种情况。
The below codes contain two parts of the shinny app in R which are UI and server.以下代码包含 R 中 shinny 应用程序的两个部分,即 UI 和服务器。 I self-checked the UI part and it seemed fine while running.
我自我检查了 UI 部分,运行时似乎很好。
I looked up some posts and solutions of them.我查了一些帖子和他们的解决方案。 They suggested I check if all outputs are in the server function, which I did and turned out that all outputs should be inside the server function now.
他们建议我检查所有输出是否都在服务器函数中,我这样做了,结果现在所有输出都应该在服务器函数中。 But the issue "output object not found" still recurs.
但是“找不到输出对象”的问题仍然存在。
library(rsconnect)
library(shiny)
library(survival)
library(survminer)
library(readxl)
myData <- structure(list(Years_Diff_Surg_Death = c(36.99383984,
2.584531143, 36.91991786,
36.89527721, 36.88158795, 36.82135524),
Survival = c(1L, 0L, 1L, 1L, 1L, 1L)))
data_2013 <- structure(list(Years_Diff_Surg_Death = c(36.993839835729, 2.5845311430527,
36.9199178644764, 36.8952772073922, 36.8815879534565, 36.8213552361396),
Survival = c(1, 0, 1, 1, 1, 0)))
data_2014 <- structure(list(Years_Diff_Surg_Death = c(0.542094455852156, 5.19644079397673,
35.9561943874059,
35.917864476386, 35.8658453114305,
35.8275154004107),
Survival = c(0,
0, 1, 1, 1, 1)))
data_2015 <- structure(list(Years_Diff_Surg_Death = c(34.4476386036961, 34.2559890485969,
0.621492128678987, 34.3874058863792,
34.3326488706365, 1.08145106091718), Survival = c(0,
1, 1, 0, 1, 0)))
data_2016 <- structure(list(Years_Diff_Surg_Death = c(2.90212183436003,
0.950034223134839,
33.9301848049281, 33.9110198494182, 33.8726899383984,
33.8535249828884),
Survival = c(1, 1, 0, 0, 0, 0)))
data_2017 <- structure(list(Years_Diff_Surg_Death = c(32.9911019849418, 3.34839151266256,
32.952772073922, 32.911704312115, 32.8761122518823, 0.791238877481177),
Survival = c(1, 0, 1, 1, 1, 1)))
set.seed(121)
mydate = seq(Sys.Date(), by = "day", length.out = 1800)
mystatus = sample(c("DATE_OF_SURGERY", "DATE_OF_DEATH"), 30, replace = TRUE)
mydf = data.frame(Date = mydate, Status = mystatus)
# Define UI for dataset viewer application
ui <- shinyUI(
pageWithSidebar(
# Application title.
headerPanel(""),
sidebarPanel(
sliderInput(inputId = "surv",
label = "Number of Years",
min = 0,
max = 5,
value = 1,
animate = animationOptions(interval=800, loop=TRUE)
)
),
mainPanel(
h1("Survival Plot"),
img(src = "logo-no-tag.png", height = 120, weight = 200),
fluidPage(
# Copy the line below to make a date selector
dateRangeInput(
inputId = "daterange",
label = "select the date range",
start = min(mydf$Date),
end = max(mydf$Date),
min = min(mydf$Date),
max = max(mydf$Date),
format = "yyyy/mm/dd",
separator = "to"
),
textOutput("startdate"),
textOutput("enddate"),
textOutput("range"),
tableOutput("subdata"),
hr(),
fluidRow(column(3, verbatimTextOutput("value"))),
fluidRow(column(3, verbatimTextOutput("value"))),
),
tabsetPanel(
tabPanel("Plot", plotOutput("plot_predicted")),
tabPanel("Model Summary", verbatimTextOutput("summary")),
tabPanel("Data", tableOutput("raw_data")),
tabPanel("Survival Curves for Each Year", plotOutput("plot_predicted_2")),
id = "tabs"
)
)
))
survfitdata4 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = myData)
server <- function(input, output, session) {
output$value <- renderPrint({ input$date })
output$plot_predicted <- renderPlot({
survobj1 <- with(myData, Surv(Years_Diff_Surg_Death, Survival))
fit1 <- survfit(survobj1~1, data = myData)
ggsurvplot(fit1, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none")
})
output$plot_predicted_2 <- renderPlot({
survobj_2013 <- with(data_2013, Surv(Years_Diff_Surg_Death, Survival))
survobj_2014 <- with(data_2014, Surv(Years_Diff_Surg_Death, Survival))
survobj_2015 <- with(data_2015, Surv(Years_Diff_Surg_Death, Survival))
survobj_2016 <- with(data_2016, Surv(Years_Diff_Surg_Death, Survival))
survobj_2017 <- with(data_2017, Surv(Years_Diff_Surg_Death, Survival))
surv_2013 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2013)
surv_2014 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2014)
surv_2015 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2015)
surv_2016 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2016)
surv_2017 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2017)
fit_comb <- list(s_2013 = surv_2013,
s_2014 = surv_2014,
s_2015 = surv_2015,
s_2016 = surv_2016,
s_2017 = surv_2017)
ggsurvplot_combine(fit_comb,
data_ECV,
xlab = ("Years"),
xlim = c(0,5),
break.x.by = 1)
})
output$summary <- renderPrint({
summary(survfitdata4)
})
output$raw_data <- renderTable({
myData
}, include.rownames = FALSE)
# returns the start date selected by the user
output$startdate <- renderText({
as.character(input$daterange[1]) # start date selected by user
})
# returns the end date selected by the user
output$enddate <- renderText({
as.character(input$daterange[2]) # End date selected by the user
})
# returns the text showing the date range selected by the user
output$range <- renderText({
paste("Selected date range is ", input$daterange[1], "to", input$daterange[2])
})
# returns the status count information based on selected date
output$subdata <- renderTable({
# subsetting using the dates selected by the user
s = subset(mydf, mydf$Date>= input$daterange[1] & mydf$Date<= input$daterange[2])
table(s$Status) # to get the counts on status for selected date range
})
}
You had the output object value
being used twice in the ui
.您在
ui
中使用了两次输出对象value
。 These IDs should be unique.这些 ID 应该是唯一的。 Also, made
myData
to be a data frame.此外,使
myData
成为数据框。 Try this尝试这个
library(rsconnect)
library(shiny)
library(survival)
library(survminer)
library(readxl)
myData <- data.frame(Years_Diff_Surg_Death = c(36.99383984, 2.584531143, 36.91991786, 36.89527721, 36.88158795, 36.82135524),
Survival = c(1L, 0L, 1L, 1L, 1L, 1L))
data_2013 <- structure(list(Years_Diff_Surg_Death = c(36.993839835729, 2.5845311430527,
36.9199178644764, 36.8952772073922, 36.8815879534565, 36.8213552361396),
Survival = c(1, 0, 1, 1, 1, 0)))
data_2014 <- structure(list(Years_Diff_Surg_Death = c(0.542094455852156, 5.19644079397673,
35.9561943874059,
35.917864476386, 35.8658453114305,
35.8275154004107),
Survival = c(0,
0, 1, 1, 1, 1)))
data_2015 <- structure(list(Years_Diff_Surg_Death = c(34.4476386036961, 34.2559890485969,
0.621492128678987, 34.3874058863792,
34.3326488706365, 1.08145106091718), Survival = c(0,
1, 1, 0, 1, 0)))
data_2016 <- structure(list(Years_Diff_Surg_Death = c(2.90212183436003,
0.950034223134839,
33.9301848049281, 33.9110198494182, 33.8726899383984,
33.8535249828884),
Survival = c(1, 1, 0, 0, 0, 0)))
data_2017 <- structure(list(Years_Diff_Surg_Death = c(32.9911019849418, 3.34839151266256,
32.952772073922, 32.911704312115, 32.8761122518823, 0.791238877481177),
Survival = c(1, 0, 1, 1, 1, 1)))
set.seed(121)
mydate = seq(Sys.Date(), by = "day", length.out = 1800)
mystatus = sample(c("DATE_OF_SURGERY", "DATE_OF_DEATH"), 30, replace = TRUE)
mydf = data.frame(Date = mydate, Status = mystatus)
# Define UI for dataset viewer application
ui <- shinyUI(
pageWithSidebar(
# Application title.
headerPanel(""),
sidebarPanel(
sliderInput(inputId = "surv",
label = "Number of Years",
min = 0,
max = 5,
value = 1,
animate = animationOptions(interval=800, loop=TRUE)
)
),
mainPanel(
h1("Survival Plot"),
img(src = "logo-no-tag.png", height = 120, weight = 200),
fluidPage(
# Copy the line below to make a date selector
dateRangeInput(
inputId = "daterange",
label = "select the date range",
start = min(mydf$Date),
end = max(mydf$Date),
min = min(mydf$Date),
max = max(mydf$Date),
format = "yyyy/mm/dd",
separator = "to"
),
textOutput("startdate"),
textOutput("enddate"),
textOutput("range"),
tableOutput("subdata"),
hr(),
fluidRow(column(3, verbatimTextOutput("value"))),
fluidRow(column(3, verbatimTextOutput("value2"))),
),
tabsetPanel(
tabPanel("Plot", plotOutput("plot_predicted")),
tabPanel("Model Summary", verbatimTextOutput("summary")),
tabPanel("Data", tableOutput("raw_data")),
tabPanel("Survival Curves for Each Year", plotOutput("plot_predicted_2")),
id = "tabs"
)
)
))
survfitdata4 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = myData)
server <- function(input, output, session) {
output$value <- renderPrint({ input$date })
output$value2 <- renderPrint({ input$date + 1 })
survobj1 <- with(myData, Surv(Years_Diff_Surg_Death, Survival))
output$plot_predicted <- renderPlot({
fit1 <- survfit(survobj1 ~ 1, data = myData)
ggsurvplot(fit1, conf.int = TRUE,
ylim = c(0,1),
xlim = c(0,5),
break.x.by = 1,
title = ("Years of Death After Surgery via Survival"),
xlab = ("Years"),
legend = "none")
})
output$plot_predicted_2 <- renderPlot({
survobj_2013 <- with(data_2013, Surv(Years_Diff_Surg_Death, Survival))
survobj_2014 <- with(data_2014, Surv(Years_Diff_Surg_Death, Survival))
survobj_2015 <- with(data_2015, Surv(Years_Diff_Surg_Death, Survival))
survobj_2016 <- with(data_2016, Surv(Years_Diff_Surg_Death, Survival))
survobj_2017 <- with(data_2017, Surv(Years_Diff_Surg_Death, Survival))
surv_2013 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2013)
surv_2014 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2014)
surv_2015 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2015)
surv_2016 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2016)
surv_2017 <- survfit(Surv(Years_Diff_Surg_Death, Survival) ~ 1, data = data_2017)
fit_comb <- list(s_2013 = surv_2013,
s_2014 = surv_2014,
s_2015 = surv_2015,
s_2016 = surv_2016,
s_2017 = surv_2017)
ggsurvplot_combine(fit_comb,
data_ECV,
xlab = ("Years"),
xlim = c(0,5),
break.x.by = 1)
})
output$summary <- renderPrint({
summary(survfitdata4)
})
output$raw_data <- renderTable({
myData
}, include.rownames = FALSE)
# returns the start date selected by the user
output$startdate <- renderText({
as.character(input$daterange[1]) # start date selected by user
})
# returns the end date selected by the user
output$enddate <- renderText({
as.character(input$daterange[2]) # End date selected by the user
})
# returns the text showing the date range selected by the user
output$range <- renderText({
paste("Selected date range is ", input$daterange[1], "to", input$daterange[2])
})
# returns the status count information based on selected date
output$subdata <- renderTable({
# subsetting using the dates selected by the user
s = subset(mydf, mydf$Date>= input$daterange[1] & mydf$Date<= input$daterange[2])
table(s$Status) # to get the counts on status for selected date range
})
}
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.