1

I have an r script includes a Identify_IP() that returns a list of dataframe and a ggplot. I want to call the script and render both the dataframe and the plot.

This is Identify_IP() function. I took off unrelative code and kept only the plot, lines and ggplot code to give a clear example of my type of ggplot.

library(ggplot2)
library(matrixStats)
library(fda.usc)

#df <- read.table("name.XLS", header = FALSE)

Identify_IP = function(df1){
  mlearn <- df1[,'V7']
     formul <- plot(blue_curve$x, blue_curve$y * 30, type = 'l', col = 'blue')

  formula_deriv <- lines(blue_curve$x, red_curve$y1 * 30, col = 'red')

  p <- ggplot(df1, aes(blue_curve$x)) + 
    geom_line(aes(y = blue_curve$y, colour = "0 Deriv")) + 
    geom_line(aes(y = red_curve$y1, colour = "1st Deriv")) + 
    geom_vline(xintercept = x_loc) + geom_hline(yintercept = 0)


  return(list(df1,p))

}

Now, this is a modified Shiny code based on amrr and micstr suggestion.

 source('InflectionP2.R', local = TRUE)

library(ggplot2)
library(shiny)
runApp(
  list(
    ui = fluidPage(
      titlePanel("Upload your file"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose xls file',
                    accept = c(".XLS")),

      actionButton("btn", "Update Table"),
      actionButton("btn1", "Display Plot")

    ),

    mainPanel(

      tableOutput('what'),
      plotOutput('pl'))
  )

)
,

server = function(input, output, session){

  dataOP <- reactive({
    inFile <- input$file1
    if (is.null(input$file1))
      return(NULL)

    dfs <- Identify_IP(read.table(inFile$datapath))

    return(dfs)
  })


  observeEvent(input$btn, output$what <-  renderTable({

    dataOP()[[1]]

  }))

  observeEvent(input$btn1, output$pl <- renderPlot({

    pp <- dataOP()

    pp[[2]]

  }))             
}))

This was really helpful in teaching me how to call r script in reactive(). And it makes sense to me. Yet, it render the table but the Display Plot button is not rendering the plot. Does my ggplot in Identify_IP function has anything to do with not being able to display the plot? I also tried print(ggplot(pp[[2]])) and still the same.

12
  • How are you returning ggplot in that function? just an assignment like p <- ggplot() ? Commented Nov 9, 2017 at 7:01
  • And I couldn't see any plot in dataOP() It's just dfs for both tble and plt Commented Nov 9, 2017 at 7:02
  • Yes for the first question. As for the second, this is what I am stuck with. I really don't know how to pass the plot. I tried different methods and nothing worked so far. Commented Nov 9, 2017 at 7:05
  • 1
    Could you please share the code where you create ggplot() Commented Nov 9, 2017 at 7:09
  • Let me understand, you are already creating ggplot inside Identify_IP or you want to create one inside the reactive function in server.R? Commented Nov 9, 2017 at 7:10

2 Answers 2

1

I managed to get this working.

Note I used the internal data set iris and made a toy Identify_IP function as I do not have your code.

Note you still need to choose a file to trigger the events but it will ignore that file and use iris data.

Workaround I used [[1]] to get the table not dataOP()$tble

CODE

library(shiny)
library(ggplot2)

# source('InflectionP2.R', local = TRUE)
# MAKE TEST FUNCTION
Identify_IP <- function(mydata) {

  #shrink data
  tble <- head(mydata)

  plt <-  ggplot(data = head(mydata),
                 mapping = aes(y = Sepal.Length,
                               x = Petal.Length)) + geom_point()

  return(list(tble, plt))
}




runApp(
  list(
    ui = fluidPage(
      titlePanel("Upload your file"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose xls file',
                    accept = c(".XLS")),

          actionButton("btn", "Update Table"),
          actionButton("btn1", "Display Plot")

        ),

        mainPanel(

          tableOutput('what'),
          plotOutput('pl'))
      )

    )
    ,

    server = function(input, output, session){

      dataOP <- reactive({
        inFile <- input$file1
        if (is.null(input$file1))
          return(NULL)

        # ORIGINAL dfs <- Identify_IP(read.table(inFile$datapath))
        # using internal dataset for example
        dfs <- Identify_IP(iris)

        # ORIGINAL list(tble = dfs, plt = dfs)
        # lets just return your dfs, its already a list in code above
        return(dfs)
      })


      observeEvent(input$btn, output$what <-  renderTable({

        #print(dataOP()) # debug line that led to [[1]] idea
        # ORIGINAL  dataOP()$tble
        # just say first in list
        dataOP()[[1]]

        }))

      observeEvent(input$btn1, output$pl <- renderPlot({


        #ggplot(dataOP()$plt)
        # since already a plot just need to index it
        # I found [[2]] worked better than explicit dataOP()$plt
        pp <- dataOP()

        pp[[2]]

      }))             
    }))

RESULT

table and plot example

Voila!

Sign up to request clarification or add additional context in comments.

1 Comment

It makes sense to me. It still doesn't work. I will modify my code above and display Identify_IP() and the ggplot it returns.
0

1) Try print (ggplot(dataOP()$plt))

Take a look at this answer I wrote.

2) Sorry its hard to interpret without your ggplot code bit and data. Given @amrrs questions can you try debug in your Shiny code with print() and str() temporary lines to see what your data is returning. i.e.

print(dataOP()$plt)
str(dataOP())

Worse case, try split your code in two. So Identify_IP code to do the data leg and then make a Print_IP with the ggplot code that just returns the plot. It might rule out your chart is not the problem.

3) Take a look at reactiveValues()

https://shiny.rstudio.com/reference/shiny/0.11/reactiveValues.html

It "bakes" a result that was reactive. The type coming out of your chart may be a reactive type not a chart type. Perhaps share any error messages you are getting.

2 Comments

Not working. I'm wondering if I'm passing the objects correctly in the List inside reactive function. I think the problem is that I don't know how to pass the arguments.
MarJamil i think this answer may be very useful - stackoverflow.com/a/24899084/4606130. Inside your renderPlot() take dd <- dataOP()$plt do your p <- ggplots(dd etc) on dd and then print(p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.