我正在使用rgl和shinyRGL包进行编码,试图通过让用户插入csv特定格式的文件来绘制3D线图.但是对象类型关闭错误不断出现.好像是因为找不到功能plot3d,或者我可能错了.
这是代码:
UI
library(shiny)
library(rgl)
library(shinyRGL)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
titlePanel("title panel"),
sidebarLayout(
sidebarPanel(
helpText("Please select a CSV file with the correct format."),
tags$hr(),
fileInput("file","Choose file to upload",accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv',
label = h3("File input"))
),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
actionButton("graph","PLOT!")
),
mainPanel(textOutput("text1"),
webGLOutput("Aplot")))
)
)
Run Code Online (Sandbox Code Playgroud)
服务器
library(shiny)
library(rgl)
library(shinyRGL)
options(shiny.maxRequestSize = 9*1024^2)
shinyServer(
function(input, output) {
output$text1 <- renderText({
paste("You have selected", input$select)
})
output$"Aplot" <- renderWebGL({
inFile <- reactive(input$file)
theFrames <- eventReactive(input$graph,read.csv(inFile$datapath,
header = input$header))
plot3d(theFrames[[4]],theFrames[[5]],theFrames[[6]],xlab="x",ylab="y",zlab
= "z", type = "l", col = ifelse(theFrames[[20]]>0.76,"red","blue"))
})
})
Run Code Online (Sandbox Code Playgroud)
错误
警告:包hinyRGL?是在R版本3.3.1下构建的警告:[[:类型'闭包'的对象不是子集表中的错误堆栈跟踪(最里面的第一个):70:plot3d 69:func [C:\ Users\Ian\workspace\Leap SDK/Test\App_1/server.R#19复制68:输出$ Aplot 1:runApp
请记住此错误消息,因为它对于闪亮的应用程序来说非常典型.
它几乎总是意味着,你有一个反应值,但没有用括号.
关于你的代码,我在这里发现了这个错误:
inFile <- reactive(input$file)
theFrames <- eventReactive(input$graph,read.csv(inFile$datapath,
header = input$header))
plot3d(theFrames[[4]],theFrames[[5]],theFrames[[6]],xlab="x",ylab="y",zlab
= "z", type = "l", col = ifelse(theFrames[[20]]>0.76,"red","blue"))
Run Code Online (Sandbox Code Playgroud)
你inFile像普通变量一样使用,但事实并非如此.它是一个反应值,因此必须被调用inFile().同样适用theFrames,你打电话给theFrames[[i]],但应该调用theFrames()[[i]].
所以正确的版本将是
inFile <- reactive(input$file)
theFrames <- eventReactive(input$graph,read.csv(inFile()$datapath,
header = input$header))
plot3d(theFrames()[[4]],theFrames()[[5]],theFrames()[[6]],xlab="x",ylab="y",zlab
= "z", type = "l", col = ifelse(theFrames()[[20]]>0.76,"red","blue"))
Run Code Online (Sandbox Code Playgroud)
可能还有一些关于错误消息的附加信息:Shiny仅在需要时评估变量,因此theFrames从plot3d函数内部执行包含错误的反应.这就是为什么错误消息会告诉您有关错误的信息plot3d,即使错误位于其他位置.