R 将被动对象中的值作为列添加到现有数据框中
我不熟悉使用Shiny,并且在向现有数据帧添加反应性值时遇到一些问题 我有一个称为CalculatedDistance的反应对象,它在Shining应用程序上的输入发生更改时计算距离。我试图将此对象中计算的距离添加到数据帧中的新列,但收到以下错误消息:R 将被动对象中的值作为列添加到现有数据框中,r,shiny,R,Shiny,我不熟悉使用Shiny,并且在向现有数据帧添加反应性值时遇到一些问题 我有一个称为CalculatedDistance的反应对象,它在Shining应用程序上的输入发生更改时计算距离。我试图将此对象中计算的距离添加到数据帧中的新列,但收到以下错误消息: CalculatedDistance[i]中的错误:“closure”类型的对象不可子集 在我尝试将值添加到新列之前,以下代码工作正常 library('shiny') #allows for the shiny app to be used
CalculatedDistance[i]中的错误:“closure”类型的对象不可子集
在我尝试将值添加到新列之前,以下代码工作正常
library('shiny') #allows for the shiny app to be used
alldata <- iris
#adds a column of a unique ID for each row
alldata$ID <- 1:nrow(alldata)
# UI
ui<-fluidPage(
titlePanel("Explorer"),
fluidRow(
wellPanel(
numericInput(inputId = "UserPetalLength", label="Input Petal Length", value = 0, step = 0.1),
numericInput(inputId = "UserPetalWidth", label="Input Petal Width", value = 0, step = 0.1)),
tableOutput('Table')
))
#SERVER
server<-function(input,output,session)
{
CalculatedDistance<- reactive({
calculatedDistance <- sqrt((alldata$Petal.Length-input$UserPetalLength)^2 + (alldata$Petal.Width-input$UserPetalWidth)^2)
})
alldata$distance<- NA
nRows <- nrow(alldata)
for (i in 1:nRows)
{
alldata$distance[i]= CalculatedDistance[i]
}
output$Table = renderTable(alldata)
}
#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)
library('shinny')#允许使用shinny应用程序
所有数据这对我有效;我在服务器中做了一些更改以使其正常工作
- 在反应式环境中包装添加到数据帧部分
- 您的循环不是必需的,因为
CalculatedDistance
返回一个向量,我们可以轻松地将它添加到数据帧中
- 在
output$表中添加了一对{}
- 将dataframe转换为datatable
#server.R
shinyServer(功能(输入、输出、会话)
{
CalculatedDistance这对我来说很有效;我在服务器中做了一些更改以使其正常工作
- 在反应式环境中包装添加到数据帧部分
- 您的循环不是必需的,因为
CalculatedDistance
返回一个向量,我们可以轻松地将它添加到数据帧中
- 在
output$表中添加了一对{}
- 将dataframe转换为datatable
#server.R
shinyServer(功能(输入、输出、会话)
{
CalculatedDistance我想您在服务器中遗漏了一组括号:所有数据$distance[i]=CalculatedDistance()[i]
可能有效您可能还应该将该部分包装成一个反应式
,这样每当用户petallength
更改时它都会更新。我认为您在服务器中遗漏了一组括号:所有数据$distance[i]=CalculatedDistance()[i]
可能有效您可能还应该将该部分包装在一个反应式
中,以便每次用户更改长度时它都会更新。谢谢!这很好!必须安装data.table软件包,但之后一切都很好!谢谢,这很好!必须安装data.table软件包,但之后一切都很好!
#server.R
shinyServer(function(input,output,session)
{
CalculatedDistance<- reactive({
calculatedDistance <- sqrt((alldata$Petal.Length-input$UserPetalLength)^2 + (alldata$Petal.Width-input$UserPetalWidth)^2)
calculatedDistance
})
add_to_df <- reactive({
alldata$distance<- NA
nRows <- nrow(alldata)
alldata$distance <- CalculatedDistance()
alldata
})
output$Table <- renderTable({
data.table(add_to_df())
})
})