打印变量的输出

时间:2019-11-05 13:27:05

标签: r ggplot2 shiny

我正在Rstudio中使用Shiny编写我的第一个代码,它需要一个文件输入(CSV文件,并在主面板中打印绘图。打印绘图后,我试图打印传递给绘图以观察其变量的变量的值我无法执行的值,它将在Rstudio控制台中打印出来;其次,我正在寻找创建这些变量的数据框,并下载包含这些值的CSV文件。希望我已经解释了该查询。我正在复制下面的代码

我能够在浏览器中打印绘图。但是变量正在控制台(rstudio)中打印出来,我需要在绘图下的浏览器窗口中使用它们,并下载包含这些变量数据框的CSV文件。如果有人可以指导我,我将不胜感激。

structure(list(Point1 = c(9999, 9999, 433.333, 9999, 1433.333, 
33.333, 4283.333, 9999, 33.333, 9999, 9999, 9999, 9999, 0.033, 
1323.333, 883.333, 9999, 9999, 9999, 9999, 433.333, 1433.333, 
0.633, 9999, 9999, 9999, 33.333, 9999, 133.333, 433.333, 433.333, 
0.003, 4023.333, 33.333, 423.333, 1323.333, 423.333, 1323.333, 
4323.333, 2073.333, 1323.333, 1323.333, 1323.333, 1323.333, 1323.333, 
33.333, 123.333, 3363.333, 123.333, 0.333, 423.333), Point2 = c(4433.333, 
4433.333, 133.333, 4133.333, 433.333, 3.333, 1283.333, 4433.333, 
3.333, 4433.333, 4433.333, 4433.333, 4433.333, 0.003, 423.333, 
433.333, 4433.333, 4433.333, 4433.333, 4433.333, 133.333, 433.333, 
0.333, 4358.333, 4433.333, 4433.333, 3.333, 4433.333, 33.333, 
133.333, 133.333, NA, 1323.333, 3.333, 123.333, 423.333, 123.333, 
423.333, 1323.333, 1323.333, 423.333, 423.333, 423.333, 423.333, 
423.333, 3.333, 33.333, 1323.333, 33.333, 0.033, 123.333), OUTP1 = c(NA, 
NA, 1L, NA, 1L, 1L, 1L, NA, 1L, NA, NA, NA, NA, 1L, 1L, 1L, NA, 
NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L, NA, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), Types = c(NA, NA, 3L, NA, 3L, 3L, 3L, NA, 3L, NA, NA, 
NA, NA, 3L, 3L, 3L, NA, NA, NA, NA, 3L, 3L, 3L, NA, NA, NA, 3L, 
NA, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("Point1", "Point2", 
"OUTP1", "Types"), class = "data.frame", row.names = c(NA, -51L
))

options(scipen=999)
library(survival)

ui <- bootstrapPage(
titlePanel("Survival analysis step1"),
tags$hr(),
sidebarPanel(
fileInput("file1", "Choose CSV File",
           multiple = FALSE,
           accept = c("text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv")),

tags$hr(),

checkboxInput("header", "Header", TRUE),

 radioButtons("sep", "Separator",
              choices = c(Comma = ",",
                          Semicolon = ";",
                          Tab = "\t"),
              selected = ","),

 radioButtons("quote", "Quote",
              choices = c(None = "",
                          "Double Quote" = '"',
                          "Single Quote" = "'"),
              selected = '"'),

 tags$hr(),

 radioButtons("disp", "Display",
              choices = c(Head = "head",
                          All = "all"),
              selected = "head")
 ),
mainPanel(
plotOutput('plot'),
textOutput("selected_var")
     )
               )
server <- function(input, output) {
output$plot <- renderPlot({

req(input$file1)

testpeanut1 <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)
    print(dput(testpeanut1))
testpeanut1[is.na(testpeanut1)]<-"0"
testpeanut1$modified_Point1<-ifelse(as.numeric(testpeanut1$Types) ==2, as.numeric(testpeanut1$Point2), as.numeric(testpeanut1$Point1))
testpeanut1$modified_Point2<-ifelse(as.numeric(testpeanut1$Types) ==2, as.numeric(testpeanut1$Point1), as.numeric(testpeanut1$Point2))
set.seed(1234567)
testpeanut1$Survial_analysis_obj<-Surv(as.numeric(testpeanut1$modified_Point2),as.numeric(testpeanut1$modified_Point1), as.numeric(testpeanut1$Types), type="interval")
model_log_N_obj<-survreg(formula= testpeanut1$Survial_analysis_obj~1, data=testpeanut1, dist="lognormal")
new1<-data.frame(1)
predmodel_log_N_obj<-predict(model_log_N_obj, newdata=new1, type='quantile', p=seq(0.001,0.99, by=0.0001), se.fit=TRUE)


    plot(predmodel_log_N_obj$fit, seq(0.001,0.99, by=0.0001), 
     type="l", col=2, lty=1, lwd=2, xlim=c(0.00001,100000000), 
     main="Plot description", 
     xlab="Caption",
     ylab = "probability",  
     log="x", xaxt = "n", yaxt = "n") 
lines(exp(log(predmodel_log_N_obj$fit)+1.96* (predmodel_log_N_obj$se.fit)/ predmodel_log_N_obj$fit),seq(0.001,0.99, by=0.0001), type="l", col=2, lty=2)
lines(exp(log(predmodel_log_N_obj$fit)-1.96* (predmodel_log_N_obj$se.fit)/ predmodel_log_N_obj$fit),seq(0.001,0.99, by=0.0001), type="l", col=2, lty=2)

ED01_log_N_obj<-predict(model_log_N_obj, newdata=new1, type='quantile', p=0.01, se.fit=TRUE) #0.18
print(ED01_log_N_obj)
ED01_log_N_CL_obj <- exp(log(ED01_log_N_obj$fit)-1.96* (ED01_log_N_obj$se.fit)/ ED01_log_N_obj$fit)
print(ED01_log_N_CL_obj)
#ED01 upper confidence level - 0.75
ED01_log_N_CU_obj<- exp(log(ED01_log_N_obj$fit)+1.96* (ED01_log_N_obj$se.fit)/ ED01_log_N_obj$fit)
print(ED01_log_N_CU_obj)

df<-data.frame(ED01_log_N_CU_obj)
df$ED01_log_N_CL_obj<-ED01_log_N_CL_obj
df$ED01_log_N_objfit<-ED01_log_N_obj$fit

dput(testpeanut1)    
})

output$selected_var <- renderText("ED01_log_N_obj") 
}

shinyApp(ui = ui, server = server)

单击按钮等或作为下载链接,可在浏览器和CSV文件中打印并打印变量。

1 个答案:

答案 0 :(得分:0)

我猜您只在控制台中获得了输出,而不是Shiny,因为您使用了print而不是将它们放入renderXXX。我尝试将您更新的数据用作csv输入,以下应该可以工作。数据后有“()”,因为它是一个响应值,您可以像函数一样调用它。另外,您可能需要编辑下载文件的扩展名。我没有时间清理代码,因此仍然有冗余。希望它会有所帮助。

library(survival)
library(shiny)

ui <- bootstrapPage(
    titlePanel("Survival analysis step1"),
    tags$hr(),
    sidebarPanel(
        fileInput("file1", "Choose CSV File",
                  multiple = FALSE,
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")),

        tags$hr(),

        checkboxInput("header", "Header", TRUE),

        radioButtons("sep", "Separator",
                     choices = c(Comma = ",",
                                 Semicolon = ";",
                                 Tab = "\t"),
                     selected = ","),

        radioButtons("quote", "Quote",
                     choices = c(None = "",
                                 "Double Quote" = '"',
                                 "Single Quote" = "'"),
                     selected = '"'),

        tags$hr(),

        radioButtons("disp", "Display",
                     choices = c(Head = "head",
                                 All = "all"),
                     selected = "head"),

        downloadButton("downloadData", "Download")
    ),
    mainPanel(
        plotOutput('plot'),
        tableOutput("selected_var")
    )
)
server <- function(input, output) {

    data <- reactive({
        req(input$file1)

        testpeanut1 <- read.csv(input$file1$datapath,
                                header = input$header,
                                sep = input$sep,
                                quote = input$quote)
        testpeanut1[is.na(testpeanut1)] <- "0"
        testpeanut1$modified_Point1 <- ifelse(as.numeric(testpeanut1$Types)==2, as.numeric(testpeanut1$Point2), as.numeric(testpeanut1$Point1))
        testpeanut1$modified_Point2 <- ifelse(as.numeric(testpeanut1$Types)==2, as.numeric(testpeanut1$Point1), as.numeric(testpeanut1$Point2))
        testpeanut1$Survial_analysis_obj <- Surv(as.numeric(testpeanut1$modified_Point2),as.numeric(testpeanut1$modified_Point1), as.numeric(testpeanut1$Types), type="interval")

        model_log_N_obj<-survreg(formula= testpeanut1$Survial_analysis_obj~1, data=testpeanut1, dist="lognormal")
        new1<-data.frame(1)
        predmodel_log_N_obj<-predict(model_log_N_obj, newdata=new1, type='quantile', p=seq(0.001,0.99, by=0.0001), se.fit=TRUE)

        ED01_log_N_obj <- predict(model_log_N_obj, newdata=new1, type='quantile', p=0.01, se.fit=TRUE) #0.18
        ED01_log_N_CL_obj <- exp(log(ED01_log_N_obj$fit)-1.96* (ED01_log_N_obj$se.fit)/ ED01_log_N_obj$fit)
        #ED01 upper confidence level - 0.75
        ED01_log_N_CU_obj <- exp(log(ED01_log_N_obj$fit)+1.96* (ED01_log_N_obj$se.fit)/ ED01_log_N_obj$fit)

        dfOut<-data.frame(ED01_log_N_CU_obj)
        dfOut$ED01_log_N_CL_obj<-ED01_log_N_CL_obj
        dfOut$ED01_log_N_objfit<-ED01_log_N_obj$fit

        return(list(df = testpeanut1,
                    predmodel = predmodel_log_N_obj,
                    summary = dfOut))
    })


    output$plot <- renderPlot({

        plot(data()$predmodel$fit, seq(0.001,0.99, by=0.0001), 
             type="l", col=2, lty=1, lwd=2, xlim=c(0.00001,100000000), 
             main="Plot description", 
             xlab="Caption",
             ylab = "probability",  
             log="x", xaxt = "n", yaxt = "n") 
        lines(exp(log(data()$predmodel$fit)+1.96* (data()$predmodel$se.fit)/ data()$predmodel$fit),seq(0.001,0.99, by=0.0001), type="l", col=2, lty=2)
        lines(exp(log(data()$predmodel$fit)-1.96* (data()$predmodel$se.fit)/ data()$predmodel$fit),seq(0.001,0.99, by=0.0001), type="l", col=2, lty=2)

    })

    output$selected_var <- renderTable(data()$summary)

    output$downloadData <- downloadHandler(

        filename = "output.csv",
        content = function(file) {
            write.csv(data()$df, file, row.names = FALSE)
        },
        contentType = "text/csv"
    )
}

shinyApp(ui = ui, server = server)