R 根据用户输入的数据更新绘图
我有一个闪亮的应用程序,它基本上解析用户上传的R 根据用户输入的数据更新绘图,r,shiny,R,Shiny,我有一个闪亮的应用程序,它基本上解析用户上传的.txt文件(请参阅我询问的有关数据输入和解析功能的问题),然后通过调用位于我的shinyServer调用服务器.R上方的一些绘图函数来生成多个绘图 在我的server.R脚本中调用的函数是: parseR() # Returns a dataframe (which is the result of parsing the user input entered in `fileInput)
.txt
文件(请参阅我询问的有关数据输入和解析功能的问题),然后通过调用位于我的shinyServer
调用服务器.R
上方的一些绘图函数来生成多个绘图
在我的server.R脚本中调用的函数是:
parseR() # Returns a dataframe (which is the result of parsing
the user input entered in `fileInput) ([see here][1] for details)
senderPosts() # Returns a plot that is shown in tabPanel 1
wordFreq() # Returns a plot that is shown in tabPanel 2
chatCloud() # Returns a plot that is shown in tabPanel 3
我可以成功地在tabPanel 1中获得绘图,以显示parseR()返回的数据帧中某个因子的级别,但我不确定如何使用它来实际更新绘图
我的问题是,如何根据用户输入更新绘图?
这是server.R
:
shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- parseR(file=inFile$datapath) # Call my parser function
updateSelectInput(session, inputId = 'sender', label = 'Sender',
choices = levels(df$sender), selected = levels(df$sender))
return(df)
})
# Main page
output$contents <- renderTable({
head(data(), 25)
})
# tabPanel 1
output$postCount <-renderPlot({
senderPosts(file=input$file1$datapath)
})
# tabPanel 2
output$wordCount <-renderPlot({
wordFreq(file=input$file1$datapath)
})
# tabPanel 3
output$chatCloud <-renderPlot({
chatCloud(file=input$file1$datapath)
})
})
正如我首先提到的,您不希望将updateInput保留在被动函数中。这是因为反应式是惰性的。它们最好放在渴望评估的观察者(
观察
或观察事件
)中
然后,您可以通过input$'inputId'
我也会将绘图计算放在反应函数中,但这不是必需的
shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- parseR(file=inFile$datapath) # Call my parser function
return(df)
})
observe({
df = data()
updateSelectInput(session, inputId = 'sender', label = 'Sender',
choices = levels(df$sender), selected = levels(df$sender))
})
# Main page
output$contents <- renderTable({
head(data(), 25)
})
# tabPanel 1
output$postCount <-renderPlot({
senderPosts(file=input$file1$datapath,newParamter = input$sender)
})
# tabPanel 2
output$wordCount <-renderPlot({
wordFreq(file=input$file1$datapath)
})
# tabPanel 3
output$chatCloud <-renderPlot({
chatCloud(file=input$file1$datapath)
})
})
shinyServer(功能(输入、输出、会话){
数据使用反应值data()
作为传递给renderPlot
中的绘图函数的数据参数。每当data()时,该参数将重新绘图
已更新。渲染函数通常是反应式的。这意味着每当输入变量更改其中的值时,它们都应自动重新计算。此外,您不希望在反应式函数中有更新输入函数,最好将其放置在单独的观察(事件)中函数。@BertilNestorius-我不确定我是否理解。您是否可以发布一个描述作为答案(或描述的链接)?@Nate-我传递给绘图函数的数据帧是由parseR()生成的。我不确定如何将我的选择传播到parseR()中函数。完全混乱!我已经按照您的建议调整了我的shinySever
调用:senderPosts(file=input$file1$datapath,user=input$sender)
,我的绘图函数现在接受输入$sender
:senderPosts否,它不起作用。当我隔离这两个函数时,它们按预期运行,并在user='user'
上生成一个经过过滤的绘图,所以它一定是在shinySever
调用中发生了什么。有什么想法吗?你的ui.r看起来像什么吗?啊-我一直在想谢谢你的帮助!
shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- parseR(file=inFile$datapath) # Call my parser function
return(df)
})
observe({
df = data()
updateSelectInput(session, inputId = 'sender', label = 'Sender',
choices = levels(df$sender), selected = levels(df$sender))
})
# Main page
output$contents <- renderTable({
head(data(), 25)
})
# tabPanel 1
output$postCount <-renderPlot({
senderPosts(file=input$file1$datapath,newParamter = input$sender)
})
# tabPanel 2
output$wordCount <-renderPlot({
wordFreq(file=input$file1$datapath)
})
# tabPanel 3
output$chatCloud <-renderPlot({
chatCloud(file=input$file1$datapath)
})
})