printing a output of a variable in shiny

Dear All,
I was wondering if someone could please help me? I am new to working in Shiny apps using Rstudio. I am facing an issue with the coding, I am printing a ggplot by uploading a CSV file which works fine. Issue I am facing is to print values of certain variables which show the parameters being passed to GGPLOT. I want to achieve following

  1. Printing the value of variables to browser underneath the plot or above plot
  2. Write a CSV file containing these variables and download it.

Hope I have explained my question correctly. I am copying sample code underneath


Global variables can go here


# Define the UI

ui <- bootstrapPage(
titlePanel("Survival analysis step1"),
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",

 # Horizontal line ----
 # Input: Checkbox if file has header ----
 checkboxInput("header", "Header", TRUE),
 # Input: Select separator ----
 radioButtons("sep", "Separator",
              choices = c(Comma = ",",
                          Semicolon = ";",
                          Tab = "\t"),
              selected = ","),
 # Input: Select quotes ----
 radioButtons("quote", "Quote",
              choices = c(None = "",
                          "Double Quote" = '"',
                          "Single Quote" = "'"),
              selected = '"'),
 # Horizontal line ----
 # Input: Select number of rows to display ----
 radioButtons("disp", "Display",
              choices = c(Head = "head",
                          All = "all"),
              selected = "head")


Define the server code

server <- function(input, output) {
output$plot <- renderPlot({


# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
    testdf <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)

#testdf<-read.csv("objective.csv",header = TRUE,na.strings=0)
testdf$objl<-ifelse(as.numeric(testdf$obj_censor) ==2, as.numeric(testdf$objn), as.numeric(testdf$objl))
testdf$modified_objn<-ifelse(as.numeric(testdf$obj_censor) ==2, as.numeric(testdf$objl), as.numeric(testdf$objn))
testdf$Survial_analysis_obj<-Surv(as.numeric(testdf$modified_objn),as.numeric(testdf$objl), as.numeric(testdf$obj_censor), type="interval")
model_log_N_obj<-survreg(formula= testdf$Survial_analysis_obj~1, data=testdf, dist="lognormal")
predmodel_log_N_obj<-predict(model_log_N_obj, newdata=new1, type='quantile', p=seq(0.001,0.99, by=0.0001),

    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="Log-normal probability distribution models (objective symptoms)", 
     xlab="dose of peanut protein (mg)",
     ylab = "probability",  
     log="x", xaxt = "n", yaxt = "n") 
lines(exp(log(predmodel_log_N_obj$fit)+1.96* (predmodel_log_N_obj$ 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$ predmodel_log_N_obj$fit),seq(0.001,0.99, by=0.0001), type="l", col=2, lty=2)

log_N_obj1<-predict(model_log_N_obj, newdata=new1, type='quantile', p=0.01, #0.18
#lower confidence level - 0.04
log_N_CL_obj1 <- exp(log(log_N_obj1$fit)-1.96* (log_N_obj1$ log_N_obj1$fit)
#upper confidence level - 0.75
log_N_CU_obj1<- exp(log(log_N_obj1$fit)+1.96* (log_N_obj1$ log_N_obj1$fit)

########### Trying to print and save these variables and write data frame into csv file

visFun <- renderPrint(env=parent.frame(),{

Return a Shiny app object

shinyApp(ui = ui, server = server)

You need something in the server to render the text into output list, then something in the ui to display this.

This page has an example

By the way, to prevent your code looking terrible here, put three backticks ``` on a separate line before it and after it. This will create a proper code block.

Thanks Woodward, I will give it a try. in fact I was trying to do that I did not know how to render it on server and ui to display that because all calculations were done while I was trying to display the plot.

Hi Woodward,
I tried to do that, similar way and still my code contains the code the way I was doing but still no solution.

It gives an error that variable does not exist.
What I did in ui

and then

output$selected_var <- renderText(ED01_log_N_obj)
or even tried following line as well


but still no issue. If you see in above main codes that the variables that does the printing of plot contains the variable. Do you think? I should try and move these variable outside renderPlot??


That should work provided ED01_log_N_obj is a string. If it's not a string you need to turn it into string. It looks like it might be a model object? Maybe print(ED01_log_N_obj)?

The curly brackets {} allow you to have more than one statement or a whole block of code in place of the expression, and it will render the last thing in that block of code.

And format your code correctly, it looks like a mess on here. Put the backticks before and after like I told you. RStudio has an Addin on the tool bar, "Style active file", maybe you should use it.

Thanks @woodward, I will give it a try. Because I am posting first time so I did not know how to copy, paste code inside the editor. Thanks a lot for your suggestions.

The main editor features to know are single backticks around inline code like this and triple backticks aruond block code like

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.