R 打印变量的输出

R 打印变量的输出,r,ggplot2,shiny,R,Ggplot2,Shiny,我正在Rstudio中使用Shining编写我的第一个代码,它需要一个文件输入(CSV文件并在主面板中打印绘图。打印绘图后,我尝试打印传递给绘图的变量值,以观察它们的值,但我无法做到。它在Rstudio控制台中打印它们。其次,我希望创建这些变量的数据框,并下载包含这些值的CSV文件。希望我已经解释过查询。我正在复制下面的代码 我可以在浏览器中打印绘图。但变量正在控制台(rstudio)中打印。我需要在绘图下方的浏览器窗口中打印这些变量,并下载包含这些变量的数据帧的CSV文件。若有人能指导我,我将

我正在Rstudio中使用Shining编写我的第一个代码,它需要一个文件输入(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)
结构(列表(点1=c(999999433.3339991433.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),点2=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),输出1=c(NA,
NA,1L,NA,1L,1L,NA,1L,NA,NA,NA,NA,NA,1L,1L,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,1L,1L,1L,1L,1L,1L,
1L,1L),类型=c(NA,NA,3L,NA,3L,3L,3L,NA,3L,NA,
NA,NA,3L,3L,3L,NA,NA,NA,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,3L,3L,3L,3L,3L,3L),名称=c(“点1”,“点2”,
“OUTP1”,“Types”),class=“data.frame”,row.names=c(NA,-51L
))
选项(scipen=999)
图书馆(生存)

ui我猜您只在控制台中获得了输出,而没有在控制台中获得输出,因为您使用的是打印,而不是将它们放入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)
库(生存)
图书馆(闪亮)

ui我猜您只在控制台中获得了输出,而没有在控制台中获得输出,因为您使用的是打印,而不是将它们放入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)
库(生存)
图书馆(闪亮)

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