R Shiny-第一个块中的数据集加载在第二个块中不存在...? [英] R Shiny - dataset load in a first chunk doesn't exist in a second chunk ...?
问题描述
我使用库 learnr 构建的闪亮应用程序中出现一个奇怪的错误.关于我刚刚加载并可视化的对象的错误"找不到对象"(表示该对象不存在?)
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 :
-
我有第一个块{r load},它加载数据集.这里没有错误,我什至可以可视化数据集(下面的屏幕截图)
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.
问题在于环境/工作空间.在第一个块中,即使我加载数据集,也不会将其存储在环境中.我在第二个块中测试了功能ls()
,它告诉我工作空间中没有对象.加载的数据集不在这里,我不知道为什么...
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 ...
推荐答案
在我看来,Shiny不存储任何数据.您必须按照以下说明将其手动从一个块传递到另一块(仅添加server
中的代码段):
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)
您可以在此处找到完整的代码:子集a基于可点击事件的数据框
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
这篇关于R Shiny-第一个块中的数据集加载在第二个块中不存在...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!