R Shiny - dataset load in a first chunk doesn't exist in a second chunk …?

 ̄綄美尐妖づ 提交于 2020-01-06 05:01:48

问题


I have a strange error in a shiny app I built with the library learnr. An error "Object not found" about an object I just loaded and just visualized (meaning the object exists no ?)

Although I don't have a reproducible example, some of you will maybe understand what is creating the error :

  • I have a first chunk {r load} that loads a dataset. There is no error here, I can even visualize the dataset (screenshot below)

  • Then I have a second chunk, where I would like to manipulate the dataset. But it tells me dataset doesn't exist ! How it could be possible, I just visualized it one chunk before ?! ...

I don't understand how a dataset could be exists in a chunk, and not in another. Does it mean the dataset isn't loaded in the global environment ? Is it a problem with the learnr library ?

Maybe someone will have an idea, or something I could test. Thank you in advance.

EDIT: The problem is about the environment/workspace. In the first chunk, even if I load the dataset, it is not store in the environment. I tested the function ls() in a second chunk, and it tells me there is no object in the workspace. The loaded dataset is not here, I don't know why ...


回答1:


In my opinion, shiny doesn't store any data. You have to pass it manually from one chunk to other as follow (only adding the code snippet from server):

server <- function(input, output, session) {
  output$heat <- renderPlotly({
    Name<-c("John","Bob","Jack")
    Number<-c(3,3,5)
    Count<-c(2,2,1)
    NN<-data.frame(Name,Number,Count)
    render_value(NN) # You need function otherwise data.frame NN is not visible
    # You can consider this as chunk 1
  })

  render_value=function(NN){
   # Here your loaded data is available
     head(NN)
   # You can consider this as chunk 2 
    })  
  }           
}

shinyApp(ui, server)

You can find full code here: Subset a dataframe based on plotly click event

OR

Create global.R file as suggested here and follow this URL: R Shiny - create global data frame at start of app



来源:https://stackoverflow.com/questions/54632339/r-shiny-dataset-load-in-a-first-chunk-doesnt-exist-in-a-second-chunk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!