我在创建闪亮的应用程序方面是非常新的。需要一些帮助来解决错误。因此,我在文件final.csv
中有如下数据:
structure(list(Samples = structure(c(1L, 12L, 23L, 34L, 45L,
46L, 47L, 48L, 49L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L), .Label = c("Sample1", "Sample10", "Sample11",
"Sample12", "Sample13", "Sample14", "Sample15", "Sample16", "Sample17",
"Sample18", "Sample19", "Sample2", "Sample20", "Sample21", "Sample22",
"Sample23", "Sample24", "Sample25", "Sample26", "Sample27", "Sample28",
"Sample29", "Sample3", "Sample30", "Sample31", "Sample32", "Sample33",
"Sample34", "Sample35", "Sample36", "Sample37", "Sample38", "Sample39",
"Sample4", "Sample40", "Sample41", "Sample42", "Sample43", "Sample44",
"Sample45", "Sample46", "Sample47", "Sample48", "Sample49", "Sample5",
"Sample6", "Sample7", "Sample8", "Sample9"), class = "factor"),
years = c(1.301369863, 0.4, 1.054794521, 0.134246575, 0.794520548,
3.287671233, 3.646575342, 3.887671233, 3.646575342, 3.619178082,
3.575342466, 2.02739726, 3.523287671, 3.742465753, 2.926027397,
3.8, 1.161643836, 1.380821918, 3.087671233, 3.104109589,
3.084931507, 2.887671233, 2.778082192, 2.728767123, 3.043835616,
1.210958904, 2.704109589, 2.742465753, 2.635616438, 2.536986301,
2.432876712, 2.794520548, 1.967123288, 1.84109589, 1.838356164,
2.726027397, 2.430136986, 2.257534247, 1.876712329, 2.010958904,
0.698630137, 2.090410959, 2.098630137, 2.01369863, 1.717808219,
1.81369863, 2.057534247, 2.032876712, 1.989041096), patient.vital_status = c(0L,
0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L,
1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), A1BG = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("high",
"low"), class = "factor"), A1CF = structure(c(1L, 1L, 1L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L), .Label = c("high", "low"), class = "factor")), class = "data.frame", row.names = c(NA,
-49L))
使用以上信息,我正在尝试创建一个应用程序,但是在使其更正时遇到了一些麻烦。下面是我使用的代码:
library(shiny)
library(shinyjs)
library(tidyverse)
library(survminer)
ui <- fluidPage(
titlePanel("survival"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "thegene", label = "Gene", choices = c("A1BG", "A1CF"), selected = "A1CF"),
radioButtons(inputId = "FileType", label = "Select the file type", choices = list("png", "pdf"), selected = "pdf"),
width = 3
),
mainPanel(
plotOutput("plot"),
downloadButton(outputId = "downloadPlot", label = "Download the plot"),
width = 9
)
)
)
server <- function(input, output, session) {
final <- read.csv("final.csv")
genes <- as.factor(names(final[c(4:5)]))
vals <- reactiveValues()
alldat <- reactive({
choices <- genes
selected <- isolate(input$thegene)
if (!selected %in% choices) selected <- choices[1]
updateSelectInput(session, "thegene", choices = choices, selected = selected)
final
})
dat <- reactive({
x <- alldat()
x[ x$variable == input$thegene,,drop=FALSE]
})
output$plot <- renderPlot({
fit <- survfit(as.formula(paste0("Surv(years, patient.vital_status) ~", names(final[c(4:5)]))),
data = final)
gg <-ggsurvplot(fit,
pval = TRUE, conf.int = FALSE,
risk.table = TRUE, # Add risk table
risk.table.col = "strata", # Change risk table color by groups
linetype = "strata", # Change line type by groups
surv.median.line = "hv", # Specify median survival
ggtheme = theme_bw(), # Change ggplot2 theme
palette = c("#FF0027", "#060606"),
xlim = c(0,10),
break.x.by = 3,
xlab="Time in years",
risk.table.y.text.col = T, # colour risk table text annotations.
risk.table.y.text = FALSE)
vals$gg <- gg
print(gg)
})
output$downloadPlot <- downloadHandler(
filename = function() {
paste(input$thegene, input$FileType,sep=".")
},
# content is a function with argument file. content writes the plot to the device
content = function(file){
if(input$FileType=="png")
png(file, units="in", width=5, height=5, res=300)
else
pdf(file, width = 5, height = 5, onefile = FALSE)
print(vals$gg)
dev.off()
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
运行应用程序时使用上面的代码,我遇到了一个错误。
Error: object 'final' not found
我看到上面的错误,因此在运行应用程序之前,我加载了final.csv
,还为genes
分配了第4列和第5列的列名。然后,当我运行该应用程序时,它似乎正常工作,但是我只能看到A1BG
基因输出。当我在界面中选择A1CF
基因时,没有看到该输出,而是再次看到了A1BG
的输出。
答案 0 :(得分:1)
问题来自if
中的alldata
条件:您必须用if(!selected %in% choices)
替换if (choices != NULL)
,因为choices
仅是NULL
如果数据框final
不存在(即未导入)。
您现在在survminer
软件包中遇到了一些功能上的麻烦,但这是另一个问题,并且肯定是由于某些功能上的错字造成的。
答案 1 :(得分:1)
该图未更新的原因是因为您使用names(final[c(4:5)]))
作为survfit
中的预测变量,而我认为您想使用input$thegene
来代替。似乎按照预期的方式工作。
其他建议/注释:
alldat
更改为observe
,而只是在final
反应式中调用dat
,因为您并没有以任何方式对其进行操作。首先反应。survival
软件包未显式加载,而"variable"
名称中不存在final
)。也许也值得一看。