【问题标题】:Printing output of a variable变量的打印输出
【发布时间】:2019-11-09 05:45:06
【问题描述】:

我正在 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 文件中绘制+打印的变量。

【问题讨论】:

  • 首先,我认为您在服务器之后错过了{。其次,您将所有内容都放在 renderPlot 下,这可能不是一种做法。您正在做两件不同的事情,一件是具有拟合概率和预测区间的图,另一件是数据框。没有任何实际数据很难提供帮助。我建议你把数据框部分移到绘图之前,你可以利用数据框来绘图。而且,您可能需要 actionButton 来满足您的最后一个请求。
  • 嗨@wxxyyyzz,感谢您的回复。首先,是的,我在复制粘贴代码时错过了 {。因为这是我第一次使用闪亮的环境,所以我只是将所有代码移到了 renderPlot 块中。在渲染图之外使用数据框是指从 read.CSV 到 predict() 的行吗?如果是,我会尝试移动它们。关于数据集示例,请您告诉我应该如何共享数据集,因为我不完全熟悉 stackoverflow 环境并且不知道如何附加数据集。
  • 要在 stackoverflow 上共享数据帧,请使用函数 dput。例如,dput(mtcars) 给出了生成数据框 mtcars 的代码(只需将其替换为您的数据框的名称,或您的数据框的子集)
  • 嗨@wxxyyyzz 我已经用数据集更新了示例,希望现在很容易解决我的问题。
  • 嗨@bretauv,谢谢我用put根据你的建议分享我的数据集。

标签: r ggplot2 shiny


【解决方案1】:

我猜你只在控制台而不是 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)

【讨论】:

  • 亲爱的@wxxyyyzz,非常感谢。它工作正常。我会努力改进的。
  • 亲爱的@wxxyyyzz,我有一个简单的问题。我按照您的建议使用 output$selected_var
猜你喜欢
  • 1970-01-01
  • 2020-02-11
  • 2023-02-07
  • 1970-01-01
  • 2016-12-11
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多